cesarleoni commited on
Commit
fef9e1b
·
verified ·
1 Parent(s): 8ad749f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -78
app.py CHANGED
@@ -18,7 +18,6 @@ openai.api_key = OPENAI_KEY
18
  client = openai.OpenAI(api_key=OPENAI_KEY)
19
 
20
  # --- Tool Definitions ---
21
-
22
  def wiki_lookup(query: str) -> str:
23
  resp = requests.get(
24
  f"https://en.wikipedia.org/api/rest_v1/page/summary/{requests.utils.quote(query)}"
@@ -26,15 +25,6 @@ def wiki_lookup(query: str) -> str:
26
  data = resp.json()
27
  return data.get("extract", "No summary found.")
28
 
29
- class YouTubeTranscriptTool:
30
- def run(self, url: str) -> str:
31
- video_id = url.split('v=')[-1]
32
- resp = requests.get(
33
- "https://youtubetranscriptparser.example/api/transcript",
34
- params={"id": video_id}
35
- )
36
- return resp.json().get("transcript", "")
37
-
38
  class WhisperASRTool:
39
  def run(self, filename: str) -> str:
40
  with open(filename, "rb") as audio_file:
@@ -69,85 +59,100 @@ class EnhancedGaiaAgent:
69
  self.model = model_name
70
  self.tools = {
71
  'wikipedia': wiki_lookup,
72
- 'web_search': lambda q: requests.get(
73
- "https://api.duckduckgo.com/",
74
- params={"q": q, "format": "json", "t": "hf_agent"}
75
- ).json().get('AbstractText',''),
76
- 'python': lambda code: str(exec(code)),
77
- 'youtube_transcript': YouTubeTranscriptTool().run,
78
  'whisper_asr': WhisperASRTool().run,
79
  'chess': ChessAnalysisTool().run,
80
  'excel_summation': ExcelSummationTool().run,
81
  }
82
 
83
  def __call__(self, question: str, file: str = None) -> str:
84
- prompt = [
85
- {"role": "system", "content": (
86
- "You are a multi-tool agent. Use TOOL_NAME(input) to call a tool when needed. "
87
- "Return only the final answer."
88
- )},
89
- {"role": "user", "content": question}
90
- ]
91
- resp = self.client.chat.completions.create(
92
- model=self.model,
93
- messages=prompt,
94
- temperature=0,
95
- max_tokens=512
96
- )
97
- answer = resp.choices[0].message.content.strip()
98
-
99
- if '(' in answer and ')' in answer:
100
- tool, args = answer.split('(',1)
101
- args = args.rsplit(')',1)[0]
102
- func = self.tools.get(tool.strip())
103
- if func:
104
- return func(args)
105
- return answer
106
-
107
- # --- Gradio App Wiring ---
108
- demo = gr.Blocks()
109
- with demo:
110
- gr.Markdown("# GAIA Level 1: Enhanced Multi-Tool Agent")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  gr.Markdown(
112
  """
113
- Set `OPENAIAPIKEY` in secrets.
114
- Upload images/audio/spreadsheets via Gradio’s upload widgets.
115
- Then click **Run** to answer tasks.
 
 
 
116
  """
117
  )
118
  gr.LoginButton()
119
- video_in = gr.Textbox(label="YouTube URL (for transcript)")
120
- audio_in = gr.Audio(label="Audio file (mp3)")
121
- image_in = gr.Image(label="Chess Position (PNG)")
122
- file_in = gr.File(label="Other File (e.g., .xlsx, .py)")
123
- ask_in = gr.JSON(label="Tasks JSON")
124
- run_btn = gr.Button("Run Agent on All Tasks")
125
- status_out = gr.Textbox(label="Status", lines=5, interactive=False)
126
- results_tbl = gr.DataFrame(label="Results")
127
-
128
- def run_all(profile, tasks, y_url, audio, image, file):
129
- if not profile:
130
- return "Please log in.", None
131
- agent = EnhancedGaiaAgent()
132
- results = []
133
- for item in tasks:
134
- q = item['question']
135
- fn = item.get('file_name') or ''
136
- if fn.endswith('.mp3'):
137
- ans = agent.tools['whisper_asr'](audio.name)
138
- elif fn.endswith('.png'):
139
- ans = agent.tools['chess'](image.name)
140
- elif fn.endswith('.xlsx'):
141
- ans = agent.tools['excel_summation'](file.name)
142
- else:
143
- ans = agent(q)
144
- results.append({'task_id': item['task_id'], 'answer': ans})
145
- return "Done", pd.DataFrame(results)
146
-
147
- run_btn.click(
148
- fn=run_all,
149
- inputs=[gr.State(), ask_in, video_in, audio_in, image_in, file_in],
150
- outputs=[status_out, results_tbl]
151
  )
152
 
153
  if __name__ == "__main__":
 
18
  client = openai.OpenAI(api_key=OPENAI_KEY)
19
 
20
  # --- Tool Definitions ---
 
21
  def wiki_lookup(query: str) -> str:
22
  resp = requests.get(
23
  f"https://en.wikipedia.org/api/rest_v1/page/summary/{requests.utils.quote(query)}"
 
25
  data = resp.json()
26
  return data.get("extract", "No summary found.")
27
 
 
 
 
 
 
 
 
 
 
28
  class WhisperASRTool:
29
  def run(self, filename: str) -> str:
30
  with open(filename, "rb") as audio_file:
 
59
  self.model = model_name
60
  self.tools = {
61
  'wikipedia': wiki_lookup,
 
 
 
 
 
 
62
  'whisper_asr': WhisperASRTool().run,
63
  'chess': ChessAnalysisTool().run,
64
  'excel_summation': ExcelSummationTool().run,
65
  }
66
 
67
  def __call__(self, question: str, file: str = None) -> str:
68
+ # Direct questions without file use wiki lookup or LLM
69
+ if file is None:
70
+ # simple LLM call
71
+ messages = [
72
+ {"role": "system", "content": "Answer concisely."},
73
+ {"role": "user", "content": question}
74
+ ]
75
+ resp = self.client.chat.completions.create(
76
+ model=self.model,
77
+ messages=messages,
78
+ temperature=0,
79
+ max_tokens=256
80
+ )
81
+ return resp.choices[0].message.content.strip()
82
+ # File-based tasks
83
+ ext = os.path.splitext(file)[1].lower()
84
+ if ext == '.mp3':
85
+ return self.tools['whisper_asr'](file)
86
+ if ext == '.png':
87
+ return self.tools['chess'](file)
88
+ if ext == '.xlsx':
89
+ return self.tools['excel_summation'](file)
90
+ # fallback
91
+ return ""
92
+
93
+ # --- Run & Submit Function ---
94
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
95
+ if not profile:
96
+ return "Please log in to Hugging Face with the button.", None
97
+
98
+ username = profile.username.strip()
99
+ space_id = os.getenv("SPACE_ID", "unknown-space")
100
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
101
+
102
+ agent = EnhancedGaiaAgent(model_name=os.getenv("OPENAI_MODEL", "gpt-4"))
103
+
104
+ # Fetch questions
105
+ resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
106
+ resp.raise_for_status()
107
+ questions = resp.json()
108
+
109
+ results = []
110
+ answers_payload = []
111
+ for item in questions:
112
+ task_id = item.get("task_id")
113
+ question = item.get("question")
114
+ file_name = item.get("file_name") or None
115
+ # Download file if provided
116
+ local_file = None
117
+ if file_name:
118
+ dl = requests.get(f"{DEFAULT_API_URL}/file/{file_name}")
119
+ open(file_name, 'wb').write(dl.content)
120
+ local_file = file_name
121
+ ans = agent(question, local_file)
122
+ results.append({"Task ID": task_id, "Submitted Answer": ans})
123
+ answers_payload.append({"task_id": task_id, "submitted_answer": ans})
124
+
125
+ # Submit
126
+ submission = {"username": username, "agent_code": agent_code, "answers": answers_payload}
127
+ post = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
128
+ post.raise_for_status()
129
+ res = post.json()
130
+ status = (
131
+ f"Submission Successful! Score: {res.get('score')}%"
132
+ )
133
+ return status, pd.DataFrame(results)
134
+
135
+ # --- Original Gradio UI ---
136
+ with gr.Blocks() as demo:
137
+ gr.Markdown("# Basic Agent Evaluation Runner")
138
  gr.Markdown(
139
  """
140
+ **Instructions:**
141
+
142
+ 1. Clone this space and modify the agent logic.
143
+ 2. Log in to your HF account.
144
+ 3. Click 'Run Evaluation & Submit All Answers'.
145
+
146
  """
147
  )
148
  gr.LoginButton()
149
+ run_button = gr.Button("Run Evaluation & Submit All Answers")
150
+ status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
151
+ results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
152
+
153
+ run_button.click(
154
+ fn=run_and_submit_all,
155
+ outputs=[status_output, results_table]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  )
157
 
158
  if __name__ == "__main__":