Spaces:
Runtime error
Runtime error
update final answer extraction
Browse files
app.py
CHANGED
|
@@ -14,6 +14,7 @@ from youtube_transcript_api._errors import TranscriptsDisabled, NoTranscriptFoun
|
|
| 14 |
from urllib.parse import urlparse, parse_qs
|
| 15 |
import json
|
| 16 |
import whisper
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
|
|
@@ -153,12 +154,43 @@ class BasicAgent:
|
|
| 153 |
)
|
| 154 |
print("BasicAgent initialized.")
|
| 155 |
|
| 156 |
-
def __call__(self, task_id:str, question: str, file_name: str) -> str:
|
| 157 |
if file_name:
|
| 158 |
question = self.enrich_question_with_associated_file_details(task_id, question, file_name)
|
| 159 |
|
| 160 |
final_result = self.code_agent.run(question)
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
def enrich_question_with_associated_file_details(self, task_id:str, question: str, file_name: str) -> str:
|
| 164 |
api_url = DEFAULT_API_URL
|
|
|
|
| 14 |
from urllib.parse import urlparse, parse_qs
|
| 15 |
import json
|
| 16 |
import whisper
|
| 17 |
+
import re
|
| 18 |
|
| 19 |
|
| 20 |
|
|
|
|
| 154 |
)
|
| 155 |
print("BasicAgent initialized.")
|
| 156 |
|
| 157 |
+
def __call__(self, task_id: str, question: str, file_name: str) -> str:
|
| 158 |
if file_name:
|
| 159 |
question = self.enrich_question_with_associated_file_details(task_id, question, file_name)
|
| 160 |
|
| 161 |
final_result = self.code_agent.run(question)
|
| 162 |
+
|
| 163 |
+
# Extract text after "FINAL ANSWER:" (case-insensitive, and trims whitespace)
|
| 164 |
+
match = re.search(r'final answer:\s*(.*)', str(final_result), re.IGNORECASE | re.DOTALL)
|
| 165 |
+
if match:
|
| 166 |
+
return match.group(1).strip()
|
| 167 |
+
|
| 168 |
+
# Fallback in case the pattern is not found
|
| 169 |
+
return str(final_result).strip()
|
| 170 |
+
|
| 171 |
+
def enrich_question_with_associated_file_details(self, task_id:str, question: str, file_name: str) -> str:
|
| 172 |
+
api_url = DEFAULT_API_URL
|
| 173 |
+
get_associated_files_url = f"{api_url}/files/{task_id}"
|
| 174 |
+
response = requests.get(get_associated_files_url, timeout=15)
|
| 175 |
+
response.raise_for_status()
|
| 176 |
+
|
| 177 |
+
if file_name.endswith(".mp3"):
|
| 178 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 179 |
+
tmp_file.write(response.content)
|
| 180 |
+
file_path = tmp_file.name
|
| 181 |
+
return question + "\n\nMentioned .mp3 file local path is: " + file_path
|
| 182 |
+
elif file_name.endswith(".py"):
|
| 183 |
+
file_content = response.text
|
| 184 |
+
return question + "\n\nBelow is mentioned Python file:\n\n```python\n" + file_content + "\n```\n"
|
| 185 |
+
elif file_name.endswith(".xlsx"):
|
| 186 |
+
xlsx_io = BytesIO(response.content)
|
| 187 |
+
df = pd.read_excel(xlsx_io)
|
| 188 |
+
file_content = df.to_csv(index=False)
|
| 189 |
+
return question + "\n\nBelow is mentioned excel file in CSV format:\n\n```csv\n" + file_content + "\n```\n"
|
| 190 |
+
elif file_name.endswith(".png"):
|
| 191 |
+
base64_str = base64.b64encode(response.content).decode('utf-8')
|
| 192 |
+
return question + "\n\nBelow is the .png image in base64 format:\n\n```base64\n" + base64_str + "\n```\n"
|
| 193 |
+
|
| 194 |
|
| 195 |
def enrich_question_with_associated_file_details(self, task_id:str, question: str, file_name: str) -> str:
|
| 196 |
api_url = DEFAULT_API_URL
|