Aya1610 commited on
Commit
d066919
·
verified ·
1 Parent(s): e9d4a81

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +56 -2
agent.py CHANGED
@@ -3,6 +3,8 @@ import os
3
  import operator
4
  import json
5
  import re
 
 
6
  from youtube_transcript_api import YouTubeTranscriptApi
7
  from typing import TypedDict, Annotated, Sequence, Union
8
  from langgraph.graph import StateGraph, END
@@ -149,13 +151,65 @@ def convert_currency(amount: float, from_currency: str, to_currency: str) -> flo
149
  return round(amount * rates[from_currency.upper()][to_currency.upper()], 2)
150
  except:
151
  return "Error: Currency not supported"
152
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  # ---------------------
154
  # Agent Setup
155
  # ---------------------
156
 
157
  # Available tools
158
- tools = [search_tool, wikipedia, analyze_video, describe_image, calculate, convert_date, convert_currency]
159
  tools_as_openai = [convert_to_openai_tool(t) for t in tools]
160
 
161
  # Agent State Definition
 
3
  import operator
4
  import json
5
  import re
6
+ import requests
7
+ import tempfile
8
  from youtube_transcript_api import YouTubeTranscriptApi
9
  from typing import TypedDict, Annotated, Sequence, Union
10
  from langgraph.graph import StateGraph, END
 
151
  return round(amount * rates[from_currency.upper()][to_currency.upper()], 2)
152
  except:
153
  return "Error: Currency not supported"
154
+ @tool
155
+ def process_audio_note(audio_url: str, instructions: str) -> str:
156
+ """
157
+ Extract specific information from an audio note based on user instructions.
158
+ Handles various requests like recipes, meeting notes, reminders, etc.
159
+
160
+ Args:
161
+ audio_url: URL of the audio file
162
+ instructions: Specific instructions for what to extract and how to format
163
+
164
+ Returns:
165
+ Requested information formatted as specified
166
+ """
167
+ try:
168
+ # Download audio file
169
+ response = requests.get(audio_url)
170
+ response.raise_for_status()
171
+
172
+ # Create temporary audio file
173
+ with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_audio:
174
+ temp_audio.write(response.content)
175
+ temp_audio_path = temp_audio.name
176
+
177
+ # Transcribe audio using Whisper
178
+ client = OpenAI()
179
+ with open(temp_audio_path, "rb") as audio_file:
180
+ transcript = client.audio.transcriptions.create(
181
+ model="whisper-large-v3",
182
+ file=audio_file,
183
+ response_format="text"
184
+ )
185
+
186
+ # Create system prompt based on instructions
187
+ system_prompt = (
188
+ "You're an audio processing assistant. Carefully follow these instructions:\n"
189
+ f"{instructions}\n\n"
190
+ "Transcript of the audio note:\n"
191
+ )
192
+
193
+ # Process transcript to extract requested information
194
+ response = client.chat.completions.create(
195
+ model="gpt-4-turbo",
196
+ messages=[
197
+ {"role": "system", "content": system_prompt},
198
+ {"role": "user", "content": transcript}
199
+ ],
200
+ max_tokens=1000
201
+ )
202
+
203
+ return response.choices[0].message.content
204
+
205
+ except Exception as e:
206
+ return f"Error processing audio: {str(e)}"
207
  # ---------------------
208
  # Agent Setup
209
  # ---------------------
210
 
211
  # Available tools
212
+ tools = [search_tool, wikipedia, analyze_video, describe_image, calculate, convert_date, convert_currency, process_audio_note]
213
  tools_as_openai = [convert_to_openai_tool(t) for t in tools]
214
 
215
  # Agent State Definition