1
1
import base64
2
- import json
3
2
import logging
4
3
import os
5
4
import shutil
6
5
import tempfile
7
6
import time
8
7
import uuid
9
8
from enum import Enum
10
- from io import BytesIO
11
9
from typing import Optional
12
10
13
- import grpc
14
11
from asgiref .sync import async_to_sync
15
12
from django .conf import settings
13
+ from langrocks .client .files import FileOperations
14
+ from langrocks .common .models .files import FileMimeType
16
15
from pydantic import Field , model_validator
17
16
18
17
from llmstack .apps .schemas import OutputTemplate
@@ -51,41 +50,11 @@ def _file_extension_from_mime_type(mime_type):
51
50
return "bin"
52
51
53
52
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
-
85
53
class FileOperationsInput (ApiProcessorSchema ):
86
54
content : Optional [str ] = Field (
87
55
default = "" ,
88
56
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" },
89
58
)
90
59
content_mime_type : Optional [FileMimeType ] = Field (
91
60
default = FileMimeType .TEXT ,
@@ -105,7 +74,7 @@ class FileOperationsInput(ApiProcessorSchema):
105
74
)
106
75
output_mime_type : FileMimeType = Field (
107
76
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" ,
109
78
)
110
79
111
80
@model_validator (mode = "before" )
@@ -226,8 +195,6 @@ def get_bookkeeping_data(self) -> BookKeepingData:
226
195
)
227
196
228
197
def process (self ) -> dict :
229
- from langrocks .common .models import runner_pb2 , runner_pb2_grpc
230
-
231
198
input_content_bytes = None
232
199
input_content_mime_type = None
233
200
data_uri = None
@@ -256,26 +223,19 @@ def process(self) -> dict:
256
223
if operation == FileOperationOperation .CONVERT :
257
224
if input_content_bytes is None or input_content_mime_type is None :
258
225
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 ,
268
233
)
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 )
274
234
data_uri = create_data_uri (
275
- response_buffer . read () ,
235
+ response . data ,
276
236
str (self ._input .output_mime_type ),
277
237
base64_encode = True ,
278
- filename = full_file_path ,
238
+ filename = response . name ,
279
239
)
280
240
281
241
elif operation == FileOperationOperation .CREATE :
0 commit comments