Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,7 +38,9 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 38 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 39 |
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
|
| 44 |
class SmartAgentV2:
|
|
@@ -47,6 +49,10 @@ class SmartAgentV2:
|
|
| 47 |
self.summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 48 |
self.whisper_model = whisper.load_model("base")
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
def search_web(self, query):
|
| 51 |
with DDGS() as ddgs:
|
| 52 |
results = ddgs.text(query, max_results=3)
|
|
@@ -128,46 +134,45 @@ class SmartAgentV2:
|
|
| 128 |
q = question.lower().strip()
|
| 129 |
try:
|
| 130 |
if any(word in q for word in ["image", "chess", "diagram"]):
|
| 131 |
-
return "I'm a text-only agent and cannot interpret images."
|
| 132 |
if any(word in q for word in ["youtube", "video"]):
|
| 133 |
-
return "I'm unable to access or analyze video/audio from YouTube."
|
| 134 |
if 'etirw ,ecnetnes' in q:
|
| 135 |
-
return self.reverse_text_puzzle(question)
|
| 136 |
if "counter-examples" in q and "commutative" in q:
|
| 137 |
-
return self.non_commutative_subset()
|
| 138 |
if "vegetables" in q and "botany" in q:
|
| 139 |
-
return self.true_vegetables()
|
| 140 |
if file:
|
| 141 |
if filepath := getattr(file, "name", None):
|
| 142 |
if filepath.endswith(".mp3"):
|
| 143 |
transcript = self.transcribe_audio(filepath)
|
| 144 |
if "ingredient" in q:
|
| 145 |
-
return self.extract_ingredients(transcript)
|
| 146 |
if "page" in q:
|
| 147 |
-
return self.extract_page_numbers(transcript)
|
| 148 |
-
return transcript
|
| 149 |
elif filepath.endswith(".xlsx") or filepath.endswith(".xls"):
|
| 150 |
-
return self.sum_food_sales(filepath)
|
| 151 |
if q.startswith("summarize:"):
|
| 152 |
url = question.split(":", 1)[1].strip()
|
| 153 |
-
return self.summarize_url(url)
|
| 154 |
elif q.startswith("generate citation:") or q.startswith("cite:"):
|
| 155 |
url = question.split(":", 1)[1].strip()
|
| 156 |
-
return self.generate_citation(url)
|
| 157 |
elif q.startswith("search:"):
|
| 158 |
query = question.split(":", 1)[1].strip()
|
| 159 |
-
return self.search_web(query)
|
| 160 |
elif "wikipedia" in q:
|
| 161 |
-
return self.get_wikipedia_answer(question)
|
| 162 |
else:
|
| 163 |
-
return self.answer_fact(question)
|
| 164 |
except Exception as e:
|
| 165 |
logging.error(f"Error: {e}")
|
| 166 |
return "An error occurred processing the question."
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
-
|
| 171 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 172 |
"""
|
| 173 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 38 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 39 |
|
| 40 |
|
| 41 |
+
logging.basicConfig(filename="agent_debug.log", level=logging.INFO)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
|
| 45 |
|
| 46 |
class SmartAgentV2:
|
|
|
|
| 49 |
self.summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 50 |
self.whisper_model = whisper.load_model("base")
|
| 51 |
|
| 52 |
+
def log_response(self, qtype: str, question: str, answer: str):
|
| 53 |
+
logging.info(f"[TYPE: {qtype}] Q: {question}\nA: {answer}\n")
|
| 54 |
+
return answer
|
| 55 |
+
|
| 56 |
def search_web(self, query):
|
| 57 |
with DDGS() as ddgs:
|
| 58 |
results = ddgs.text(query, max_results=3)
|
|
|
|
| 134 |
q = question.lower().strip()
|
| 135 |
try:
|
| 136 |
if any(word in q for word in ["image", "chess", "diagram"]):
|
| 137 |
+
return self.log_response("image-block", question, "I'm a text-only agent and cannot interpret images.")
|
| 138 |
if any(word in q for word in ["youtube", "video"]):
|
| 139 |
+
return self.log_response("video-block", question, "I'm unable to access or analyze video/audio from YouTube.")
|
| 140 |
if 'etirw ,ecnetnes' in q:
|
| 141 |
+
return self.log_response("text-reversal", question, self.reverse_text_puzzle(question))
|
| 142 |
if "counter-examples" in q and "commutative" in q:
|
| 143 |
+
return self.log_response("logic-check", question, self.non_commutative_subset())
|
| 144 |
if "vegetables" in q and "botany" in q:
|
| 145 |
+
return self.log_response("classification", question, self.true_vegetables())
|
| 146 |
if file:
|
| 147 |
if filepath := getattr(file, "name", None):
|
| 148 |
if filepath.endswith(".mp3"):
|
| 149 |
transcript = self.transcribe_audio(filepath)
|
| 150 |
if "ingredient" in q:
|
| 151 |
+
return self.log_response("mp3-ingredients", question, self.extract_ingredients(transcript))
|
| 152 |
if "page" in q:
|
| 153 |
+
return self.log_response("mp3-pages", question, self.extract_page_numbers(transcript))
|
| 154 |
+
return self.log_response("mp3-generic", question, transcript)
|
| 155 |
elif filepath.endswith(".xlsx") or filepath.endswith(".xls"):
|
| 156 |
+
return self.log_response("excel-sum", question, self.sum_food_sales(filepath))
|
| 157 |
if q.startswith("summarize:"):
|
| 158 |
url = question.split(":", 1)[1].strip()
|
| 159 |
+
return self.log_response("summarize", question, self.summarize_url(url))
|
| 160 |
elif q.startswith("generate citation:") or q.startswith("cite:"):
|
| 161 |
url = question.split(":", 1)[1].strip()
|
| 162 |
+
return self.log_response("citation", question, self.generate_citation(url))
|
| 163 |
elif q.startswith("search:"):
|
| 164 |
query = question.split(":", 1)[1].strip()
|
| 165 |
+
return self.log_response("search", question, self.search_web(query))
|
| 166 |
elif "wikipedia" in q:
|
| 167 |
+
return self.log_response("wiki-lookup", question, self.get_wikipedia_answer(question))
|
| 168 |
else:
|
| 169 |
+
return self.log_response("fact-qa", question, self.answer_fact(question))
|
| 170 |
except Exception as e:
|
| 171 |
logging.error(f"Error: {e}")
|
| 172 |
return "An error occurred processing the question."
|
| 173 |
|
| 174 |
|
| 175 |
|
|
|
|
| 176 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 177 |
"""
|
| 178 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|