Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,8 +8,10 @@ import pandas as pd
|
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
|
|
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
| 13 |
from smolagents import CodeAgent, LiteLLMModel, DuckDuckGoSearchTool, VisitWebpageTool, PythonInterpreterTool, SpeechToTextTool #, ImageCaptioningTool
|
| 14 |
|
| 15 |
class BasicAgent():
|
|
@@ -18,7 +20,7 @@ class BasicAgent():
|
|
| 18 |
|
| 19 |
# Configuration du modèle Azure GPT-4o-mini
|
| 20 |
self.model = LiteLLMModel(
|
| 21 |
-
model_id="azure/gpt-4o
|
| 22 |
api_key=os.getenv("api_key_4o"),
|
| 23 |
api_base=os.getenv("base_url_4o"),
|
| 24 |
api_version="2025-01-01-preview"
|
|
@@ -28,7 +30,7 @@ class BasicAgent():
|
|
| 28 |
tools = [
|
| 29 |
DuckDuckGoSearchTool(), # Recherche web (Wikipedia)
|
| 30 |
VisitWebpageTool(), # Visite de pages web
|
| 31 |
-
PythonInterpreterTool(),
|
| 32 |
SpeechToTextTool(), # Speech to text
|
| 33 |
#ImageCaptioningTool(), # Reconnaissance d'image
|
| 34 |
]
|
|
@@ -41,13 +43,27 @@ class BasicAgent():
|
|
| 41 |
|
| 42 |
print("Alfred agent ready with tools!")
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
def __call__(self, question: str) -> str:
|
| 45 |
print(f"Alfred received question (first 50 chars): {question[:50]}...")
|
| 46 |
|
| 47 |
try:
|
| 48 |
prompt = f"""
|
| 49 |
You are Alfred, an intelligent assistant with access to multiple tools.
|
| 50 |
-
|
| 51 |
To answer questions:
|
| 52 |
1. ANALYZE the question to identify the type of task.
|
| 53 |
2. USE the appropriate tool:
|
|
@@ -82,9 +98,10 @@ class BasicAgent():
|
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
print(f"Erreur lors du traitement: {e}")
|
| 85 |
-
return
|
| 86 |
|
| 87 |
|
|
|
|
| 88 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 89 |
"""
|
| 90 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
+
|
| 12 |
# --- Basic Agent Definition ---
|
| 13 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 14 |
+
import os
|
| 15 |
from smolagents import CodeAgent, LiteLLMModel, DuckDuckGoSearchTool, VisitWebpageTool, PythonInterpreterTool, SpeechToTextTool #, ImageCaptioningTool
|
| 16 |
|
| 17 |
class BasicAgent():
|
|
|
|
| 20 |
|
| 21 |
# Configuration du modèle Azure GPT-4o-mini
|
| 22 |
self.model = LiteLLMModel(
|
| 23 |
+
model_id="azure/gpt-4o",
|
| 24 |
api_key=os.getenv("api_key_4o"),
|
| 25 |
api_base=os.getenv("base_url_4o"),
|
| 26 |
api_version="2025-01-01-preview"
|
|
|
|
| 30 |
tools = [
|
| 31 |
DuckDuckGoSearchTool(), # Recherche web (Wikipedia)
|
| 32 |
VisitWebpageTool(), # Visite de pages web
|
| 33 |
+
PythonInterpreterTool(), # Calculs, Excel, traitement de données
|
| 34 |
SpeechToTextTool(), # Speech to text
|
| 35 |
#ImageCaptioningTool(), # Reconnaissance d'image
|
| 36 |
]
|
|
|
|
| 43 |
|
| 44 |
print("Alfred agent ready with tools!")
|
| 45 |
|
| 46 |
+
def extract_final_answer(self, response):
|
| 47 |
+
"""Extrait la réponse finale du response de l'agent"""
|
| 48 |
+
if isinstance(response, str):
|
| 49 |
+
# Si c'est déjà une string, la retourner directement
|
| 50 |
+
return response.strip()
|
| 51 |
+
|
| 52 |
+
# Si c'est un objet avec des attributs, essayer d'extraire le contenu
|
| 53 |
+
if hasattr(response, 'content'):
|
| 54 |
+
return str(response.content).strip()
|
| 55 |
+
elif hasattr(response, 'text'):
|
| 56 |
+
return str(response.text).strip()
|
| 57 |
+
else:
|
| 58 |
+
# Convertir en string par défaut
|
| 59 |
+
return str(response).strip()
|
| 60 |
+
|
| 61 |
def __call__(self, question: str) -> str:
|
| 62 |
print(f"Alfred received question (first 50 chars): {question[:50]}...")
|
| 63 |
|
| 64 |
try:
|
| 65 |
prompt = f"""
|
| 66 |
You are Alfred, an intelligent assistant with access to multiple tools.
|
|
|
|
| 67 |
To answer questions:
|
| 68 |
1. ANALYZE the question to identify the type of task.
|
| 69 |
2. USE the appropriate tool:
|
|
|
|
| 98 |
|
| 99 |
except Exception as e:
|
| 100 |
print(f"Erreur lors du traitement: {e}")
|
| 101 |
+
return "Je ne peux pas répondre à cette question."
|
| 102 |
|
| 103 |
|
| 104 |
+
|
| 105 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 106 |
"""
|
| 107 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|