Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,17 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
import requests
|
|
|
|
| 4 |
from smolagents import (
|
| 5 |
CodeAgent,
|
| 6 |
DuckDuckGoSearchTool,
|
| 7 |
-
|
| 8 |
Tool
|
| 9 |
)
|
| 10 |
from smolagents import VisitWebpageTool
|
| 11 |
|
| 12 |
-
# ==============================================================================
|
| 13 |
-
# 1. INFOS
|
| 14 |
-
# ==============================================================================
|
| 15 |
USERNAME = "Idealcom"
|
| 16 |
SPACE_URL = "https://huggingface.co/spaces/Idealcom/Final_Assignment_Template"
|
| 17 |
|
| 18 |
-
# ==============================================================================
|
| 19 |
-
# 2. CONFIGURATION DE L'AGENT
|
| 20 |
-
# ==============================================================================
|
| 21 |
class FinalAnswerTool(Tool):
|
| 22 |
name = "final_answer"
|
| 23 |
description = "Provides a final answer to the given problem."
|
|
@@ -27,14 +21,14 @@ class FinalAnswerTool(Tool):
|
|
| 27 |
return answer
|
| 28 |
|
| 29 |
final_answer = FinalAnswerTool()
|
| 30 |
-
|
| 31 |
-
visit_webpage = VisitWebpageTool()
|
| 32 |
-
|
| 33 |
-
model = HuggingFaceHubModel(
|
| 34 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 35 |
temperature=0.5,
|
| 36 |
)
|
| 37 |
|
|
|
|
|
|
|
|
|
|
| 38 |
agent = CodeAgent(
|
| 39 |
model=model,
|
| 40 |
tools=[web_search, visit_webpage, final_answer],
|
|
@@ -44,9 +38,6 @@ agent = CodeAgent(
|
|
| 44 |
description="Agent capable of solving complex tasks using web search",
|
| 45 |
)
|
| 46 |
|
| 47 |
-
# ==============================================================================
|
| 48 |
-
# 3. Evaluation Runner
|
| 49 |
-
# ==============================================================================
|
| 50 |
API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 51 |
|
| 52 |
def run_evaluation_and_submit():
|
|
@@ -54,68 +45,58 @@ def run_evaluation_and_submit():
|
|
| 54 |
yield logs
|
| 55 |
|
| 56 |
try:
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
| 60 |
yield logs
|
| 61 |
except Exception as e:
|
| 62 |
-
yield f"❌ Erreur
|
| 63 |
return
|
| 64 |
|
| 65 |
answers = []
|
| 66 |
|
| 67 |
for i, q in enumerate(questions, 1):
|
| 68 |
-
logs += f"
|
| 69 |
yield logs
|
| 70 |
|
| 71 |
try:
|
| 72 |
-
# L'agent travaille
|
| 73 |
result = agent.run(q["question"])
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
"task_id": q["task_id"],
|
| 78 |
-
"submitted_answer": ans_str
|
| 79 |
-
})
|
| 80 |
-
logs += f" 👉 Réponse : {ans_str}\n\n"
|
| 81 |
except Exception as e:
|
| 82 |
-
logs += f"
|
| 83 |
answers.append({"task_id": q["task_id"], "submitted_answer": "Error"})
|
| 84 |
|
| 85 |
yield logs
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
logs += "📤 Envoi des résultats...\n"
|
| 89 |
yield logs
|
| 90 |
|
| 91 |
try:
|
| 92 |
-
|
| 93 |
-
"
|
| 94 |
-
"agent_code": SPACE_URL,
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
logs +=
|
| 103 |
except Exception as e:
|
| 104 |
-
logs += f"\n❌ Erreur
|
| 105 |
|
| 106 |
yield logs
|
| 107 |
|
| 108 |
-
# ==============================================================================
|
| 109 |
-
# 4. L'INTERFACE
|
| 110 |
-
# ==============================================================================
|
| 111 |
with gr.Blocks() as demo:
|
| 112 |
-
gr.Markdown("# 🎓
|
| 113 |
-
gr.
|
| 114 |
-
|
| 115 |
-
btn = gr.Button("🚀 LANCER L'EXAMEN & SOUMETTRE", variant="primary")
|
| 116 |
-
logs = gr.Textbox(label="Logs", lines=25)
|
| 117 |
-
|
| 118 |
btn.click(run_evaluation_and_submit, outputs=logs)
|
| 119 |
|
| 120 |
if __name__ == "__main__":
|
| 121 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import requests
|
| 3 |
+
import time
|
| 4 |
from smolagents import (
|
| 5 |
CodeAgent,
|
| 6 |
DuckDuckGoSearchTool,
|
| 7 |
+
InferenceClientModel, # ✅ BON (pas HuggingFaceHubModel)
|
| 8 |
Tool
|
| 9 |
)
|
| 10 |
from smolagents import VisitWebpageTool
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
USERNAME = "Idealcom"
|
| 13 |
SPACE_URL = "https://huggingface.co/spaces/Idealcom/Final_Assignment_Template"
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
class FinalAnswerTool(Tool):
|
| 16 |
name = "final_answer"
|
| 17 |
description = "Provides a final answer to the given problem."
|
|
|
|
| 21 |
return answer
|
| 22 |
|
| 23 |
final_answer = FinalAnswerTool()
|
| 24 |
+
model = InferenceClientModel( # ✅ CORRECT
|
|
|
|
|
|
|
|
|
|
| 25 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 26 |
temperature=0.5,
|
| 27 |
)
|
| 28 |
|
| 29 |
+
web_search = DuckDuckGoSearchTool()
|
| 30 |
+
visit_webpage = VisitWebpageTool()
|
| 31 |
+
|
| 32 |
agent = CodeAgent(
|
| 33 |
model=model,
|
| 34 |
tools=[web_search, visit_webpage, final_answer],
|
|
|
|
| 38 |
description="Agent capable of solving complex tasks using web search",
|
| 39 |
)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
| 41 |
API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 42 |
|
| 43 |
def run_evaluation_and_submit():
|
|
|
|
| 45 |
yield logs
|
| 46 |
|
| 47 |
try:
|
| 48 |
+
logs += "📚 Récupération des questions...\n"
|
| 49 |
+
yield logs
|
| 50 |
+
questions = requests.get(f"{API_URL}/questions").json()
|
| 51 |
+
logs += f"✅ {len(questions)} questions reçues.\n\n"
|
| 52 |
yield logs
|
| 53 |
except Exception as e:
|
| 54 |
+
yield f"❌ Erreur : {e}"
|
| 55 |
return
|
| 56 |
|
| 57 |
answers = []
|
| 58 |
|
| 59 |
for i, q in enumerate(questions, 1):
|
| 60 |
+
logs += f"[{i}/{len(questions)}] {q['question'][:60]}...\n"
|
| 61 |
yield logs
|
| 62 |
|
| 63 |
try:
|
|
|
|
| 64 |
result = agent.run(q["question"])
|
| 65 |
+
ans = str(result).replace("FINAL ANSWER:", "").strip()
|
| 66 |
+
answers.append({"task_id": q["task_id"], "submitted_answer": ans})
|
| 67 |
+
logs += f"✅ {ans}\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
except Exception as e:
|
| 69 |
+
logs += f"⚠️ Erreur: {e}\n\n"
|
| 70 |
answers.append({"task_id": q["task_id"], "submitted_answer": "Error"})
|
| 71 |
|
| 72 |
yield logs
|
| 73 |
+
time.sleep(0.2)
|
| 74 |
|
| 75 |
+
logs += "📤 Envoi au serveur...\n"
|
|
|
|
| 76 |
yield logs
|
| 77 |
|
| 78 |
try:
|
| 79 |
+
res = requests.post(
|
| 80 |
+
f"{API_URL}/submit",
|
| 81 |
+
json={"username": USERNAME, "agent_code": SPACE_URL, "answers": answers}
|
| 82 |
+
).json()
|
| 83 |
+
|
| 84 |
+
logs += f"\n🎉 SCORE: {res.get('score', '?')}/{res.get('total', 20)}\n"
|
| 85 |
+
logs += f"📊 {res.get('percentage', '?')}%\n"
|
| 86 |
|
| 87 |
+
if res.get('percentage', 0) >= 30:
|
| 88 |
+
logs += "\n✅ BRAVO! ≥ 30%!\n"
|
| 89 |
+
logs += "Certificat: https://huggingface.co/spaces/agents-course/Unit4-Final-Certificate"
|
| 90 |
except Exception as e:
|
| 91 |
+
logs += f"\n❌ Erreur envoi: {e}"
|
| 92 |
|
| 93 |
yield logs
|
| 94 |
|
|
|
|
|
|
|
|
|
|
| 95 |
with gr.Blocks() as demo:
|
| 96 |
+
gr.Markdown("# 🎓 GAIA Agent - Examen Final")
|
| 97 |
+
btn = gr.Button("🚀 LANCER L'EXAMEN", variant="primary", size="lg")
|
| 98 |
+
logs = gr.Textbox(label="Logs", lines=30, interactive=False)
|
|
|
|
|
|
|
|
|
|
| 99 |
btn.click(run_evaluation_and_submit, outputs=logs)
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
+
demo.launch()
|