Update app.py
Browse files
app.py
CHANGED
|
@@ -10,14 +10,56 @@ 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 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("BasicAgent initialized.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
fixed_answer = "This is a default answer."
|
| 19 |
-
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 20 |
-
return fixed_answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
|
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
+
from transformers import pipeline
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
class BasicAgent:
|
| 18 |
def __init__(self):
|
| 19 |
print("BasicAgent initialized.")
|
| 20 |
+
llm_pipeline = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.2")
|
| 21 |
+
from tools import (
|
| 22 |
+
web_search_tool,
|
| 23 |
+
analyze_image_tool,
|
| 24 |
+
ocr_tool,
|
| 25 |
+
video_ocr_tool,
|
| 26 |
+
document_tool,
|
| 27 |
+
chat_tool,
|
| 28 |
+
)
|
| 29 |
+
self.web_search_tool = web_search_tool
|
| 30 |
+
self.analyze_image_tool = analyze_image_tool
|
| 31 |
+
self.ocr_tool = ocr_tool
|
| 32 |
+
self.video_ocr_tool = video_ocr_tool
|
| 33 |
+
self.document_tool = document_tool
|
| 34 |
+
self.chat_tool = chat_tool
|
| 35 |
+
|
| 36 |
def __call__(self, question: str) -> str:
|
| 37 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 38 |
+
#fixed_answer = "This is a default answer."
|
| 39 |
+
#print(f"Agent returning fixed answer: {fixed_answer}")
|
| 40 |
+
#return fixed_answer
|
| 41 |
+
|
| 42 |
+
classification = llm(f"Classify the question: '{question}' into one of: web_search, image_analysis, ocr, video_ocr, document, chat")
|
| 43 |
+
if classification == "web_search":
|
| 44 |
+
return self.web_search_tool(question)
|
| 45 |
+
elif classification == "image_analysis":
|
| 46 |
+
return self.analyze_image_tool(question)
|
| 47 |
+
elif classification == "ocr":
|
| 48 |
+
return self.ocr_tool(question)
|
| 49 |
+
elif classification == "video_ocr":
|
| 50 |
+
return self.video_ocr_tool(question)
|
| 51 |
+
elif classification == "document":
|
| 52 |
+
return self.document_tool(question)
|
| 53 |
+
elif classification == "chat":
|
| 54 |
+
return self.chat_tool(question)
|
| 55 |
+
else:
|
| 56 |
+
return "Didn't understand you."
|
| 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 |
"""
|