Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
|
| 7 |
-
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
|
@@ -12,12 +12,51 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
@@ -163,7 +202,7 @@ with gr.Blocks() as demo:
|
|
| 163 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 164 |
|
| 165 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 166 |
-
|
| 167 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 168 |
|
| 169 |
run_button.click(
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
+
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
| 7 |
|
|
|
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
|
|
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
+
print("Inizializzazione del VERO Agente AI...")
|
| 16 |
+
|
| 17 |
+
# 1. Definisci il modello (usiamo Qwen su HF)
|
| 18 |
+
self.model = HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 19 |
+
|
| 20 |
+
# 2. Fornisci all'agente gli strumenti per cercare su internet
|
| 21 |
+
self.tools = [DuckDuckGoSearchTool()]
|
| 22 |
+
|
| 23 |
+
# 3. Crea l'agente
|
| 24 |
+
self.agent = CodeAgent(
|
| 25 |
+
tools=self.tools,
|
| 26 |
+
model=self.model,
|
| 27 |
+
max_steps=10,
|
| 28 |
+
additional_authorized_imports=["requests", "bs4", "json", "time"]
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# 4. IL TRUCCO PER L'EXACT MATCH: Istruzioni severissime
|
| 32 |
+
self.prompt_template = """
|
| 33 |
+
You must answer the following question.
|
| 34 |
+
|
| 35 |
+
CRITICAL RULE (EXACT MATCH):
|
| 36 |
+
You must output ONLY the exact requested answer.
|
| 37 |
+
Absolutely NO introductory phrases, no "The answer is...", no "FINAL ANSWER:".
|
| 38 |
+
Just return the raw data, word, or number.
|
| 39 |
+
|
| 40 |
+
Question to solve: {question}
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
def __call__(self, question: str) -> str:
|
| 44 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 45 |
+
try:
|
| 46 |
+
# Combina la domanda con le nostre istruzioni severe
|
| 47 |
+
formatted_prompt = self.prompt_template.format(question=question)
|
| 48 |
+
|
| 49 |
+
# Fai ragionare e agire il tuo agente!
|
| 50 |
+
answer = self.agent.run(formatted_prompt)
|
| 51 |
+
|
| 52 |
+
# Pulisce la stringa da eventuali spazi extra
|
| 53 |
+
final_answer = str(answer).strip()
|
| 54 |
+
print(f"L'agente ha trovato la risposta: {final_answer}")
|
| 55 |
+
return final_answer
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"Errore durante l'esecuzione dell'agente: {e}")
|
| 58 |
+
return "Error"
|
| 59 |
+
|
| 60 |
|
| 61 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 62 |
"""
|
|
|
|
| 202 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 203 |
|
| 204 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 205 |
+
|
| 206 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 207 |
|
| 208 |
run_button.click(
|