anewryzm commited on
Commit
75b1177
·
verified ·
1 Parent(s): 50c3a48

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. .gitattributes +1 -0
  2. app.py +70 -101
  3. test_files/hello-mcp.wav +3 -0
.gitattributes CHANGED
@@ -37,3 +37,4 @@ temp_files/23eb960d-34db-4eb4-9d74-a9dfb5a3ca94.m4a filter=lfs diff=lfs merge=lf
37
  temp_files/313511c1-b8bb-4c2a-8ace-777b3bbd01a8.m4a filter=lfs diff=lfs merge=lfs -text
38
  temp_files/d612cbb0-1910-44f6-b4a8-9ccee4af0a52.m4a filter=lfs diff=lfs merge=lfs -text
39
  test_files/hi.wav filter=lfs diff=lfs merge=lfs -text
 
 
37
  temp_files/313511c1-b8bb-4c2a-8ace-777b3bbd01a8.m4a filter=lfs diff=lfs merge=lfs -text
38
  temp_files/d612cbb0-1910-44f6-b4a8-9ccee4af0a52.m4a filter=lfs diff=lfs merge=lfs -text
39
  test_files/hi.wav filter=lfs diff=lfs merge=lfs -text
40
+ test_files/hello-mcp.wav filter=lfs diff=lfs merge=lfs -text
app.py CHANGED
@@ -1,13 +1,9 @@
1
  import gradio as gr
2
  import anthropic
3
  import os
4
- import asyncio
5
  from dotenv import load_dotenv
6
  from smolagents.mcp_client import MCPClient
7
  from gradio import ChatMessage
8
- import concurrent.futures
9
- import uuid
10
- import shutil
11
 
12
  # Load environment variables
13
  load_dotenv()
@@ -25,10 +21,6 @@ mcp_url = "https://agents-mcp-hackathon-transcript-generator.hf.space/gradio_api
25
  # Global variable to store tools
26
  mcp_tools = {}
27
 
28
- # Create a directory to store temporary files
29
- TEMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "temp_files")
30
- os.makedirs(TEMP_DIR, exist_ok=True)
31
-
32
  # Initialize tools at startup
33
  def initialize_tools():
34
  global mcp_tools
@@ -51,27 +43,8 @@ def format_history(history):
51
  messages.append({"role": item["role"], "content": item["content"]})
52
  return messages
53
 
54
- # File validation function
55
- def validate_file(file):
56
- """Validate uploaded file type and size."""
57
- if file is None:
58
- return False, "No file uploaded"
59
-
60
- # Check file size (25MB limit)
61
- file_size_mb = os.path.getsize(file) / (1024 * 1024)
62
- if file_size_mb > 25:
63
- return False, f"File size ({file_size_mb:.1f}MB) exceeds 25MB limit"
64
-
65
- # Check file extension
66
- valid_extensions = ['.mp3', '.mp4', '.mpeg', '.mpga', '.m4a', '.wav', '.webm', '.flac', '.ogg', '.aac']
67
- file_extension = os.path.splitext(file)[1].lower()
68
- if file_extension not in valid_extensions:
69
- return False, f"Invalid file type. Supported formats: {', '.join(valid_extensions)}"
70
-
71
- return True, "File is valid"
72
-
73
- # Process the transcription using the MCP tool
74
- def process_transcription(audio_file, user_groq_api_key, request: gr.Request):
75
  try:
76
  global mcp_tools
77
  global groq_api_key
@@ -85,35 +58,19 @@ def process_transcription(audio_file, user_groq_api_key, request: gr.Request):
85
  return "Error: Failed to initialize MCP tools"
86
 
87
  # Get the transcription tool
88
- tool = mcp_tools.get("transcript_generator_transcribe_audio")
89
  if not tool:
90
  return "Error: Transcription tool not available"
