akhaliq HF staff commited on
Commit
b0e159b
·
verified ·
1 Parent(s): a5451aa

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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"])
10
+
11
+ # Create the model
12
+ generation_config = {
13
+ "temperature": 1,
14
+ "top_p": 0.95,
15
+ "top_k": 40,
16
+ "max_output_tokens": 8192,
17
+ }
18
+
19
+ model = genai.GenerativeModel(
20
+ model_name="gemini-1.5-pro-latest",
21
+ generation_config=generation_config,
22
+ )
23
+
24
+ # Initialize the chat session
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()