Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Configure the Gemini API
|
| 7 |
+
genai.configure(api_key=os.environ.get("AIzaSyCFdxcKVO6VSxEBaNE2W3LIvRLPEPpyMGw"))
|
| 8 |
+
|
| 9 |
+
# Set up the model
|
| 10 |
+
model = genai.GenerativeModel('gemini-pro-vision')
|
| 11 |
+
|
| 12 |
+
def chat_with_gemini(history, user_message, image):
|
| 13 |
+
history = history or []
|
| 14 |
+
|
| 15 |
+
if image is not None:
|
| 16 |
+
# If an image is uploaded, include it in the message
|
| 17 |
+
response = model.generate_content([user_message, image])
|
| 18 |
+
else:
|
| 19 |
+
# Text-only message
|
| 20 |
+
response = model.generate_content(user_message)
|
| 21 |
+
|
| 22 |
+
history.append((user_message, response.text))
|
| 23 |
+
return history, history
|
| 24 |
+
|
| 25 |
+
def clear_conversation():
|
| 26 |
+
return None
|
| 27 |
+
|
| 28 |
+
# Define the Gradio interface
|
| 29 |
+
with gr.Blocks() as demo:
|
| 30 |
+
chatbot = gr.Chatbot(label="Chat with Gemini")
|
| 31 |
+
msg = gr.Textbox(label="Type your message here")
|
| 32 |
+
clear = gr.Button("Clear")
|
| 33 |
+
image_upload = gr.Image(type="pil", label="Upload an image (optional)")
|
| 34 |
+
|
| 35 |
+
msg.submit(chat_with_gemini, [chatbot, msg, image_upload], [chatbot, chatbot])
|
| 36 |
+
clear.click(clear_conversation, outputs=[chatbot])
|
| 37 |
+
|
| 38 |
+
# Launch the app
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
demo.launch()
|
| 41 |
+
|
| 42 |
+
# Requirements for Hugging Face Spaces
|
| 43 |
+
# requirements.txt
|
| 44 |
+
'''
|
| 45 |
+
gradio==3.50.2
|
| 46 |
+
google-generativeai==0.3.1
|
| 47 |
+
Pillow==10.0.0
|
| 48 |
+
'''
|