Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,71 +23,55 @@ class BasicAgent:
|
|
| 23 |
|
| 24 |
def __call__(self, question: str, file_bytes: bytes = None, file_type: str = None) -> str:
|
| 25 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 26 |
-
|
| 27 |
-
file_info = ""
|
| 28 |
-
if file_bytes and file_type:
|
| 29 |
-
file_info = self.process_file(file_bytes, file_type)
|
| 30 |
-
|
| 31 |
-
full_prompt = question
|
| 32 |
-
if file_info:
|
| 33 |
-
full_prompt += f"\n\n[File Content Below]\n{file_info}"
|
| 34 |
-
|
| 35 |
-
if file_type == "png":
|
| 36 |
-
try:
|
| 37 |
-
encoded_image = base64.b64encode(file_bytes).decode("utf-8")
|
| 38 |
-
|
| 39 |
-
messages = [HumanMessage(content=[
|
| 40 |
-
{"type": "text", "text": question},
|
| 41 |
-
{"type": "image_url", "image_url": f"data:image/png;base64,{encoded_image}"}
|
| 42 |
-
])]
|
| 43 |
-
|
| 44 |
-
response = self.graph.invoke({"messages": messages})
|
| 45 |
-
answer = response['messages'][-1].content
|
| 46 |
-
return answer.split("FINAL ANSWER:")[-1].strip()
|
| 47 |
-
|
| 48 |
-
except Exception as e:
|
| 49 |
-
print(f"Error processing image: {e}")
|
| 50 |
-
return f"Error: {e}"
|
| 51 |
-
|
| 52 |
-
elif file_type == "mp3":
|
| 53 |
-
try:
|
| 54 |
-
encoded_audio = base64.b64encode(file_bytes).decode("utf-8")
|
| 55 |
-
audio_mime_type = "audio/mpeg"
|
| 56 |
-
|
| 57 |
-
messages = [HumanMessage(content=[
|
| 58 |
-
{"type": "text", "text": question},
|
| 59 |
-
{
|
| 60 |
-
"type": "media",
|
| 61 |
-
"data": encoded_audio,
|
| 62 |
-
"mime_type": audio_mime_type,
|
| 63 |
-
}
|
| 64 |
-
])]
|
| 65 |
-
|
| 66 |
-
response = self.graph.invoke({"messages": messages})
|
| 67 |
-
answer = response['messages'][-1].content
|
| 68 |
-
return answer.split("FINAL ANSWER:")[-1].strip()
|
| 69 |
-
|
| 70 |
-
except Exception as e:
|
| 71 |
-
print(f"Error processing audio: {e}")
|
| 72 |
-
return f"Error: {e}"
|
| 73 |
-
|
| 74 |
-
else:
|
| 75 |
-
messages = [HumanMessage(content=full_prompt)]
|
| 76 |
-
messages = self.graph.invoke({"messages": messages})
|
| 77 |
-
answer = messages['messages'][-1].content
|
| 78 |
-
return answer.split("FINAL ANSWER:")[-1].strip()
|
| 79 |
|
| 80 |
-
|
| 81 |
try:
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
except Exception as e:
|
| 90 |
-
return f"Error processing
|
| 91 |
|
| 92 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 93 |
"""
|
|
|
|
| 23 |
|
| 24 |
def __call__(self, question: str, file_bytes: bytes = None, file_type: str = None) -> str:
|
| 25 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
messages = []
|
| 28 |
try:
|
| 29 |
+
# Handle special formats
|
| 30 |
+
if file_bytes and file_type:
|
| 31 |
+
if file_type == "png":
|
| 32 |
+
encoded_image = base64.b64encode(file_bytes).decode("utf-8")
|
| 33 |
+
messages = [HumanMessage(content=[
|
| 34 |
+
{"type": "text", "text": question},
|
| 35 |
+
{"type": "image_url", "image_url": f"data:image/png;base64,{encoded_image}"}
|
| 36 |
+
])]
|
| 37 |
+
|
| 38 |
+
elif file_type == "mp3":
|
| 39 |
+
encoded_audio = base64.b64encode(file_bytes).decode("utf-8")
|
| 40 |
+
audio_mime_type = "audio/mpeg"
|
| 41 |
+
messages = [HumanMessage(content=[
|
| 42 |
+
{"type": "text", "text": question},
|
| 43 |
+
{
|
| 44 |
+
"type": "media",
|
| 45 |
+
"data": encoded_audio,
|
| 46 |
+
"mime_type": audio_mime_type,
|
| 47 |
+
}
|
| 48 |
+
])]
|
| 49 |
+
|
| 50 |
+
else:
|
| 51 |
+
# For general files like .py, .xlsx
|
| 52 |
+
file_info = ""
|
| 53 |
+
if file_type == "py":
|
| 54 |
+
file_info = file_bytes.decode("utf-8")
|
| 55 |
+
elif file_type == "xlsx":
|
| 56 |
+
df = pd.read_excel(BytesIO(file_bytes))
|
| 57 |
+
file_info = df.to_string()
|
| 58 |
+
else:
|
| 59 |
+
file_info = f"[Unsupported file type: {file_type}]"
|
| 60 |
+
|
| 61 |
+
full_prompt = f"{question}\n\n[File Content Below]\n{file_info}"
|
| 62 |
+
messages = [HumanMessage(content=full_prompt)]
|
| 63 |
+
|
| 64 |
+
else:
|
| 65 |
+
# No file attached
|
| 66 |
+
messages = [HumanMessage(content=question)]
|
| 67 |
+
|
| 68 |
+
# Invoke final agent and extract answer
|
| 69 |
+
response = self.final_agent.invoke({"messages": messages})
|
| 70 |
+
answer = response['messages'][-1].content
|
| 71 |
+
return answer.split("FINAL ANSWER:")[-1].strip()
|
| 72 |
+
|
| 73 |
except Exception as e:
|
| 74 |
+
return f"Error during processing: {e}"
|
| 75 |
|
| 76 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 77 |
"""
|