Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,44 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
import
|
| 4 |
from smolagents import (
|
| 5 |
CodeAgent,
|
| 6 |
DuckDuckGoSearchTool,
|
| 7 |
-
|
| 8 |
-
load_tool,
|
| 9 |
-
tool,
|
| 10 |
Tool
|
| 11 |
)
|
| 12 |
from smolagents import VisitWebpageTool
|
| 13 |
-
from Gradio_UI import GradioUI
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
# 1.
|
| 17 |
-
#
|
|
|
|
|
|
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
class FinalAnswerTool(Tool):
|
| 20 |
name = "final_answer"
|
| 21 |
description = "Provides a final answer to the given problem."
|
| 22 |
-
inputs = {
|
| 23 |
-
"answer": {
|
| 24 |
-
"type": "string",
|
| 25 |
-
"description": "The final answer to the problem",
|
| 26 |
-
}
|
| 27 |
-
}
|
| 28 |
output_type = "string"
|
| 29 |
-
|
| 30 |
def forward(self, answer: str) -> str:
|
| 31 |
return answer
|
| 32 |
|
| 33 |
final_answer = FinalAnswerTool()
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
model = InferenceClientModel( # ✅ CORRIGÉ
|
| 40 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 41 |
temperature=0.5,
|
| 42 |
)
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
# 3. Les Outils de mon agents
|
| 46 |
-
# ---------------------------------------------------------
|
| 47 |
-
web_search = DuckDuckGoSearchTool()
|
| 48 |
-
visit_webpage = VisitWebpageTool()
|
| 49 |
-
|
| 50 |
-
# ---------------------------------------------------------
|
| 51 |
-
# 4. L'Agent
|
| 52 |
-
# ---------------------------------------------------------
|
| 53 |
agent = CodeAgent(
|
| 54 |
model=model,
|
| 55 |
tools=[web_search, visit_webpage, final_answer],
|
|
@@ -59,7 +48,80 @@ agent = CodeAgent(
|
|
| 59 |
description="Agent capable of solving complex tasks using web search",
|
| 60 |
)
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
#
|
| 64 |
-
#
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import requests
|
| 4 |
from smolagents import (
|
| 5 |
CodeAgent,
|
| 6 |
DuckDuckGoSearchTool,
|
| 7 |
+
HuggingFaceHubModel,
|
|
|
|
|
|
|
| 8 |
Tool
|
| 9 |
)
|
| 10 |
from smolagents import VisitWebpageTool
|
|
|
|
| 11 |
|
| 12 |
+
# ==============================================================================
|
| 13 |
+
# 1. TES INFORMATIONS (NE TOUCHE À RIEN ICI)
|
| 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 |
+
# Outil de réponse
|
| 22 |
class FinalAnswerTool(Tool):
|
| 23 |
name = "final_answer"
|
| 24 |
description = "Provides a final answer to the given problem."
|
| 25 |
+
inputs = {"answer": {"type": "string", "description": "The final answer"}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
output_type = "string"
|
|
|
|
| 27 |
def forward(self, answer: str) -> str:
|
| 28 |
return answer
|
| 29 |
|
| 30 |
final_answer = FinalAnswerTool()
|
| 31 |
+
web_search = DuckDuckGoSearchTool()
|
| 32 |
+
visit_webpage = VisitWebpageTool()
|
| 33 |
|
| 34 |
+
# Le Modèle (Cerveau)
|
| 35 |
+
# Grâce à la mise à jour requirements, cette classe va enfin marcher !
|
| 36 |
+
model = HuggingFaceHubModel(
|
|
|
|
|
|
|
| 37 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 38 |
temperature=0.5,
|
| 39 |
)
|
| 40 |
|
| 41 |
+
# L'Agent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
agent = CodeAgent(
|
| 43 |
model=model,
|
| 44 |
tools=[web_search, visit_webpage, final_answer],
|
|
|
|
| 48 |
description="Agent capable of solving complex tasks using web search",
|
| 49 |
)
|
| 50 |
|
| 51 |
+
# ==============================================================================
|
| 52 |
+
# 3. LE MOTEUR D'EXAMEN & SOUMISSION
|
| 53 |
+
# ==============================================================================
|
| 54 |
+
API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 55 |
+
|
| 56 |
+
def run_evaluation_and_submit():
|
| 57 |
+
logs = "🚀 DÉBUT DE L'EXAMEN...\n"
|
| 58 |
+
yield logs
|
| 59 |
+
|
| 60 |
+
# 1. Récupération des questions
|
| 61 |
+
try:
|
| 62 |
+
response = requests.get(f"{API_URL}/questions")
|
| 63 |
+
questions = response.json()
|
| 64 |
+
logs += f"📚 {len(questions)} questions chargées.\n\n"
|
| 65 |
+
yield logs
|
| 66 |
+
except Exception as e:
|
| 67 |
+
yield f"❌ Erreur connexion serveur : {e}"
|
| 68 |
+
return
|
| 69 |
+
|
| 70 |
+
answers = []
|
| 71 |
+
|
| 72 |
+
# 2. L'agent résout les questions
|
| 73 |
+
for i, q in enumerate(questions, 1):
|
| 74 |
+
logs += f"🔹 Question {i}/{len(questions)} : {q['question']}\n"
|
| 75 |
+
yield logs
|
| 76 |
+
|
| 77 |
+
try:
|
| 78 |
+
# L'agent travaille
|
| 79 |
+
result = agent.run(q["question"])
|
| 80 |
+
ans_str = str(result).replace("FINAL ANSWER:", "").strip()
|
| 81 |
+
|
| 82 |
+
answers.append({
|
| 83 |
+
"task_id": q["task_id"],
|
| 84 |
+
"submitted_answer": ans_str
|
| 85 |
+
})
|
| 86 |
+
logs += f" 👉 Réponse : {ans_str}\n\n"
|
| 87 |
+
except Exception as e:
|
| 88 |
+
logs += f" ⚠️ Erreur : {e}\n\n"
|
| 89 |
+
answers.append({"task_id": q["task_id"], "submitted_answer": "Error"})
|
| 90 |
+
|
| 91 |
+
yield logs
|
| 92 |
+
|
| 93 |
+
# 3. Envoi des résultats
|
| 94 |
+
logs += "📤 Envoi des notes au juge...\n"
|
| 95 |
+
yield logs
|
| 96 |
+
|
| 97 |
+
try:
|
| 98 |
+
payload = {
|
| 99 |
+
"username": USERNAME,
|
| 100 |
+
"agent_code": SPACE_URL,
|
| 101 |
+
"answers": answers
|
| 102 |
+
}
|
| 103 |
+
response = requests.post(f"{API_URL}/submit", json=payload)
|
| 104 |
+
res_json = response.json()
|
| 105 |
+
|
| 106 |
+
logs += f"\n🎉 RÉSULTAT REÇU : {res_json}\n"
|
| 107 |
+
if 'score' in res_json:
|
| 108 |
+
logs += f"🏆 TON SCORE FINAL : {res_json['score']}"
|
| 109 |
+
except Exception as e:
|
| 110 |
+
logs += f"\n❌ Erreur d'envoi : {e}"
|
| 111 |
+
|
| 112 |
+
yield logs
|
| 113 |
+
|
| 114 |
+
# ==============================================================================
|
| 115 |
+
# 4. INTERFACE
|
| 116 |
+
# ==============================================================================
|
| 117 |
+
with gr.Blocks() as demo:
|
| 118 |
+
gr.Markdown("# 🎓 Certification GAIA Agent")
|
| 119 |
+
gr.Markdown("Clique sur le bouton pour lancer l'examen.")
|
| 120 |
+
|
| 121 |
+
btn = gr.Button("🚀 LANCER L'EXAMEN & SOUMETTRE", variant="primary")
|
| 122 |
+
logs = gr.Textbox(label="Journal de bord", lines=25)
|
| 123 |
+
|
| 124 |
+
btn.click(run_evaluation_and_submit, outputs=logs)
|
| 125 |
+
|
| 126 |
+
if __name__ == "__main__":
|
| 127 |
+
demo.launch()
|