Update app.py
Browse files
app.py
CHANGED
|
@@ -10,56 +10,61 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
-
from
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
class BasicAgent:
|
| 18 |
-
def __init__(self):
|
| 19 |
print("BasicAgent initialized.")
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
def __call__(self, question: str) -> str:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
elif
|
| 46 |
-
return
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
elif
|
| 54 |
-
return
|
|
|
|
| 55 |
else:
|
| 56 |
-
return "
|
| 57 |
|
| 58 |
|
| 59 |
-
|
| 60 |
-
def llm(prompt: str) -> str:
|
| 61 |
-
result = llm_pipeline(prompt, max_new_tokens=100, do_sample=False)
|
| 62 |
-
return result[0]['generated_text']
|
| 63 |
|
| 64 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 65 |
"""
|
|
|
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
+
from tools import (
|
| 14 |
+
web_search_tool,
|
| 15 |
+
image_analysis_tool,
|
| 16 |
+
audio_transcription_tool,
|
| 17 |
+
document_analysis_tool,
|
| 18 |
+
llm_tool
|
| 19 |
+
)
|
| 20 |
|
| 21 |
class BasicAgent:
|
| 22 |
+
def __init__(self, llm):
|
| 23 |
print("BasicAgent initialized.")
|
| 24 |
+
self.llm = llm
|
| 25 |
+
|
| 26 |
+
def classify_question(self, question: str) -> str:
|
| 27 |
+
prompt = f"""
|
| 28 |
+
You are a question classifier.
|
| 29 |
+
Classify the question into one of the following categories:
|
| 30 |
+
|
| 31 |
+
- web: for questions that require searching current information online
|
| 32 |
+
- image: when user uploads an image or asks about visual content
|
| 33 |
+
- audio: when user uploads an audio or asks what is said
|
| 34 |
+
- video: for YouTube or video questions
|
| 35 |
+
- document: for PDFs or documents
|
| 36 |
+
- text: for general logic, reasoning, or knowledge questions
|
| 37 |
+
|
| 38 |
+
Question: "{question}"
|
| 39 |
+
|
| 40 |
+
Return only the label.
|
| 41 |
+
"""
|
| 42 |
+
return self.llm(prompt).strip().lower()
|
| 43 |
|
| 44 |
+
def __call__(self, question: str, file_path: str = None) -> str:
|
| 45 |
+
category = self.classify_question(question)
|
| 46 |
+
|
| 47 |
+
if category == "web":
|
| 48 |
+
return web_search_tool(question)
|
| 49 |
+
|
| 50 |
+
elif category == "image" and file_path:
|
| 51 |
+
return image_analysis_tool(file_path)
|
| 52 |
+
|
| 53 |
+
elif category == "audio" and file_path:
|
| 54 |
+
return audio_transcription_tool(file_path)
|
| 55 |
+
|
| 56 |
+
elif category == "document" and file_path:
|
| 57 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 58 |
+
content = f.read()
|
| 59 |
+
return document_analysis_tool(content)
|
| 60 |
+
|
| 61 |
+
elif category == "text":
|
| 62 |
+
return llm_tool(question, self.llm)
|
| 63 |
+
|
| 64 |
else:
|
| 65 |
+
return f"Sorry, I couldn't handle this category: {category}"
|
| 66 |
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 70 |
"""
|