shukdevdattaEX commited on
Commit
49c9e15
Β·
verified Β·
1 Parent(s): 38b2ece

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -16
app.py CHANGED
@@ -213,9 +213,8 @@ class MultimodalChatbot:
213
  def create_interface():
214
  """Create the Gradio interface"""
215
 
216
- # Initialize chatbot (you'll need to set your API key)
217
- api_key = os.getenv("OPENROUTER_API_KEY", "your_api_key_here")
218
- chatbot = MultimodalChatbot(api_key)
219
 
220
  with gr.Blocks(title="Multimodal Chatbot with Gemma 3n", theme=gr.themes.Soft()) as demo:
221
  gr.Markdown("""
@@ -228,9 +227,24 @@ def create_interface():
228
  - **Images**: Analyze visual content
229
  - **Video**: Extract frames and analyze video content
230
 
231
- **Setup**: Set your OpenRouter API key as an environment variable `OPENROUTER_API_KEY`
232
  """)
233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
  with gr.Row():
235
  with gr.Column(scale=1):
236
  # Input components
@@ -263,7 +277,7 @@ def create_interface():
263
  type="filepath"
264
  )
265
 
266
- submit_btn = gr.Button("πŸš€ Send", variant="primary", size="lg")
267
  clear_btn = gr.Button("πŸ—‘οΈ Clear", variant="secondary")
268
 
269
  with gr.Column(scale=2):
@@ -275,16 +289,45 @@ def create_interface():
275
  )
276
 
277
  # Event handlers
278
- def process_input(text, pdf, audio, image, video, history):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279
  return chatbot.chat(text, pdf, audio, image, video, history)
280
 
281
  def clear_all():
282
  return [], "", None, None, None, None
283
 
 
 
 
 
 
 
 
284
  # Button events
285
  submit_btn.click(
286
  process_input,
287
- inputs=[text_input, pdf_input, audio_input, image_input, video_input, chatbot_interface],
288
  outputs=[chatbot_interface, text_input]
289
  )
290
 
@@ -296,18 +339,27 @@ def create_interface():
296
  # Enter key support
297
  text_input.submit(
298
  process_input,
299
- inputs=[text_input, pdf_input, audio_input, image_input, video_input, chatbot_interface],
300
  outputs=[chatbot_interface, text_input]
301
  )
302
 
303
  # Examples
304
  gr.Markdown("""
305
  ### 🎯 Example Usage:
306
- - Upload a PDF and ask "Summarize this document"
307
- - Upload an image and ask "What do you see in this image?"
308
- - Record audio and ask "What did I say?"
309
- - Upload a video and ask "Describe what's happening"
310
- - Combine multiple inputs: "Compare this image with the PDF content"
 
 
 
 
 
 
 
 
 
311
  """)
312
 
313
  return demo
@@ -324,11 +376,17 @@ if __name__ == "__main__":
324
  "numpy"
325
  ]
326
 
 
 
327
  print("Required packages:", ", ".join(required_packages))
328
- print("\nTo install: pip install " + " ".join(required_packages))
329
- print("\nDon't forget to set your OPENROUTER_API_KEY environment variable!")
 
330
 
331
  demo = create_interface()
332
  demo.launch(
333
- share=True
 
 
 
334
  )
 
213
  def create_interface():
214
  """Create the Gradio interface"""
215
 
216
+ # Chatbot will be initialized when API key is provided
217
+ chatbot = None
 
218
 
219
  with gr.Blocks(title="Multimodal Chatbot with Gemma 3n", theme=gr.themes.Soft()) as demo:
220
  gr.Markdown("""
 
227
  - **Images**: Analyze visual content
228
  - **Video**: Extract frames and analyze video content
229
 
230
+ **Setup**: Enter your OpenRouter API key below to get started
231
  """)
232
 
233
+ # API Key Input Section
234
+ with gr.Row():
235
+ with gr.Column():
236
+ api_key_input = gr.Textbox(
237
+ label="πŸ”‘ OpenRouter API Key",
238
+ placeholder="Enter your OpenRouter API key here...",
239
+ type="password",
240
+ info="Your API key is not stored and only used for this session"
241
+ )
242
+ api_status = gr.Textbox(
243
+ label="Connection Status",
244
+ value="❌ API Key not provided",
245
+ interactive=False
246
+ )
247
+
248
  with gr.Row():
