akhaliq HF staff commited on
Commit
a15e449
·
verified ·
1 Parent(s): 3e818ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -26
app.py CHANGED
@@ -1,9 +1,7 @@
1
  import os
2
  import google.generativeai as genai
3
  import gradio as gr
4
- from gradio_multimodalchatbot import MultimodalChatbot
5
  from PIL import Image
6
- import io
7
 
8
  # Configure the API
9
  genai.configure(api_key=os.environ["GEMINI_API_KEY"])
@@ -17,7 +15,7 @@ generation_config = {
17
  }
18
 
19
  model = genai.GenerativeModel(
20
- model_name="Gemini-1.5-Flash-8B-Exp-0924",
21
  generation_config=generation_config,
22
  )
23
 
@@ -25,41 +23,38 @@ model = genai.GenerativeModel(
25
  chat_session = model.start_chat(history=[])
26
 
27
  def process_file(file):
28
- if file.type.startswith('image'):
29
- return Image.open(file.path)
30
- elif file.type.startswith('audio') or file.type.startswith('video'):
31
- return file.path
 
 
32
  else:
33
  return None
34
 
35
- def respond(message, history):
36
- files = []
37
- for file in message.get('files', []):
38
- processed_file = process_file(file['file'])
39
- if processed_file:
40
- files.append(processed_file)
41
-
42
- prompt = message['text']
43
 
44
- if files:
45
- response = chat_session.send_message([prompt, *files])
46
  else:
47
- response = chat_session.send_message(prompt)
48
 
49
- return {"text": response.text, "files": []}
 
50
 
51
  with gr.Blocks() as demo:
52
  gr.Markdown("# Gemini Multimodal Chatbot")
53
  gr.Markdown("Chat with the Gemini 1.5 Pro model. You can send text, images, audio, and video!")
54
 
55
- chatbot = MultimodalChatbot(
56
- height=600,
57
- bubble_full_width=False,
58
- avatar_images=(None, "https://lh3.googleusercontent.com/d/1pIo02xepBgqt9gMdFkJHSocJfH_A2dqj"),
59
- render_markdown=True
60
- )
61
 
62
- chatbot.chat(respond, fill_height=False)
 
 
 
63
 
64
  if __name__ == "__main__":
65
  demo.launch()
 
1
  import os
2
  import google.generativeai as genai
3
  import gradio as gr
 
4
  from PIL import Image
 
5
 
6
  # Configure the API
7
  genai.configure(api_key=os.environ["GEMINI_API_KEY"])
 
15
  }
16
 
17
  model = genai.GenerativeModel(
18
+ model_name="gemini-1.5-pro-latest",
19
  generation_config=generation_config,
20
  )
21
 
 
23
  chat_session = model.start_chat(history=[])
24
 
25
  def process_file(file):
26
+ if file.name.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
27
+ return Image.open(file.name)
28
+ elif file.name.lower().endswith(('.mp3', '.wav', '.ogg')):
29
+ return file.name # Return the file path for audio
30
+ elif file.name.lower().endswith(('.mp4', '.avi', '.mov')):
31
+ return file.name # Return the file path for video
32
  else:
33
  return None
34
 
35
+ def respond(message, chat_history, files):
36
+ processed_files = [process_file(file) for file in files if file is not None]
 
 
 
 
 
 
37
 
38
+ if processed_files:
39
+ response = chat_session.send_message([message, *processed_files])
40
  else:
41
+ response = chat_session.send_message(message)
42
 
43
+ chat_history.append((message, response.text))
44
+ return "", chat_history
45
 
46
  with gr.Blocks() as demo:
47
  gr.Markdown("# Gemini Multimodal Chatbot")
48
  gr.Markdown("Chat with the Gemini 1.5 Pro model. You can send text, images, audio, and video!")
49
 
50
+ chatbot = gr.Chatbot()
51
+ msg = gr.Textbox()
52
+ clear = gr.ClearButton([msg, chatbot])
 
 
 
53
 
54
+ file_output = gr.File()
55
+ upload_button = gr.UploadButton("📁 Upload files", file_types=["image", "audio", "video"], file_count="multiple")
56
+
57
+ msg.submit(respond, [msg, chatbot, upload_button], [msg, chatbot])
58
 
59
  if __name__ == "__main__":
60
  demo.launch()