dlaima commited on
Commit
ce74e50
·
verified ·
1 Parent(s): 36b55d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -157
app.py CHANGED
@@ -1,188 +1,113 @@
1
  import os
2
  import gradio as gr
3
  import requests
4
- import pandas as pd
5
- from typing import List, Dict
6
 
7
- from smolagents import CodeAgent, DuckDuckGoSearchTool, Tool
8
-
9
- from wikipedia_searcher import WikipediaSearcher
10
  from audio_transcriber import AudioTranscriptionTool
11
  from image_analyzer import ImageAnalysisTool
 
12
 
 
 
 
13
 
14
- class WikipediaSearchTool(Tool):
15
- name = "wikipedia_search"
16
- description = "Search Wikipedia for a given query."
17
- inputs = {
18
- "query": {
19
- "type": "string",
20
- "description": "The search query string"
21
- }
22
- }
23
- output_type = "string"
24
-
25
- def __init__(self):
26
- super().__init__()
27
- self.searcher = WikipediaSearcher()
28
-
29
- def forward(self, query: str) -> str:
30
- return self.searcher.search(query)
31
-
32
-
33
- # Hugging Face Inference API wrapper for chat completion
34
- class HFChatModel:
35
- def __init__(self, model_id: str):
36
- self.model_id = model_id
37
- self.api_url = f"https://api-inference.huggingface.co/models/{model_id}"
38
- self.headers = {"Authorization": f"Bearer {os.getenv('HF_API_TOKEN')}"}
39
- self.system_prompt = """
40
- You are an agent solving the GAIA benchmark and you are required to provide exact answers.
41
  Rules to follow:
42
  1. Return only the exact requested answer: no explanation and no reasoning.
43
  2. For yes/no questions, return exactly "Yes" or "No".
44
  3. For dates, use the exact format requested.
45
  4. For numbers, use the exact number, no other format.
46
  5. For names, use the exact name as found in sources.
47
- 6. If the question has an associated file, download the file first using the task ID.
48
  Examples of good responses:
49
  - "42"
50
  - "Yes"
51
  - "October 5, 2001"
52
  - "Buenos Aires"
53
  Never include phrases like "the answer is..." or "Based on my research".
54
- Only return the exact answer.
55
- """
56
-
57
- def generate(self, messages: List[Dict[str, str]]) -> str:
58
- # Prepend system prompt as first message
59
- all_messages = [{"role": "system", "content": self.system_prompt}] + messages
60
-
61
- payload = {
62
- "inputs": {
63
- "past_user_inputs": [],
64
- "generated_responses": [],
65
- "text": "\n".join(m["content"] for m in all_messages if m["role"] != "system")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  }
67
- }
68
-
69
- # Some HF chat models expect just a string prompt; adjust accordingly per your model's requirements
70
-
71
- response = requests.post(self.api_url, headers=self.headers, json=payload)
72
- if response.status_code == 200:
73
- output = response.json()
74
- # Output format depends on model; adjust as needed
75
- if isinstance(output, list) and len(output) > 0 and "generated_text" in output[0]:
76
- return output[0]["generated_text"].strip()
77
- elif isinstance(output, dict) and "generated_text" in output:
78
- return output["generated_text"].strip()
79
- else:
80
- # fallback to raw text
81
- return str(output).strip()
82
- else:
83
- raise RuntimeError(f"Hugging Face API error {response.status_code}: {response.text}")
84
-
85
-
86
- class MyAgent:
87
- def __init__(self):
88
- self.model = HFChatModel(model_id="gpt-4o-mini") # Or any HF chat model you want
89
-
90
- self.agent = CodeAgent(
91
- tools=[
92
- DuckDuckGoSearchTool(),
93
- WikipediaSearchTool(),
94
- AudioTranscriptionTool(),
95
- ImageAnalysisTool(),
96
- ],
97
- model=self, # We'll route calls via __call__ below
98
  )
