Skip to content

Commit b0cdc63

Browse files
committed
Use converter from langrocks
1 parent 40f4e75 commit b0cdc63

File tree

1 file changed

+13
-53
lines changed

1 file changed

+13
-53
lines changed

llmstack/processors/providers/promptly/file_operations.py

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import base64
2-
import json
32
import logging
43
import os
54
import shutil
65
import tempfile
76
import time
87
import uuid
98
from enum import Enum
10-
from io import BytesIO
119
from typing import Optional
1210

13-
import grpc
1411
from asgiref.sync import async_to_sync
1512
from django.conf import settings
13+
from langrocks.client.files import FileOperations
14+
from langrocks.common.models.files import FileMimeType
1615
from pydantic import Field, model_validator
1716

1817
from llmstack.apps.schemas import OutputTemplate
@@ -51,41 +50,11 @@ def _file_extension_from_mime_type(mime_type):
5150
return "bin"
5251

5352

54-
class FileMimeType(str, Enum):
55-
TEXT = "text/plain"
56-
HTML = "text/html"
57-
CSS = "text/css"
58-
JAVASCRIPT = "application/javascript"
59-
JSON = "application/json"
60-
XML = "application/xml"
61-
CSV = "text/csv"
62-
TSV = "text/tab-separated-values"
63-
MARKDOWN = "text/markdown"
64-
PDF = "application/pdf"
65-
OCTET_STREAM = "application/octet-stream"
66-
67-
def __str__(self):
68-
return self.value
69-
70-
def grpc_mime_type(self):
71-
from langrocks.common.models import runner_pb2
72-
73-
if self == FileMimeType.TEXT:
74-
return runner_pb2.ContentMimeType.TEXT
75-
elif self == FileMimeType.HTML:
76-
return runner_pb2.ContentMimeType.HTML
77-
elif self == FileMimeType.JSON:
78-
return runner_pb2.ContentMimeType.JSON
79-
elif self == FileMimeType.PDF:
80-
return runner_pb2.ContentMimeType.PDF
81-
else:
82-
return runner_pb2.ContentMimeType.TEXT
83-
84-
8553
class FileOperationsInput(ApiProcessorSchema):
8654
content: Optional[str] = Field(
8755
default="",
8856
description="The contents of the file. Skip this field if you want to create an archive of the directory",
57+
json_schema_extra={"widget": "textarea"},
8958
)
9059
content_mime_type: Optional[FileMimeType] = Field(
9160
default=FileMimeType.TEXT,
@@ -105,7 +74,7 @@ class FileOperationsInput(ApiProcessorSchema):
10574
)
10675
output_mime_type: FileMimeType = Field(
10776
default=FileMimeType.TEXT,
108-
description="The mimetype of the file. If not provided, it will be inferred from the filename",
77+
description="The mimetype of the output file. If not provided, it will be inferred from the filename",
10978
)
11079

11180
@model_validator(mode="before")
@@ -226,8 +195,6 @@ def get_bookkeeping_data(self) -> BookKeepingData:
226195
)
227196

228197
def process(self) -> dict:
229-
from langrocks.common.models import runner_pb2, runner_pb2_grpc
230-
231198
input_content_bytes = None
232199
input_content_mime_type = None
233200
data_uri = None
@@ -256,26 +223,19 @@ def process(self) -> dict:
256223
if operation == FileOperationOperation.CONVERT:
257224
if input_content_bytes is None or input_content_mime_type is None:
258225
raise ValueError("Content is missing or invalid")
259-
with grpc.insecure_channel(f"{settings.RUNNER_HOST}:{settings.RUNNER_PORT}") as channel:
260-
stub = runner_pb2_grpc.RunnerStub(channel)
261-
request = runner_pb2.FileConverterRequest(
262-
file=runner_pb2.Content(
263-
data=input_content_bytes,
264-
mime_type=input_content_mime_type.grpc_mime_type(),
265-
),
266-
target_mime_type=self._input.output_mime_type.grpc_mime_type(),
267-
options=json.loads(self._config.operation_config),
226+
227+
with FileOperations(f"{settings.RUNNER_HOST}:{settings.RUNNER_PORT}") as fops:
228+
response = fops.convert_file(
229+
data=input_content_bytes,
230+
filename=filename,
231+
input_mime_type=input_content_mime_type,
232+
output_mime_type=self._input.output_mime_type,
268233
)
269-
response_iter = stub.GetFileConverter(iter([request]))
270-
response_buffer = BytesIO()
271-
for response in response_iter:
272-
response_buffer.write(response.file.data)
273-
response_buffer.seek(0)
274234
data_uri = create_data_uri(
275-
response_buffer.read(),
235+
response.data,
276236
str(self._input.output_mime_type),
277237
base64_encode=True,
278-
filename=full_file_path,
238+
filename=response.name,
279239
)
280240

281241
elif operation == FileOperationOperation.CREATE:

0 commit comments

Comments
 (0)