akhaliq HF Staff commited on
Commit
ae2655c
·
verified ·
1 Parent(s): 819074c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -34
app.py CHANGED
@@ -1,17 +1,16 @@
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["API_KEY"])
8
 
9
  # Create the model
10
  generation_config = {
11
- "temperature": 1,
12
  "top_p": 0.95,
13
  "top_k": 40,
14
- "max_output_tokens": 8192,
15
  }
16
 
17
  model = genai.GenerativeModel(
@@ -20,43 +19,23 @@ model = genai.GenerativeModel(
20
  )
21
 
22
  # Initialize the chat session
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 = []
37
- if files:
38
- processed_files = [process_file(file) for file in files if file is not None]
39
-
40
- if processed_files:
41
- response = chat_session.send_message([message] + processed_files)
42
- else:
43
- response = chat_session.send_message(message)
44
-
45
- chat_history.append((message, response.text))
46
- return "", chat_history
47
 
 
 
 
 
 
 
48
  with gr.Blocks() as demo:
49
- gr.Markdown("# Gemini Multimodal Chatbot")
50
- gr.Markdown("Chat with the Gemini 1.5 Pro model. You can send text, images, audio, and video!")
51
 
52
  chatbot = gr.Chatbot()
53
- msg = gr.Textbox()
54
  clear = gr.ClearButton([msg, chatbot])
55
-
56
- file_output = gr.File()
57
- upload_button = gr.UploadButton("📁 Upload files", file_types=["image", "audio", "video"], file_count="multiple")
58
 
59
- msg.submit(respond, [msg, chatbot, upload_button], [msg, chatbot])
60
 
61
  if __name__ == "__main__":
62
  demo.launch()
 
1
  import os
2
  import google.generativeai as genai
3
  import gradio as gr
 
4
 
5
  # Configure the API
6
  genai.configure(api_key=os.environ["API_KEY"])
7
 
8
  # Create the model
9
  generation_config = {
10
+ "temperature": 0.9,
11
  "top_p": 0.95,
12
  "top_k": 40,
13
+ "max_output_tokens": 1024,
14
  }
15
 
16
  model = genai.GenerativeModel(
 
19
  )
20
 
21
  # Initialize the chat session
22
+ chat = model.start_chat(history=[])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ def respond(message, history):
25
+ response = chat.send_message(message)
26
+ history.append((message, response.text))
27
+ return "", history
28
+
29
+ # Create the Gradio interface
30
  with gr.Blocks() as demo:
31
+ gr.Markdown("# Simple Gemini Chatbot")
32
+ gr.Markdown("Chat with the Gemini 1.5 Pro model.")
33
 
34
  chatbot = gr.Chatbot()
35
+ msg = gr.Textbox(label="Your message")
36
  clear = gr.ClearButton([msg, chatbot])
 
 
 
37
 
38
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
39
 
40
  if __name__ == "__main__":
41
  demo.launch()