Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,13 +18,73 @@ class BasicAgent:
|
|
| 18 |
print("BasicAgent initialized.")
|
| 19 |
self.graph = simple_graph()
|
| 20 |
|
| 21 |
-
def __call__(self, question: str) -> str:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 30 |
"""
|
|
@@ -86,8 +146,29 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 86 |
if not task_id or question_text is None:
|
| 87 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 88 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
try:
|
| 90 |
-
submitted_answer = agent(question_text)
|
|
|
|
|
|
|
| 91 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 92 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 93 |
except Exception as e:
|
|
|
|
| 18 |
print("BasicAgent initialized.")
|
| 19 |
self.graph = simple_graph()
|
| 20 |
|
| 21 |
+
def __call__(self, question: str, file_bytes: bytes = None, file_type: str = None) -> str:
|
| 22 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 23 |
+
|
| 24 |
+
file_info = ""
|
| 25 |
+
if file_bytes and file_type:
|
| 26 |
+
file_info = self.process_file(file_bytes, file_type)
|
| 27 |
+
|
| 28 |
+
full_prompt = question
|
| 29 |
+
if file_info:
|
| 30 |
+
full_prompt += f"\n\n[File Content Below]\n{file_info}"
|
| 31 |
+
|
| 32 |
+
if file_type == "png":
|
| 33 |
+
try:
|
| 34 |
+
encoded_image = base64.b64encode(file_bytes).decode("utf-8")
|
| 35 |
+
|
| 36 |
+
messages = [HumanMessage(content=[
|
| 37 |
+
{"type": "text", "text": question},
|
| 38 |
+
{"type": "image_url", "image_url": f"data:image/png;base64,{encoded_image}"}
|
| 39 |
+
])]
|
| 40 |
+
|
| 41 |
+
response = self.final_agent.invoke({"messages": messages})
|
| 42 |
+
answer = response['messages'][-1].content
|
| 43 |
+
return answer.split("FINAL ANSWER:")[-1].strip()
|
| 44 |
+
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"Error processing image: {e}")
|
| 47 |
+
return f"Error: {e}"
|
| 48 |
+
|
| 49 |
+
elif file_type == "mp3":
|
| 50 |
+
try:
|
| 51 |
+
encoded_audio = base64.b64encode(file_bytes).decode("utf-8")
|
| 52 |
+
audio_mime_type = "audio/mpeg"
|
| 53 |
+
|
| 54 |
+
messages = [HumanMessage(content=[
|
| 55 |
+
{"type": "text", "text": question},
|
| 56 |
+
{
|
| 57 |
+
"type": "media",
|
| 58 |
+
"data": encoded_audio,
|
| 59 |
+
"mime_type": audio_mime_type,
|
| 60 |
+
}
|
| 61 |
+
])]
|
| 62 |
+
|
| 63 |
+
response = self.final_agent.invoke({"messages": messages})
|
| 64 |
+
answer = response['messages'][-1].content
|
| 65 |
+
return answer.split("FINAL ANSWER:")[-1].strip()
|
| 66 |
+
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print(f"Error processing audio: {e}")
|
| 69 |
+
return f"Error: {e}"
|
| 70 |
+
|
| 71 |
+
else:
|
| 72 |
+
messages = [HumanMessage(content=full_prompt)]
|
| 73 |
+
messages = self.final_agent.invoke({"messages": messages})
|
| 74 |
+
answer = messages['messages'][-1].content
|
| 75 |
+
return answer.split("FINAL ANSWER:")[-1].strip()
|
| 76 |
+
|
| 77 |
+
def process_file(self, file_bytes: bytes, file_type: str) -> str:
|
| 78 |
+
try:
|
| 79 |
+
if file_type == "py":
|
| 80 |
+
return file_bytes.decode("utf-8")
|
| 81 |
+
|
| 82 |
+
elif file_type == "xlsx":
|
| 83 |
+
df = pd.read_excel(BytesIO(file_bytes))
|
| 84 |
+
return df.to_string()
|
| 85 |
+
|
| 86 |
+
except Exception as e:
|
| 87 |
+
return f"Error processing file: {e}"
|
| 88 |
|
| 89 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 90 |
"""
|
|
|
|
| 146 |
if not task_id or question_text is None:
|
| 147 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 148 |
continue
|
| 149 |
+
file_bytes = None
|
| 150 |
+
file_type = None
|
| 151 |
+
try:
|
| 152 |
+
file_url = f"{api_url}/files/{task_id}"
|
| 153 |
+
file_response = requests.get(file_url, timeout=10)
|
| 154 |
+
if file_response.status_code == 200:
|
| 155 |
+
content_type = file_response.headers.get("Content-Type", "")
|
| 156 |
+
file_bytes = file_response.content
|
| 157 |
+
if "image/png" in content_type:
|
| 158 |
+
file_type = "png"
|
| 159 |
+
elif "audio/mpeg" in content_type:
|
| 160 |
+
file_type = "mp3"
|
| 161 |
+
elif "application/octet-stream" in content_type:
|
| 162 |
+
file_type = "xlsx"
|
| 163 |
+
elif "text/x-python" in content_type or "text/plain" in content_type:
|
| 164 |
+
file_type = "py"
|
| 165 |
+
except Exception as e:
|
| 166 |
+
print(f"Could not fetch or interpret file for task {task_id}: {e}")
|
| 167 |
+
|
| 168 |
try:
|
| 169 |
+
submitted_answer = agent(question_text, file_bytes=file_bytes, file_type=file_type)
|
| 170 |
+
if submitted_answer is None:
|
| 171 |
+
submitted_answer = "No answer returned."
|
| 172 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 173 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 174 |
except Exception as e:
|