AkylaiBva commited on
Commit
f93562b
·
verified ·
1 Parent(s): 867526a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -43
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 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
  """
 
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
  """