dlaima commited on
Commit
c64bf2e
·
verified ·
1 Parent(s): 03f0224

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -53
app.py CHANGED
@@ -1,22 +1,16 @@
1
  import os
2
  import requests
3
- from smolagents import Agent
 
 
 
 
4
  from audio_transcriber import AudioTranscriptionTool
5
  from image_analyzer import ImageAnalysisTool
6
  from wikipedia_searcher import WikipediaSearcher
7
- from openai import OpenAI
8
- from dotenv import load_dotenv
9
-
10
- load_dotenv()
11
-
12
- # Tools
13
- audio_tool = AudioTranscriptionTool()
14
- image_tool = ImageAnalysisTool()
15
- wiki_tool = WikipediaSearcher()
16
 
17
- # Static system prompt
18
- def build_prompt(question: str) -> str:
19
- return f"""You are an agent solving the GAIA benchmark and you are required to provide exact answers.
20
  Rules to follow:
21
  1. Return only the exact requested answer: no explanation and no reasoning.
22
  2. For yes/no questions, return exactly "Yes" or "No".
@@ -30,52 +24,45 @@ Examples of good responses:
30
  - "October 5, 2001"
31
  - "Buenos Aires"
32
  Never include phrases like "the answer is..." or "Based on my research".
33
- Only return the exact answer.
34
 
35
- QUESTION:
36
- {question}
37
- """
 
 
 
38
 
39
- # Main agent function
40
- class GAIAAgent:
41
  def __init__(self):
42
- self.llm = OpenAI(model="gpt-4-turbo", temperature=0)
 
 
 
 
 
 
 
 
 
43
 
44
  def __call__(self, task: dict) -> str:
45
  question = task.get("question", "")
46
- attachment_url = task.get("attachment", "")
47
-
48
- # Handle audio
49
- if attachment_url.endswith((".mp3", ".wav")):
50
- transcript = audio_tool.forward(attachment_url)
51
- question += f"\n\nTranscript of attached audio:\n{transcript}"
52
-
53
- # Handle image
54
- elif attachment_url.endswith((".jpg", ".jpeg", ".png")):
55
- return image_tool.forward(attachment_url, question)
56
-
57
- # Handle Python file
58
- elif attachment_url.endswith(".py"):
59
- try:
60
- code_text = requests.get(attachment_url).text
61
- question += f"\n\nAttached Python file content:\n{code_text}"
62
- except Exception as e:
63
- return f"Error retrieving Python file: {e}"
64
-
65
- # Wikipedia queries (if task type or instruction indicates)
66
- if "wikipedia" in question.lower():
67
- return wiki_tool.search(question)
68
-
69
- # Build prompt
70
- prompt = build_prompt(question)
71
-
72
- # Run model
73
- response = self.llm.chat.completions.create(
74
- messages=[{"role": "system", "content": prompt}],
75
- model="gpt-4-turbo"
76
- )
77
-
78
- return response.choices[0].message.content.strip()
79
 
80
 
81
  def run_and_submit_all(profile: gr.OAuthProfile | None):
 
1
  import os
2
  import requests
3
+ import gradio as gr
4
+ import pandas as pd
5
+ from smolagents import CodeAgent, DuckDuckGoSearchTool
6
+ from smolagents.models import OpenAIServerModel
7
+
8
  from audio_transcriber import AudioTranscriptionTool
9
  from image_analyzer import ImageAnalysisTool
10
  from wikipedia_searcher import WikipediaSearcher
 
 
 
 
 
 
 
 
 
11
 
12
+ # Static system prompt for all completions
13
+ SYSTEM_PROMPT = """You are an agent solving the GAIA benchmark and you are required to provide exact answers.
 
14
  Rules to follow:
15
  1. Return only the exact requested answer: no explanation and no reasoning.
16
  2. For yes/no questions, return exactly "Yes" or "No".
 
24
  - "October 5, 2001"
25
  - "Buenos Aires"
26
  Never include phrases like "the answer is..." or "Based on my research".
27
+ Only return the exact answer."""
28
 
29
+ class PatchedOpenAIServerModel(OpenAIServerModel):
30
+ def generate(self, messages, stop_sequences=None, **kwargs):
31
+ if isinstance(messages, list):
32
+ if not any(m["role"] == "system" for m in messages):
33
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}] + messages
34
+ return super().generate(messages=messages, stop_sequences=stop_sequences, **kwargs)
35
 
36
+ class MyAgent:
 
37
  def __init__(self):
38
+ self.model = PatchedOpenAIServerModel(model_id="gpt-4-turbo")
39
+ self.agent = CodeAgent(
40
+ tools=[
41
+ DuckDuckGoSearchTool(),
42
+ WikipediaSearcher(),
43
+ AudioTranscriptionTool(),
44
+ ImageAnalysisTool()
45
+ ],
46
+ model=self.model
47
+ )
48
 
49
  def __call__(self, task: dict) -> str:
50
  question = task.get("question", "")
51
+ attachment = task.get("attachment")
52
+
53
+ if attachment:
54
+ if attachment.endswith((".mp3", ".wav")):
55
+ question += f"\n\nAudio file: {attachment}"
56
+ elif attachment.endswith((".jpg", ".jpeg", ".png")):
57
+ question += f"\n\nImage file: {attachment}"
58
+ elif attachment.endswith(".py"):
59
+ try:
60
+ content = requests.get(attachment).text
61
+ question += f"\n\nPython code:\n{content}"
62
+ except Exception as e:
63
+ question += f"\n\nError loading code: {e}"
64
+
65
+ return self.agent.run(question)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
 
68
  def run_and_submit_all(profile: gr.OAuthProfile | None):