91
-
92
- # Create a copy of the file with a unique name to avoid conflicts
93
- file_ext = os.path.splitext(audio_file)[1]
94
- unique_filename = f"{uuid.uuid4()}{file_ext}"
95
- temp_file_path = os.path.join(TEMP_DIR, unique_filename)
96
- shutil.copy2(audio_file, temp_file_path)
97
-
98
- # Get the base URL from the request
99
- base_url = str(request.base_url)
100
- if base_url.endswith('/'):
101
- base_url = base_url[:-1]
102
 
103
- # Create a URL that points to our custom route
104
- file_url = f"{base_url}/audio_files/{unique_filename}"
105
-
106
- print(f"Using file URL: {file_url}")
107
-
108
  # Create a new MCP client for this specific transaction
109
  with MCPClient({"url": mcp_url}) as mcp_client:
110
- transcription_tool = next((t for t in mcp_client if t.name == "transcript_generator_transcribe_audio"), None)
111
 
112
  if not transcription_tool:
113
  return "Error: Transcription tool not found in MCP client"
114
 
115
- # Call the tool with the audio file URL and GROQ API key
116
- result = transcription_tool(audio_file=file_url, api_key=current_groq_api_key)
117
  return result
118
 
119
  except Exception as e:
@@ -122,8 +79,18 @@ def process_transcription(audio_file, user_groq_api_key, request: gr.Request):
122
  print(traceback.format_exc())
123
  return f"Error: {str(e)}"
124
 
 
 
 
 
 
 
 
 
 
 
125
  # Main chat function (streaming, with tool usage)
126
- def chat_with_tools(user_message, history, audio_file, user_groq_api_key, user_anthropic_api_key, request: gr.Request):
127
  global client, anthropic_api_key, groq_api_key
128
 
129
  # Check if we need to update the Anthropic client with user-provided API key
