chore: Refactor process_input function to support local and online browsing
Browse files
main.py
CHANGED
@@ -47,11 +47,11 @@ class MultiOnInputBrowse(BaseModel):
|
|
47 |
Attributes:
|
48 |
cmd (str): The command to execute. Example: "post 'hello world - I love multion' on twitter".
|
49 |
url (str): The URL where the action should be performed. Example: "https://twitter.com".
|
50 |
-
local (bool): Flag indicating whether the action should be performed locally. Default is
|
51 |
"""
|
52 |
cmd: str
|
53 |
url: str
|
54 |
-
local: bool =
|
55 |
|
56 |
async def process_image_file(file: UploadFile) -> str:
|
57 |
"""
|
@@ -69,12 +69,16 @@ async def process_image_file(file: UploadFile) -> str:
|
|
69 |
if file.content_type not in ["image/jpeg", "image/png"]:
|
70 |
raise HTTPException(status_code=400, detail="Invalid file type. Only JPEG and PNG are supported.")
|
71 |
|
|
|
72 |
image_data = await file.read()
|
73 |
image = Image.open(io.BytesIO(image_data))
|
|
|
74 |
|
75 |
try:
|
|
|
76 |
enc_image = model.encode_image(image)
|
77 |
description = model.answer_question(enc_image, "Describe this image.", tokenizer)
|
|
|
78 |
return description
|
79 |
except Exception as e:
|
80 |
raise HTTPException(status_code=500, detail=str(e))
|
@@ -84,10 +88,11 @@ def read_root():
|
|
84 |
return {"Hello": "World"}
|
85 |
|
86 |
@app.post("/process-input/")
|
87 |
-
async def process_input(text: str = Form(...), file: UploadFile = File(None)):
|
88 |
if file is not None:
|
89 |
try:
|
90 |
print("Processing image file")
|
|
|
91 |
image_description = await process_image_file(file)
|
92 |
print(f"Image description: {image_description}")
|
93 |
except HTTPException as e:
|
@@ -104,21 +109,23 @@ async def process_input(text: str = Form(...), file: UploadFile = File(None)):
|
|
104 |
|
105 |
print(f"Processed text: {processed_text}")
|
106 |
command = await generate_command(processed_text)
|
107 |
-
print(f"Command generated: {command
|
108 |
-
|
109 |
-
try:
|
110 |
-
print("Calling MultiOn API")
|
111 |
-
response = multion.browse(
|
112 |
-
cmd=command.cmd,
|
113 |
-
url=command.url,
|
114 |
-
local=command.local
|
115 |
-
)
|
116 |
-
print(f"Response received: {response.message}")
|
117 |
-
return JSONResponse(content={"response": response.message, "command": command.model_dump()})
|
118 |
-
|
119 |
-
except Exception as e:
|
120 |
-
raise HTTPException(status_code=500, detail=f"Mution API error: {str(e)}")
|
121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
async def generate_command(content: str) -> MultiOnInputBrowse:
|
124 |
try:
|
|
|
47 |
Attributes:
|
48 |
cmd (str): The command to execute. Example: "post 'hello world - I love multion' on twitter".
|
49 |
url (str): The URL where the action should be performed. Example: "https://twitter.com".
|
50 |
+
local (bool): Flag indicating whether the action should be performed locally. Default is False.
|
51 |
"""
|
52 |
cmd: str
|
53 |
url: str
|
54 |
+
local: bool = False
|
55 |
|
56 |
async def process_image_file(file: UploadFile) -> str:
|
57 |
"""
|
|
|
69 |
if file.content_type not in ["image/jpeg", "image/png"]:
|
70 |
raise HTTPException(status_code=400, detail="Invalid file type. Only JPEG and PNG are supported.")
|
71 |
|
72 |
+
print("Reading image file")
|
73 |
image_data = await file.read()
|
74 |
image = Image.open(io.BytesIO(image_data))
|
75 |
+
print("Image loaded")
|
76 |
|
77 |
try:
|
78 |
+
print("Encoding image")
|
79 |
enc_image = model.encode_image(image)
|
80 |
description = model.answer_question(enc_image, "Describe this image.", tokenizer)
|
81 |
+
print("Image description generated")
|
82 |
return description
|
83 |
except Exception as e:
|
84 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
88 |
return {"Hello": "World"}
|
89 |
|
90 |
@app.post("/process-input/")
|
91 |
+
async def process_input(text: str = Form(...), file: UploadFile = File(None), online: bool = Form(False)):
|
92 |
if file is not None:
|
93 |
try:
|
94 |
print("Processing image file")
|
95 |
+
print(f"File type: type(file) = {type(file)}, Filename: {file.filename}, Content type: {file.content_type}")
|
96 |
image_description = await process_image_file(file)
|
97 |
print(f"Image description: {image_description}")
|
98 |
except HTTPException as e:
|
|
|
109 |
|
110 |
print(f"Processed text: {processed_text}")
|
111 |
command = await generate_command(processed_text)
|
112 |
+
print(f"Command generated: {command}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
+
if not online and not command.local:
|
115 |
+
try:
|
116 |
+
print("Calling MultiOn API with online=True")
|
117 |
+
response = multion.browse(
|
118 |
+
cmd=command.cmd,
|
119 |
+
url=command.url,
|
120 |
+
local=command.local
|
121 |
+
)
|
122 |
+
print(f"Response received: {response.message}")
|
123 |
+
return JSONResponse(content={"response": response.message, "command": command.model_dump()})
|
124 |
+
|
125 |
+
except Exception as e:
|
126 |
+
raise HTTPException(status_code=500, detail=f"Mution API error: {str(e)}")
|
127 |
+
else:
|
128 |
+
return JSONResponse(content={"response": "This command is for local browsing", "command": command.model_dump()})
|
129 |
|
130 |
async def generate_command(content: str) -> MultiOnInputBrowse:
|
131 |
try:
|