99
-
100
- def __call__(self, prompt: str) -> str:
101
- # Construct chat message for HF model
102
- messages = [{"role": "user", "content": prompt}]
103
- return self.model.generate(messages)
104
-
105
-
106
- def run_and_submit_all(profile: gr.OAuthProfile | None):
107
- space_id = os.getenv("SPACE_ID")
108
-
109
- if profile:
110
- username = profile.username
111
- else:
112
- return "Please Login to Hugging Face with the button.", None
113
-
114
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
115
- questions_url = f"{DEFAULT_API_URL}/questions"
116
- submit_url = f"{DEFAULT_API_URL}/submit"
117
-
118
- try:
119
- agent = MyAgent()
120
  except Exception as e:
121
- return f"Error initializing agent: {e}", None
122
-
123
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
125
- try:
126
- response = requests.get(questions_url, timeout=15)
127
- response.raise_for_status()
128
- questions_data = response.json()
129
- except Exception as e:
130
- return f"Error fetching questions: {e}", None
131
-
132
- results_log = []
133
- answers_payload = []
134
-
135
- for item in questions_data:
136
- task_id = item.get("task_id")
137
- if not task_id:
138
- continue
139
- try:
140
- answer = agent(item.get("question", ""))
141
- answers_payload.append({"task_id": task_id, "submitted_answer": answer})
142
- results_log.append({
143
- "Task ID": task_id,
144
- "Question": item.get("question", ""),
145
- "Submitted Answer": answer
146
- })
147
- except Exception as e:
148
- results_log.append({
149
- "Task ID": task_id,
150
- "Question": item.get("question", ""),
151
- "Submitted Answer": f"Error: {e}"
152
- })
153
-
154
- submission_data = {
155
- "username": username.strip(),
156
- "agent_code": agent_code,
157
- "answers": answers_payload
158
- }
159
 
160
- try:
161
- response = requests.post(submit_url, json=submission_data, timeout=60)
162
- response.raise_for_status()
163
- result_data = response.json()
164
- final_status = (
165
- f"Submission Successful!\n"
166
- f"User: {result_data.get('username')}\n"
167
- f"Overall Score: {result_data.get('score', 'N/A')}% "
168
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
169
- f"Message: {result_data.get('message', 'No message received.')}"
170
- )
171
- return final_status, pd.DataFrame(results_log)
172
- except Exception as e:
173
- return f"Submission failed: {e}", pd.DataFrame(results_log)
174
 
 
 
 
175
 
176
- with gr.Blocks() as demo:
177
- gr.Markdown("# Basic Agent Evaluation Runner (HF API)")
178
- gr.LoginButton()
179
- run_btn = gr.Button("Run Evaluation & Submit All Answers")
180
- status_out = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
181
- results_df = gr.DataFrame(label="Questions and Agent Answers")
182
 
183
- run_btn.click(fn=run_and_submit_all, outputs=[status_out, results_df])
184
 
185
  if __name__ == "__main__":
186
- demo.launch(debug=True, share=False)
187
-
188
 
 
1
  import os
2
  import gradio as gr
3
  import requests
4
+ from smolagents import Agent, Tool
 
5
 
 
 
 
6
  from audio_transcriber import AudioTranscriptionTool
7
  from image_analyzer import ImageAnalysisTool
8
+ from wikipedia_searcher import WikipediaSearcher
9
 
10
+ # Hugging Face API setup
11
+ HF_API_TOKEN = os.getenv("HF_API_TOKEN")
12
+ HF_CHAT_MODEL_URL = "https://api-inference.huggingface.com/models/HuggingFaceH4/zephyr-7b-beta"
13
 