@@ -145,31 +112,21 @@ def chat_with_tools(user_message, history, audio_file, user_groq_api_key, user_a
145
  history = history or []
146
  messages = format_history(history)
147
 
148
- # Check if a file was uploaded
149
- file_message = ""
150
- if audio_file is not None:
151
- # Validate the file
152
- is_valid, message = validate_file(audio_file)
153
- if not is_valid:
154
- history.append(ChatMessage(
155
- role="assistant",
156
- content=message,
157
- metadata={"title": "❌ Error", "status": "done"}
158
- ))
159
- yield history
160
- return
161
-
162
- file_name = os.path.basename(audio_file)
163
- file_message = f"\n[Attached file: {file_name}]"
164
-
165
- # Add user message with file reference if applicable
166
- messages.append({"role": "user", "content": user_message + file_message})
167
 
168
  # 1. "Thinking" phase
169
  yield history + [ChatMessage(role="assistant", content="Let me think...", metadata={"title": "🧠 Thinking", "status": "pending"})]
170
 
171
- # 2. Tool usage phase - Process transcription if there's an audio file
172
- if audio_file is not None:
 
 
 
 
 
 
 
173
  try:
174
  # Check if GROQ API key is available (either from env or user input)
175
  current_groq_api_key = groq_api_key if groq_api_key else user_groq_api_key
@@ -185,26 +142,40 @@ def chat_with_tools(user_message, history, audio_file, user_groq_api_key, user_a
185
  # Show tool call status
186
  yield history + [ChatMessage(
187
  role="assistant",
188
- content="Processing your audio file...",
189
  metadata={"title": "🛠️ Tool Usage", "status": "pending"}
190
  )]
191
 
192
  # Process transcription with GROQ API key
193
- tool_result = process_transcription(audio_file, user_groq_api_key, request)
194
 
195
- if tool_result.startswith("Error:"):
196
  history.append(ChatMessage(
197
  role="assistant",
198
- content=tool_result,
199
  metadata={"title": "❌ Error", "status": "done"}
200
  ))
201
  else:
202
  # Add tool result to history
203
  history.append(ChatMessage(
204
  role="assistant",
205
- content=f"**Transcription Result:**\n\n{tool_result}",
206
  metadata={"title": "🎙️ Transcription", "status": "done"}
207
  ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
  yield history
209
  except Exception as e:
210
  import traceback
@@ -213,27 +184,38 @@ def chat_with_tools(user_message, history, audio_file, user_groq_api_key, user_a
213
 
214
  history.append(ChatMessage(
215
  role="assistant",
216
- content=f"Error processing the audio file: {str(e)}",
217
  metadata={"title": "❌ Error", "status": "done"}
218
  ))
219
  yield history
220
- # Manual transcript request without file
221
- elif "transcript" in user_message.lower():
222
  history.append(ChatMessage(
223
  role="assistant",
224
- content="Please upload an audio or video file for transcription.",
225
  metadata={"title": "ℹ️ Info", "status": "done"}
226
  ))
227
  yield history
 
228
 
229
- # 3. Claude response phase
230
  try:
 
231
  response = client.messages.create(
232
  model="claude-3-7-sonnet-latest",
233
  max_tokens=4096,
234
  messages=messages
235
  )
236
- history.append(ChatMessage(role="assistant", content=response.content[0].text))
 
 
 
 
 
 
 
 
 
237
  except Exception as e:
238
  history.append(ChatMessage(
239
  role="assistant",
@@ -246,10 +228,10 @@ def chat_with_tools(user_message, history, audio_file, user_groq_api_key, user_a
246
  # Initialize tools at startup
247
  initialize_tools()
248
 
249
- # Create the Gradio interface with file upload
250
  with gr.Blocks() as demo:
251
  gr.Markdown("# Claude 3.7 Sonnet Chat with Transcription Tools")
252
- gr.Markdown("Upload an audio or video file to generate a transcript using the MCP server.")
253
 
254
  with gr.Row():
255
  with gr.Column(scale=4):
@@ -260,12 +242,6 @@ with gr.Blocks() as demo:
260
  )
261
 
262
  with gr.Column(scale=1):
263
- audio_file = gr.File(
264
- label="Upload Audio/Video File",
265
- file_types=[".mp3", ".mp4", ".mpeg", ".mpga", ".m4a", ".wav", ".webm", ".flac", ".ogg", ".aac"],
266
- type="filepath"
267
- )
268
-
269
  # Display API key inputs only if environment variables are not set
270
  anthropic_api_key_input = gr.Textbox(
271
  label="Anthropic API Key" + (" (using env variable)" if anthropic_api_key else ""),
@@ -287,7 +263,7 @@ with gr.Blocks() as demo:
287
 
288
  with gr.Row():
289
  msg = gr.Textbox(
290
- placeholder="Ask questions or request transcript analysis...",
291
  show_label=False,
292
  container=False,
293
  scale=9
@@ -295,19 +271,12 @@ with gr.Blocks() as demo:
295
  btn = gr.Button("Send", scale=1)
296
 
297
  # Connect components
298
- btn.click(chat_with_tools, [msg, chatbot, audio_file, groq_api_key_input, anthropic_api_key_input], [chatbot], queue=True)
299
- msg.submit(chat_with_tools, [msg, chatbot, audio_file, groq_api_key_input, anthropic_api_key_input], [chatbot], queue=True)
300
 
301
  # Clear button
302
  clear = gr.Button("Clear Conversation")
303
- clear.click(lambda: (None, None, None, None, None), outputs=[chatbot, audio_file, groq_api_key_input, anthropic_api_key_input])
304
-
305
- # Add a custom route to serve audio files
306
- @demo.app.get("/audio_files/{filename}")
307
- async def serve_audio_file(filename: str):
308
- from fastapi.responses import FileResponse
309
- file_path = os.path.join(TEMP_DIR, filename)
310
- return FileResponse(file_path)
311
 
312
  if __name__ == "__main__":
313
  demo.launch()
 
1
  import gradio as gr
2
  import anthropic
3
  import os
 
4
  from dotenv import load_dotenv
5
  from smolagents.mcp_client import MCPClient
6
  from gradio import ChatMessage
 
 
 
7
 
8
  # Load environment variables
9
  load_dotenv()
 
21
  # Global variable to store tools
22
  mcp_tools = {}
23
 
 
 
 
 
24
  # Initialize tools at startup
25
  def initialize_tools():
26
  global mcp_tools
 
43
  messages.append({"role": item["role"], "content": item["content"]})
44
  return messages
45
 
46
+ # Process the transcription using the MCP tool with URL
47
+ def process_transcription_from_url(audio_url, user_groq_api_key):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  try:
49
  global mcp_tools
50
  global groq_api_key
 
58
  return "Error: Failed to initialize MCP tools"
59
 
60
  # Get the transcription tool
61
+ tool = mcp_tools.get("transcript_generator_transcribe_audio_from_url")
62
  if not tool:
63
  return "Error: Transcription tool not available"
 
 
 
 
 
 
 
 
 
 
 
64
 
 
 
 
 
 
65
  # Create a new MCP client for this specific transaction
66
  with MCPClient({"url": mcp_url}) as mcp_client:
67
+ transcription_tool = next((t for t in mcp_client if t.name == "transcript_generator_transcribe_audio_from_url"), None)
68
 
69
  if not transcription_tool:
70
  return "Error: Transcription tool not found in MCP client"
71
 
72
+ # Call the tool with the audio URL and GROQ API key
73
+ result = transcription_tool(audio_url=audio_url, api_key=current_groq_api_key)
74
  return result
75
 
76
  except Exception as e:
 
79
  print(traceback.format_exc())
80
  return f"Error: {str(e)}"
81
 
82
+ # Extract URL from message
83
+ def extract_url(message):
84
+ import re
85
+ # Simple URL extraction regex
86
+ url_pattern = r'https?://[^\s]+'
87
+ match = re.search(url_pattern, message)
88
+ if match:
89
+ return match.group(0)
90
+ return None
91
+
92
  # Main chat function (streaming, with tool usage)
93
+ def chat_with_tools(user_message, history, user_groq_api_key, user_anthropic_api_key):
94
  global client, anthropic_api_key, groq_api_key
95
 
96
  # Check if we need to update the Anthropic client with user-provided API key
 
112
  history = history or []
113
  messages = format_history(history)
114
 
115
+ # Add user message
116
+ messages.append({"role": "user", "content": user_message})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
  # 1. "Thinking" phase
119
  yield history + [ChatMessage(role="assistant", content="Let me think...", metadata={"title": "🧠 Thinking", "status": "pending"})]
120
 
121
+ # 2. Tool usage phase - Process transcription if there's an audio URL
122
+ audio_url = extract_url(user_message)
123
+ transcript_request = "transcript" in user_message.lower() or "transcribe" in user_message.lower()
124
+
125
+ # Flag to track if transcription was performed
126
+ transcription_performed = False
127
+ transcription_result = None
128
+
129
+ if audio_url and transcript_request:
130
  try:
131
  # Check if GROQ API key is available (either from env or user input)
132
  current_groq_api_key = groq_api_key if groq_api_key else user_groq_api_key
 
142
  # Show tool call status
143
  yield history + [ChatMessage(
144
  role="assistant",
145
+ content=f"Processing audio from URL: {audio_url}...",
146
  metadata={"title": "🛠️ Tool Usage", "status": "pending"}
147
  )]
148
 
149
  # Process transcription with GROQ API key
150
+ transcription_result = process_transcription_from_url(audio_url, user_groq_api_key)
151
 
152
+ if transcription_result.startswith("Error:"):
153
  history.append(ChatMessage(
154
  role="assistant",
155
+ content=transcription_result,
156
  metadata={"title": "❌ Error", "status": "done"}
157
  ))
158
  else:
159
  # Add tool result to history
160
  history.append(ChatMessage(
161
  role="assistant",
162
+ content=f"**Transcription Result:**\n\n{transcription_result}",
163
  metadata={"title": "🎙️ Transcription", "status": "done"}
164
  ))
165
+ transcription_performed = True
166
+
167
+ # Add the transcription result to messages for Claude to be aware of it
168
+ messages.append({
169
+ "role": "assistant",
170
+ "content": f"I've transcribed the audio from the URL. Here's the result:\n\n{transcription_result}"
171
+ })
172
+
173
+ # Add a user message to prompt Claude to respond to the transcription
174
+ messages.append({
175
+ "role": "user",
176
+ "content": "Please analyze this transcription and provide your thoughts or follow-up on my original request."
177
+ })
178
+
179
  yield history
180
  except Exception as e:
181
  import traceback
 
184
 
185
  history.append(ChatMessage(
186
  role="assistant",
187
+ content=f"Error processing the audio URL: {str(e)}",
188
  metadata={"title": "❌ Error", "status": "done"}
189
  ))
190
  yield history
191
+ # Manual transcript request without URL
192
+ elif transcript_request and not audio_url:
193
  history.append(ChatMessage(
194
  role="assistant",
195
+ content="Please provide an audio URL for transcription. I need a direct link to the audio file you want to transcribe.",
196
  metadata={"title": "ℹ️ Info", "status": "done"}
197
  ))
198
  yield history
199
+ return
200
 
201
+ # 3. Claude response phase - only if we haven't returned early
202
  try:
203
+ # If transcription was performed, Claude will now respond to the transcription
204
  response = client.messages.create(
205
  model="claude-3-7-sonnet-latest",
206
  max_tokens=4096,
207
  messages=messages
208
  )
209
+
210
+ response_content = response.content[0].text
211
+
212
+ # For clarity in the UI, if transcription was performed,
213
+ # we can add a prefix to indicate Claude is responding to the transcription
214
+ if transcription_performed:
215
+ prefix = "**Analysis of the transcription:**\n\n"
216
+ history.append(ChatMessage(role="assistant", content=prefix + response_content))
217
+ else:
218
+ history.append(ChatMessage(role="assistant", content=response_content))
219
  except Exception as e:
220
  history.append(ChatMessage(
221
  role="assistant",
 
228
  # Initialize tools at startup
229
  initialize_tools()
230
 
231
+ # Create the Gradio interface
232
  with gr.Blocks() as demo:
233
  gr.Markdown("# Claude 3.7 Sonnet Chat with Transcription Tools")
234
+ gr.Markdown("Send a message with an audio URL to generate a transcript using the MCP server.")
235
 
236
  with gr.Row():
237
  with gr.Column(scale=4):
 
242
  )
243
 
244
  with gr.Column(scale=1):
 
 
 
 
 
 
245
  # Display API key inputs only if environment variables are not set
246
  anthropic_api_key_input = gr.Textbox(
247
  label="Anthropic API Key" + (" (using env variable)" if anthropic_api_key else ""),
 
263
 
264
  with gr.Row():
265
  msg = gr.Textbox(
266
+ placeholder="Ask questions or paste an audio URL with 'transcribe' to generate a transcript...",
267
  show_label=False,
268
  container=False,
269
  scale=9
 
271
  btn = gr.Button("Send", scale=1)
272
 
273
  # Connect components
274
+ btn.click(chat_with_tools, [msg, chatbot, groq_api_key_input, anthropic_api_key_input], [chatbot], queue=True)
275
+ msg.submit(chat_with_tools, [msg, chatbot, groq_api_key_input, anthropic_api_key_input], [chatbot], queue=True)
276
 
277
  # Clear button
278
  clear = gr.Button("Clear Conversation")
279
+ clear.click(lambda: (None, None, None), outputs=[chatbot, groq_api_key_input, anthropic_api_key_input])
 
 
 
 
 
 
 
280
 
281
  if __name__ == "__main__":
282
  demo.launch()
test_files/hello-mcp.wav ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:99c5008c9a03d53f3026b6cd1b86b08411831cc1d5bf70defbef7f0d674ba5a2
3
+ size 393680