Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,8 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool
|
|
|
|
| 6 |
|
| 7 |
# --- Constants ---
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring. hf.space"
|
|
@@ -12,8 +13,8 @@ class IntelligentAgent:
|
|
| 12 |
def __init__(self):
|
| 13 |
print("Initializing IntelligentAgent with tools...")
|
| 14 |
try:
|
| 15 |
-
# Initialize the model engine
|
| 16 |
-
model =
|
| 17 |
|
| 18 |
# Initialize tools for web search and browsing
|
| 19 |
search_tool = DuckDuckGoSearchTool()
|
|
@@ -32,9 +33,9 @@ class IntelligentAgent:
|
|
| 32 |
self.agent = None
|
| 33 |
|
| 34 |
def __call__(self, question: str) -> str:
|
| 35 |
-
print(f"Agent received question:
|
| 36 |
|
| 37 |
-
if self.agent is None:
|
| 38 |
return "Agent initialization failed."
|
| 39 |
|
| 40 |
try:
|
|
@@ -45,8 +46,8 @@ class IntelligentAgent:
|
|
| 45 |
return answer
|
| 46 |
except Exception as e:
|
| 47 |
print(f"Error running agent: {e}")
|
| 48 |
-
# Fallback:
|
| 49 |
-
return f"I encountered an error while processing this question.
|
| 50 |
|
| 51 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 52 |
"""
|
|
@@ -124,16 +125,16 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 124 |
"task_id": task_id,
|
| 125 |
"submitted_answer": answer
|
| 126 |
})
|
| 127 |
-
results_log.append((task_id, question_text[:
|
| 128 |
print(f"β
Answer generated: {answer[:100]}...")
|
| 129 |
-
except Exception as e:
|
| 130 |
print(f"β Error running agent on question {task_id}: {e}")
|
| 131 |
error_answer = "I apologize, but I encountered an error processing this question."
|
| 132 |
-
answers_payload.append({
|
| 133 |
-
"task_id":
|
| 134 |
"submitted_answer": error_answer
|
| 135 |
})
|
| 136 |
-
results_log.append((task_id, question_text[:50], f"Error:
|
| 137 |
|
| 138 |
print("\n" + "=" * 60)
|
| 139 |
print(f"β
Completed processing all questions!")
|
|
@@ -143,7 +144,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 143 |
try:
|
| 144 |
payload = {
|
| 145 |
"username": username,
|
| 146 |
-
"answers":
|
| 147 |
"agent_code": agent_code
|
| 148 |
}
|
| 149 |
|
|
@@ -152,14 +153,14 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 152 |
json=payload,
|
| 153 |
timeout=60
|
| 154 |
)
|
| 155 |
-
submit_response.raise_for_status()
|
| 156 |
-
submission_result = submit_response.json()
|
| 157 |
print(f"β
Submission successful: {submission_result}")
|
| 158 |
-
except requests.
|
| 159 |
print(f"β HTTP Error submitting answers: {e}")
|
| 160 |
-
print(f"Response text: {submit_response.text[:500]}")
|
| 161 |
return f"Error submitting answers: {e}\n\nResponse: {submit_response.text[:200]}", None
|
| 162 |
-
except Exception as e:
|
| 163 |
print(f"β Error submitting answers: {e}")
|
| 164 |
return f"Error submitting answers: {e}", None
|
| 165 |
|
|
@@ -173,7 +174,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 173 |
result_message = f"""
|
| 174 |
## π Submission Successful!
|
| 175 |
|
| 176 |
-
### π Your Results:
|
| 177 |
- **Score:** {score}
|
| 178 |
- **Username:** {username}
|
| 179 |
- **Questions Answered:** {total_questions}
|
|
@@ -226,7 +227,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 226 |
output_text = gr. Markdown()
|
| 227 |
|
| 228 |
with gr.Row():
|
| 229 |
-
output_table = gr.
|
| 230 |
|
| 231 |
submit_button.click(
|
| 232 |
run_and_submit_all,
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool
|
| 6 |
+
from smolagents.models import HfEngine
|
| 7 |
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring. hf.space"
|
|
|
|
| 13 |
def __init__(self):
|
| 14 |
print("Initializing IntelligentAgent with tools...")
|
| 15 |
try:
|
| 16 |
+
# Initialize the model engine
|
| 17 |
+
model = HfEngine(model_id="Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 18 |
|
| 19 |
# Initialize tools for web search and browsing
|
| 20 |
search_tool = DuckDuckGoSearchTool()
|
|
|
|
| 33 |
self.agent = None
|
| 34 |
|
| 35 |
def __call__(self, question: str) -> str:
|
| 36 |
+
print(f"Agent received question: {question[:100]}...")
|
| 37 |
|
| 38 |
+
if self. agent is None:
|
| 39 |
return "Agent initialization failed."
|
| 40 |
|
| 41 |
try:
|
|
|
|
| 46 |
return answer
|
| 47 |
except Exception as e:
|
| 48 |
print(f"Error running agent: {e}")
|
| 49 |
+
# Fallback: try to give a reasonable response
|
| 50 |
+
return f"I encountered an error while processing this question."
|
| 51 |
|
| 52 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 53 |
"""
|
|
|
|
| 125 |
"task_id": task_id,
|
| 126 |
"submitted_answer": answer
|
| 127 |
})
|
| 128 |
+
results_log.append((task_id, question_text[:50], answer[:100]))
|
| 129 |
print(f"β
Answer generated: {answer[:100]}...")
|
| 130 |
+
except Exception as e:
|
| 131 |
print(f"β Error running agent on question {task_id}: {e}")
|
| 132 |
error_answer = "I apologize, but I encountered an error processing this question."
|
| 133 |
+
answers_payload. append({
|
| 134 |
+
"task_id": task_id,
|
| 135 |
"submitted_answer": error_answer
|
| 136 |
})
|
| 137 |
+
results_log. append((task_id, question_text[:50], f"Error: {e}"))
|
| 138 |
|
| 139 |
print("\n" + "=" * 60)
|
| 140 |
print(f"β
Completed processing all questions!")
|
|
|
|
| 144 |
try:
|
| 145 |
payload = {
|
| 146 |
"username": username,
|
| 147 |
+
"answers": answers_payload,
|
| 148 |
"agent_code": agent_code
|
| 149 |
}
|
| 150 |
|
|
|
|
| 153 |
json=payload,
|
| 154 |
timeout=60
|
| 155 |
)
|
| 156 |
+
submit_response. raise_for_status()
|
| 157 |
+
submission_result = submit_response. json()
|
| 158 |
print(f"β
Submission successful: {submission_result}")
|
| 159 |
+
except requests.exceptions.HTTPError as e:
|
| 160 |
print(f"β HTTP Error submitting answers: {e}")
|
| 161 |
+
print(f"Response text: {submit_response. text[:500]}")
|
| 162 |
return f"Error submitting answers: {e}\n\nResponse: {submit_response.text[:200]}", None
|
| 163 |
+
except Exception as e:
|
| 164 |
print(f"β Error submitting answers: {e}")
|
| 165 |
return f"Error submitting answers: {e}", None
|
| 166 |
|
|
|
|
| 174 |
result_message = f"""
|
| 175 |
## π Submission Successful!
|
| 176 |
|
| 177 |
+
### π Your Results:
|
| 178 |
- **Score:** {score}
|
| 179 |
- **Username:** {username}
|
| 180 |
- **Questions Answered:** {total_questions}
|
|
|
|
| 227 |
output_text = gr. Markdown()
|
| 228 |
|
| 229 |
with gr.Row():
|
| 230 |
+
output_table = gr.Dataframe(label="π Detailed Results", wrap=True)
|
| 231 |
|
| 232 |
submit_button.click(
|
| 233 |
run_and_submit_all,
|