14
+ HEADERS = {
15
+ "Authorization": f"Bearer {HF_API_TOKEN}",
16
+ "Content-Type": "application/json"
17
+ }
18
+
19
+ # Static system prompt
20
+ SYSTEM_PROMPT = """You are an agent solving the GAIA benchmark and you are required to provide exact answers.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  Rules to follow:
22
  1. Return only the exact requested answer: no explanation and no reasoning.
23
  2. For yes/no questions, return exactly "Yes" or "No".
24
  3. For dates, use the exact format requested.
25
  4. For numbers, use the exact number, no other format.
26
  5. For names, use the exact name as found in sources.
27
+ 6. If the question has an associated file, process it accordingly.
28
  Examples of good responses:
29
  - "42"
30
  - "Yes"
31
  - "October 5, 2001"
32
  - "Buenos Aires"
33
  Never include phrases like "the answer is..." or "Based on my research".
34
+ Only return the exact answer."""
35
+
36
+ # Agent tools
37
+ audio_tool = AudioTranscriptionTool()
38
+ image_tool = ImageAnalysisTool()
39
+ wiki_tool = Tool.from_function(
40
+ name="wikipedia_search",
41
+ description="Search for facts using Wikipedia.",
42
+ input_schema={"query": {"type": "string", "description": "Search query"}},
43
+ output_type="string",
44
+ forward=lambda query: WikipediaSearcher().search(query)
45
+ )
46
+
47
+ tools = [audio_tool, image_tool, wiki_tool]
48
+
49
+ agent = Agent(
50
+ tools=tools,
51
+ system_prompt=SYSTEM_PROMPT
52
+ )
53
+
54
+ def query_hf_model(prompt: str) -> str:
55
+ try:
56
+ response = requests.post(
57
+ HF_CHAT_MODEL_URL,
58
+ headers=HEADERS,
59
+ json={
60
+ "inputs": {
61
+ "past_user_inputs": [],
62
+ "text": prompt
63
+ },
64
+ "parameters": {
65
+ "max_new_tokens": 256,
66
+ "return_full_text": False
67
+ }
68
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  )
70
+ result = response.json()
71
+ if isinstance(result, dict) and "error" in result:
72
+ return f"HF API Error: {result['error']}"
73
+ return result[0]["generated_text"].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  except Exception as e:
75
+ return f"Error querying Hugging Face model: {e}"
76
+
77
+ def run_and_submit_all(question, file):
78
+ if file:
79
+ file_path = file.name
80
+ if file_path.endswith((".mp3", ".wav")):
81
+ transcript = audio_tool.forward(file_path)
82
+ question = f"{question}\n\nTranscription of audio: {transcript}"
83
+ elif file_path.endswith((".png", ".jpg", ".jpeg")):
84
+ image_answer = image_tool.forward(file_path, question)
85
+ return image_answer
86
+ elif file_path.endswith(".py"):
87
+ try:
88
+ with open(file_path, "r") as f:
89
+ code = f.read()
90
+ question = f"{question}\n\nPython code:\n{code}"
91
+ except Exception as e:
92
+ return f"Error reading code file: {e}"
93
+ else:
94
+ return "Unsupported file type."
95
 
96
+ full_prompt = f"{SYSTEM_PROMPT}\nQUESTION:\n{question}"
97
+ return query_hf_model(full_prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
+ with gr.Blocks(title="GAIA Agent with HF API") as demo:
100
+ gr.Markdown("### GAIA Evaluation Agent (Hugging Face-based)")
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
+ with gr.Row():
103
+ question_input = gr.Textbox(label="Question", placeholder="Enter your question here...", lines=3)
104
+ file_input = gr.File(label="Optional File (Audio, Image, or Python)", file_types=[".mp3", ".wav", ".jpg", ".jpeg", ".png", ".py"])
105
 
106
+ submit_button = gr.Button("Run Agent")
107
+ output_box = gr.Textbox(label="Answer")
 
 
 
 
108
 
109
+ submit_button.click(fn=run_and_submit_all, inputs=[question_input, file_input], outputs=output_box)
110
 
111
  if __name__ == "__main__":
112
+ demo.launch()
 
113