249
  with gr.Column(scale=1):
250
  # Input components
 
277
  type="filepath"
278
  )
279
 
280
+ submit_btn = gr.Button("πŸš€ Send", variant="primary", size="lg", interactive=False)
281
  clear_btn = gr.Button("πŸ—‘οΈ Clear", variant="secondary")
282
 
283
  with gr.Column(scale=2):
 
289
  )
290
 
291
  # Event handlers
292
+ def validate_api_key(api_key):
293
+ if not api_key or len(api_key.strip()) == 0:
294
+ return "❌ API Key not provided", gr.update(interactive=False)
295
+
296
+ try:
297
+ # Test the API key by creating a client
298
+ test_client = OpenAI(
299
+ base_url="https://openrouter.ai/api/v1",
300
+ api_key=api_key.strip(),
301
+ )
302
+ return "βœ… API Key validated successfully", gr.update(interactive=True)
303
+ except Exception as e:
304
+ return f"❌ API Key validation failed: {str(e)}", gr.update(interactive=False)
305
+
306
+ def process_input(api_key, text, pdf, audio, image, video, history):
307
+ if not api_key or len(api_key.strip()) == 0:
308
+ if history is None:
309
+ history = []
310
+ history.append(("Error", "❌ Please provide a valid API key first"))
311
+ return history, ""
312
+
313
+ # Initialize chatbot with the provided API key
314
+ chatbot = MultimodalChatbot(api_key.strip())
315
  return chatbot.chat(text, pdf, audio, image, video, history)
316
 
317
  def clear_all():
318
  return [], "", None, None, None, None
319
 
320
+ # API Key validation
321
+ api_key_input.change(
322
+ validate_api_key,
323
+ inputs=[api_key_input],
324
+ outputs=[api_status, submit_btn]
325
+ )
326
+
327
  # Button events
328
  submit_btn.click(
329
  process_input,
330
+ inputs=[api_key_input, text_input, pdf_input, audio_input, image_input, video_input, chatbot_interface],
331
  outputs=[chatbot_interface, text_input]
332
  )
333
 
 
339
  # Enter key support
340
  text_input.submit(
341
  process_input,
342
+ inputs=[api_key_input, text_input, pdf_input, audio_input, image_input, video_input, chatbot_interface],
343
  outputs=[chatbot_interface, text_input]
344
  )
345
 
346
  # Examples
347
  gr.Markdown("""
348
  ### 🎯 Example Usage:
349
+ 1. **First**: Enter your OpenRouter API key in the field above
350
+ 2. **Then try these examples**:
351
+ - Upload a PDF and ask "Summarize this document"
352
+ - Upload an image and ask "What do you see in this image?"
353
+ - Record audio and ask "What did I say?"
354
+ - Upload a video and ask "Describe what's happening"
355
+ - Combine multiple inputs: "Compare this image with the PDF content"
356
+
357
+ ### πŸ”‘ Getting an API Key:
358
+ 1. Go to [OpenRouter.ai](https://openrouter.ai)
359
+ 2. Sign up for an account
360
+ 3. Navigate to the API Keys section
361
+ 4. Create a new API key
362
+ 5. Copy and paste it in the field above
363
  """)
364
 
365
  return demo
 
376
  "numpy"
377
  ]
378
 
379
+ print("πŸš€ Multimodal Chatbot with Gemma 3n")
380
+ print("=" * 50)
381
  print("Required packages:", ", ".join(required_packages))
382
+ print("\nπŸ“¦ To install: pip install " + " ".join(required_packages))
383
+ print("\nπŸ”‘ Get your API key from: https://openrouter.ai")
384
+ print("πŸ’‘ Enter your API key in the web interface when it loads")
385
 
386
  demo = create_interface()
387
  demo.launch(
388
+ share=True,
389
+ server_name="0.0.0.0",
390
+ server_port=7860,
391
+ debug=True
392
  )