Lulube commited on
Commit
39aa3af
·
verified ·
1 Parent(s): 1ef732d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -29
app.py CHANGED
@@ -11,6 +11,8 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
  # --- Basic Agent Definition ---
13
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
 
14
  class BasicAgent():
15
  def __init__(self):
16
  print("MyCustomAgent with SmolaAgent initialized.")
@@ -24,46 +26,65 @@ class BasicAgent():
24
  )
25
 
26
  # Outils disponibles
27
- search_tool = DuckDuckGoSearchTool()
28
- webpage_tool = VisitWebpageTool()
 
 
 
 
 
29
 
30
  # Créer l'agent Alfred avec les outils
31
  self.alfred = CodeAgent(
32
- tools=[search_tool, webpage_tool],
33
- model=self.model,
34
- additional_authorized_imports=["pandas", "numpy"],
35
- max_steps = 15
36
  )
37
 
38
- print("Alfred agent ready with search, webpage, and Python tools!")
39
 
40
  def __call__(self, question: str) -> str:
41
- """
42
- Permet d'appeler l'agent directement avec une question
43
- """
44
  try:
45
- # Ajouter des instructions dans la question pour améliorer les réponses
46
- enhanced_question = f"""
47
- {question}
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- Instructions:
50
- - If this involves data analysis, use Python code to calculate results
51
- - If data files are mentioned try access it
52
- - Search for additional context if needed
53
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
- result = self.alfred.run(enhanced_question)
56
- return str(result)
57
  except Exception as e:
58
- if "content filtering" in str(e).lower() or "content policy" in str(e).lower():
59
- # Essayer avec une reformulation plus neutre
60
- neutral_question = f"Please analyze and provide information about: {question}"
61
- try:
62
- result = self.alfred.run(neutral_question)
63
- return str(result)
64
- except:
65
- return f"Unable to process this question due to content restrictions. Please rephrase."
66
- return f"Erreur lors du traitement de la question: {str(e)}"
67
 
68
  def run_and_submit_all( profile: gr.OAuthProfile | None):
69
  """
 
11
 
12
  # --- Basic Agent Definition ---
13
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
14
+ from smolagents import CodeAgent, LiteLLMModel, DuckDuckGoSearchTool, VisitWebpageTool, PythonCodeTool, SpeechToTextTool, ImageCaptioningTool
15
+
16
  class BasicAgent():
17
  def __init__(self):
18
  print("MyCustomAgent with SmolaAgent initialized.")
 
26
  )
27
 
28
  # Outils disponibles
29
+ tools = [
30
+ DuckDuckGoSearchTool(), # Recherche web (Wikipedia)
31
+ VisitWebpageTool(), # Visite de pages web
32
+ PythonCodeTool(), # Calculs, Excel, traitement de données
33
+ SpeechToTextTool(), # Speech to text
34
+ ImageCaptioningTool(), # Reconnaissance d'image
35
+ ]
36
 
37
  # Créer l'agent Alfred avec les outils
38
  self.alfred = CodeAgent(
39
+ tools=tools,
40
+ model=self.model
 
 
41
  )
42
 
43
+ print("Alfred agent ready with tools!")
44
 
45
  def __call__(self, question: str) -> str:
46
+ print(f"Alfred received question (first 50 chars): {question[:50]}...")
47
+
 
48
  try:
49
+ prompt = f"""
50
+ You are Alfred, an intelligent assistant with access to multiple tools.
51
+
52
+ To answer questions:
53
+ 1. ANALYZE the question to identify the type of task.
54
+ 2. USE the appropriate tool:
55
+ - For mathematical calculations: use PythonCodeTool
56
+ - To read Excel/CSV files: use PythonCodeTool with pandas
57
+ - For information retrieval: use DuckDuckGoSearchTool, then VisitWebpageTool if needed
58
+ - To analyze images: use ImageCaptioningTool
59
+ - For speech-to-text: use SpeechToTextTool
60
+ - For data processing: use PythonCodeTool
61
+ - RETURN only the final answer, clearly and concisely.
62
+
63
+ If you cannot answer, simply say: "I cannot answer this question."
64
 
65
+ Example answers:
66
+ - For a calculation: "42"
67
+ - For a date: "March 15, 2023"
68
+ - For a name: "Albert Einstein"
69
+ - For a number: "1,234,567"
70
+ - For a list: "b, e"
71
+
72
+ Here is the question to process: {question}"""
73
+
74
+ # Utiliser Alfred pour traiter la question
75
+ response = self.alfred.run(prompt)
76
+
77
+ # IMPORTANT: Extraire seulement la réponse finale
78
+ # Pas de "FINAL ANSWER" ou formatage supplémentaire
79
+ final_answer = self.extract_final_answer(response)
80
+
81
+ print(f"Alfred returning answer: {final_answer}")
82
+ return final_answer
83
 
 
 
84
  except Exception as e:
85
+ print(f"Erreur lors du traitement: {e}")
86
+ return "Je ne peux pas répondre à cette question."
87
+
 
 
 
 
 
 
88
 
89
  def run_and_submit_all( profile: gr.OAuthProfile | None):
90
  """