anewryzm commited on
Commit
bcb4653
·
verified ·
1 Parent(s): e446a53

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +68 -22
app.py CHANGED
@@ -12,8 +12,12 @@ import shutil
12
  # Load environment variables
13
  load_dotenv()
14
 
15
- # Initialize Anthropic client
16
- client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
 
 
 
 
17
 
18
  # MCP connection details
19
  mcp_url = "https://agents-mcp-hackathon-transcript-generator.hf.space/gradio_api/mcp/sse"
@@ -67,9 +71,13 @@ def validate_file(file):
67
  return True, "File is valid"
68
 
69
  # Process the transcription using the MCP tool
70
- def process_transcription(audio_file, groq_api_key, request: gr.Request):
71
  try:
72
  global mcp_tools
 
 
 
 
73
 
74
  # Check if tools are initialized
75
  if not mcp_tools:
@@ -105,7 +113,7 @@ def process_transcription(audio_file, groq_api_key, request: gr.Request):
105
  return "Error: Transcription tool not found in MCP client"
106
 
107
  # Call the tool with the audio file URL and GROQ API key
108
- result = transcription_tool(audio_file=file_url, api_key=groq_api_key)
109
  return result
110
 
111
  except Exception as e:
@@ -115,7 +123,24 @@ def process_transcription(audio_file, groq_api_key, request: gr.Request):
115
  return f"Error: {str(e)}"
116
 
117
  # Main chat function (streaming, with tool usage)
118
- def chat_with_tools(user_message, history, audio_file, groq_api_key, request: gr.Request):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  # Add user message
120
  history = history or []
121
  messages = format_history(history)
@@ -146,8 +171,9 @@ def chat_with_tools(user_message, history, audio_file, groq_api_key, request: gr
146
  # 2. Tool usage phase - Process transcription if there's an audio file
147
  if audio_file is not None:
148
  try:
149
- # Check if GROQ API key is provided
150
- if not groq_api_key:
 
151
  history.append(ChatMessage(
152
  role="assistant",
153
  content="Please provide a GROQ API key to process the transcription.",
@@ -164,7 +190,7 @@ def chat_with_tools(user_message, history, audio_file, groq_api_key, request: gr
164
  )]
165
 
166
  # Process transcription with GROQ API key
167
- tool_result = process_transcription(audio_file, groq_api_key, request)
168
 
169
  if tool_result.startswith("Error:"):
170
  history.append(ChatMessage(
@@ -201,12 +227,20 @@ def chat_with_tools(user_message, history, audio_file, groq_api_key, request: gr
201
  yield history
202
 
203
  # 3. Claude response phase
204
- response = client.messages.create(
205
- model="claude-3-7-sonnet-latest",
206
- max_tokens=4096,
207
- messages=messages
208
- )
209
- history.append(ChatMessage(role="assistant", content=response.content[0].text))
 
 
 
 
 
 
 
 
210
  yield history
211
 
212
  # Initialize tools at startup
@@ -232,11 +266,23 @@ with gr.Blocks() as demo:
232
  type="filepath"
233
  )
234
 
235
- groq_api_key = gr.Textbox(
236
- label="GROQ API Key",
237
- placeholder="Enter your GROQ API key",
 
 
 
 
 
 
 
 
 
 
238
  type="password",
239
- show_label=True
 
 
240
  )
241
 
242
  with gr.Row():
@@ -249,12 +295,12 @@ with gr.Blocks() as demo:
249
  btn = gr.Button("Send", scale=1)
250
 
251
  # Connect components
252
- btn.click(chat_with_tools, [msg, chatbot, audio_file, groq_api_key], [chatbot], queue=True)
253
- msg.submit(chat_with_tools, [msg, chatbot, audio_file, groq_api_key], [chatbot], queue=True)
254
 
255
  # Clear button
256
  clear = gr.Button("Clear Conversation")
257
- clear.click(lambda: (None, None, None), outputs=[chatbot, audio_file, groq_api_key])
258
 
259
  # Add a custom route to serve audio files
260
  @demo.app.get("/audio_files/{filename}")
@@ -264,4 +310,4 @@ with gr.Blocks() as demo:
264
  return FileResponse(file_path)
265
 
266
  if __name__ == "__main__":
267
- demo.launch()
 
12
  # Load environment variables
13
  load_dotenv()
14
 
15
+ # Get API keys from environment or use None as default
16
+ anthropic_api_key = os.environ.get("ANTHROPIC_API_KEY")
17
+ groq_api_key = os.environ.get("GROQ_API_KEY")
18
+
19
+ # Initialize Anthropic client (will be updated if user provides a key)
20
+ client = anthropic.Anthropic(api_key=anthropic_api_key) if anthropic_api_key else None
21
 
22
  # MCP connection details
23
  mcp_url = "https://agents-mcp-hackathon-transcript-generator.hf.space/gradio_api/mcp/sse"
 
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
78
+
79
+ # Use environment variable or user-provided key
80
+ current_groq_api_key = groq_api_key if groq_api_key else user_groq_api_key
81
 
82
  # Check if tools are initialized
83
  if not mcp_tools:
 
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:
 
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
130
+ current_anthropic_api_key = anthropic_api_key if anthropic_api_key else user_anthropic_api_key
131
+ if not current_anthropic_api_key:
132
+ history.append(ChatMessage(
133
+ role="assistant",
134
+ content="Please provide an Anthropic API key to continue.",
135
+ metadata={"title": "❌ Error", "status": "done"}
136
+ ))
137
+ yield history
138
+ return
139
+
140
+ # Update client if needed
141
+ if client is None or (not anthropic_api_key and user_anthropic_api_key):
142
+ client = anthropic.Anthropic(api_key=current_anthropic_api_key)
143
+
144
  # Add user message
145
  history = history or []
146
  messages = format_history(history)
 
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
176
+ if not current_groq_api_key:
177
  history.append(ChatMessage(
178
  role="assistant",
179
  content="Please provide a GROQ API key to process the transcription.",
 
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(
 
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",
240
+ content=f"Error connecting to Claude: {str(e)}",
241
+ metadata={"title": "❌ Error", "status": "done"}
242
+ ))
243
+
244
  yield history
245
 
246
  # Initialize tools at startup
 
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 ""),
272
+ placeholder="Enter your Anthropic API key" if not anthropic_api_key else "Using environment variable",
273
+ type="password",
274
+ show_label=True,
275
+ interactive=not anthropic_api_key,
276
+ value="" if not anthropic_api_key else None
277
+ )
278
+
279
+ groq_api_key_input = gr.Textbox(
280
+ label="GROQ API Key" + (" (using env variable)" if groq_api_key else ""),
281
+ placeholder="Enter your GROQ API key" if not groq_api_key else "Using environment variable",
282
  type="password",
283
+ show_label=True,
284
+ interactive=not groq_api_key,
285
+ value="" if not groq_api_key else None
286
  )
287
 
288
  with gr.Row():
 
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}")
 
310
  return FileResponse(file_path)
311
 
312
  if __name__ == "__main__":
313
+ demo.launch()