BenjaminKaindu0506 commited on
Commit
918a8b2
·
verified ·
1 Parent(s): dcedcc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -41
app.py CHANGED
@@ -3,7 +3,7 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
6
- from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool,Tool,tool,VisitWebpageTool,FinalAnswerTool
7
  import base64
8
 
9
  # (Keep Constants as is)
@@ -95,48 +95,58 @@ class BasicAgent:
95
  reader = PdfReader(file_path)
96
  return "\n".join(page.extract_text() or "" for page in reader.pages)
97
 
98
- @tool
99
- def analyze_image(file_path: str, question: str) -> str:
100
- """
101
- Analyzes an image and answers a question about its contents.
102
-
103
- Args:
104
- file_path: Local path to the image file.
105
- question: What to look for or answer about the image.
106
-
107
- Returns:
108
- A text answer describing what's found in the image.
109
- """
110
- client = InferenceClient(token=os.environ["HF_TOKEN"])
 
 
 
 
 
111
 
 
112
  with open(file_path, "rb") as f:
113
  image_bytes = f.read()
114
  image_b64 = base64.b64encode(image_bytes).decode("utf-8")
115
 
116
  result = client.chat_completion(
117
- model="Qwen/Qwen2.5-VL-72B-Instruct",
118
- messages=[
119
- {
120
- "role": "user",
121
- "content": [
122
- {"type": "text", "text": question},
123
- {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_b64}"}},
124
- ]
125
- }
126
- ]
127
- )
128
  return result.choices[0].message.content
129
- @tool
130
- def transcribe_audio(file_path: str) -> str:
131
- """
132
- Transcribes speech from an audio file to text.
133
-
134
- Args:
135
- file_path: Local path to the audio file.
136
-
137
- Returns:
138
- The transcribed text.
139
- """
 
 
 
 
140
  client = InferenceClient(token=os.environ["HF_TOKEN"])
141
  result = client.automatic_speech_recognition(
142
  file_path,
@@ -152,22 +162,23 @@ class BasicAgent:
152
  tools=[
153
  DuckDuckGoSearchTool(),
154
  VisitWebpageTool(),
155
- #PythonInterpreterTool(),
156
  FinalAnswerTool(),
157
  #pdf_tool,
158
  fetch_task_file,
159
  #read_pdf,
160
  read_spreadsheet,
161
  get_youtube_transcript,
162
- analyze_image,
163
- transcribe_audio,
164
  ],
165
  model= InferenceClientModel("Qwen/Qwen2.5-Coder-32B-Instruct") ,
166
  additional_authorized_imports=["pandas", "requests", "re"],
167
  instructions = ("You are an advanced CodeAgent that will show your capabilities to work in the real world by being tested in GAIA, the agent testing platform. If the question includes a task_id and mentions a file, call fetch_task_file first; route YouTube URLs to get_youtube_transcript, other URLs to visit_webpage, PDFs to pdf_tool, and spreadsheets to read_spreadsheet, using web_search only when no URL or file is given,then respond with only the exact final answer value, no explanation, no prefix."
168
  "Use the Thought Action observation to produce high quality results and only answer when you are sure you have performed the necessary steps for the task and question"
169
- "If the file is an image, use analyze_image with a specific question about "
170
- "what to find. If the file is audio, use transcribe_audio first, then reason over the transcribed text "),
 
171
  max_steps=20,
172
  )
173
  #answering questions
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ from smolagents import CodeAgent, InferenceClientModel,huggingface_hub, DuckDuckGoSearchTool,Tool,tool,VisitWebpageTool,PythonInterpreterTool,FinalAnswerTool
7
  import base64
8
 
9
  # (Keep Constants as is)
 
95
  reader = PdfReader(file_path)
96
  return "\n".join(page.extract_text() or "" for page in reader.pages)
97
 
98
+ class AnalyzeImageTool(Tool):
99
+ name = "analyze_image"
100
+ description = "Analyzes an image and answers a question about its contents."
101
+ inputs = {
102
+ "file_path": {
103
+ "type": "string",
104
+ "description": "Local path to the image file.",
105
+ },
106
+ "question": {
107
+ "type": "string",
108
+ "description": "What to look for or answer about the image.",
109
+ },
110
+ }
111
+ output_type = "string"
112
+
113
+ def forward(self, file_path: str, question: str) -> str:
114
+ import base64
115
+ from huggingface_hub import InferenceClient
116
 
117
+ client = InferenceClient(token=os.environ["HF_TOKEN"])
118
  with open(file_path, "rb") as f:
119
  image_bytes = f.read()
120
  image_b64 = base64.b64encode(image_bytes).decode("utf-8")
121
 
122
  result = client.chat_completion(
123
+ model="Qwen/Qwen2.5-VL-72B-Instruct",
124
+ messages=[
125
+ {
126
+ "role": "user",
127
+ "content": [
128
+ {"type": "text", "text": question},
129
+ {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_b64}"}},
130
+ ],
131
+ }
132
+ ],
133
+ )
134
  return result.choices[0].message.content
135
+
136
+
137
+ class TranscribeAudioTool(Tool):
138
+ name = "transcribe_audio"
139
+ description = "Transcribes speech from an audio file to text."
140
+ inputs = {
141
+ "file_path": {
142
+ "type": "string",
143
+ "description": "Local path to the audio file.",
144
+ }
145
+ }
146
+ output_type = "string"
147
+
148
+ def forward(self, file_path: str) -> str:
149
+ from huggingface_hub import InferenceClient
150
  client = InferenceClient(token=os.environ["HF_TOKEN"])
151
  result = client.automatic_speech_recognition(
152
  file_path,
 
162
  tools=[
163
  DuckDuckGoSearchTool(),
164
  VisitWebpageTool(),
165
+ PythonInterpreterTool(),
166
  FinalAnswerTool(),
167
  #pdf_tool,
168
  fetch_task_file,
169
  #read_pdf,
170
  read_spreadsheet,
171
  get_youtube_transcript,
172
+ AnalyzeImageTool,
173
+ TranscribeAudioTool
174
  ],
175
  model= InferenceClientModel("Qwen/Qwen2.5-Coder-32B-Instruct") ,
176
  additional_authorized_imports=["pandas", "requests", "re"],
177
  instructions = ("You are an advanced CodeAgent that will show your capabilities to work in the real world by being tested in GAIA, the agent testing platform. If the question includes a task_id and mentions a file, call fetch_task_file first; route YouTube URLs to get_youtube_transcript, other URLs to visit_webpage, PDFs to pdf_tool, and spreadsheets to read_spreadsheet, using web_search only when no URL or file is given,then respond with only the exact final answer value, no explanation, no prefix."
178
  "Use the Thought Action observation to produce high quality results and only answer when you are sure you have performed the necessary steps for the task and question"
179
+ "If the file is an image, use AnalyzeImageTool with a specific question about it"
180
+ "what to find. If the file is audio, use TranscribeAudioTool first, then reason over the transcribed text "
181
+ "Use the PythonInterpreterTool for code interpretation in python"),
182
  max_steps=20,
183
  )
184
  #answering questions