Update app.py
Browse files
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="
|
| 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.
|
| 29 |
-
return Image.open(file.
|
| 30 |
-
elif file.
|
| 31 |
-
return file.path
|
|
|
|
|
|
|
| 32 |
else:
|
| 33 |
return None
|
| 34 |
|
| 35 |
-
def respond(message,
|
| 36 |
-
|
| 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
|
| 45 |
-
response = chat_session.send_message([
|
| 46 |
else:
|
| 47 |
-
response = chat_session.send_message(
|
| 48 |
|
| 49 |
-
|
|
|
|
| 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 =
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
avatar_images=(None, "https://lh3.googleusercontent.com/d/1pIo02xepBgqt9gMdFkJHSocJfH_A2dqj"),
|
| 59 |
-
render_markdown=True
|
| 60 |
-
)
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 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()
|