Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# import required packages
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
import os
|
| 5 |
+
import PIL.Image
|
| 6 |
+
from decouple import config
|
| 7 |
+
import gradio as gr
|
| 8 |
+
from gradio_multimodalchatbot import MultimodalChatbot
|
| 9 |
+
from gradio.data_classes import FileData
|
| 10 |
+
|
| 11 |
+
# For better security practices, retrieve sensitive information like API keys from environment variables.
|
| 12 |
+
|
| 13 |
+
# Fetch an environment variable.
|
| 14 |
+
genai.configure(api_key=config("GOOGLE_API_KEY"))
|
| 15 |
+
|
| 16 |
+
# These codelines are just to verify if your api key is correct or not
|
| 17 |
+
# Use them when you clone the repo and build locally
|
| 18 |
+
#!curl \
|
| 19 |
+
#-H 'Content-Type: application/json' \
|
| 20 |
+
#-d '{ "prompt": { "text": "Write a very short story about a magic backpack"} }' \
|
| 21 |
+
#"https://generativelanguage.googleapis.com/v1beta3/models/text-bison-001:generateText?key=<enter-your-key-here>"
|
| 22 |
+
|
| 23 |
+
# Initialize genai models
|
| 24 |
+
model = genai.GenerativeModel('gemini-pro')
|
| 25 |
+
modelvis = genai.GenerativeModel('gemini-pro-vision')
|
| 26 |
+
|
| 27 |
+
def gemini(input, file, chatbot=[]):
|
| 28 |
+
"""
|
| 29 |
+
Function to handle gemini model and gemini vision model interactions.
|
| 30 |
+
|
| 31 |
+
Parameters:
|
| 32 |
+
input (str): The input text.
|
| 33 |
+
file (File): An optional file object for image processing.
|
| 34 |
+
chatbot (list): A list to keep track of chatbot interactions.
|
| 35 |
+
|
| 36 |
+
Returns:
|
| 37 |
+
tuple: Updated chatbot interaction list, an empty string, and None.
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
messages = []
|
| 41 |
+
print(chatbot)
|
| 42 |
+
|
| 43 |
+
# Process previous chatbot messages if present
|
| 44 |
+
if len(chatbot) != 0:
|
| 45 |
+
for user, bot in chatbot:
|
| 46 |
+
user, bot = user.text, bot.text
|
| 47 |
+
messages.extend([
|
| 48 |
+
{'role': 'user', 'parts': [user]},
|
| 49 |
+
{'role': 'model', 'parts': [bot]}
|
| 50 |
+
])
|
| 51 |
+
messages.append({'role': 'user', 'parts': [input]})
|
| 52 |
+
else:
|
| 53 |
+
messages.append({'role': 'user', 'parts': [input]})
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
# Process image if file is provided
|
| 57 |
+
if file is not None:
|
| 58 |
+
with PIL.Image.open(file.name) as img:
|
| 59 |
+
message = [{'role': 'user', 'parts': [input, img]}]
|
| 60 |
+
response = modelvis.generate_content(message)
|
| 61 |
+
gemini_video_resp = response.text
|
| 62 |
+
messages.append({'role': 'model', 'parts': [gemini_video_resp]})
|
| 63 |
+
|
| 64 |
+
# Construct list of messages in the required format
|
| 65 |
+
user_msg = {"text": input, "files": [{"file": FileData(path=file.name)}]}
|
| 66 |
+
bot_msg = {"text": gemini_video_resp, "files": []}
|
| 67 |
+
chatbot.append([user_msg, bot_msg])
|
| 68 |
+
else:
|
| 69 |
+
response = model.generate_content(messages)
|
| 70 |
+
gemini_resp = response.text
|
| 71 |
+
|
| 72 |
+
# Construct list of messages in the required format
|
| 73 |
+
user_msg = {"text": input, "files": []}
|
| 74 |
+
bot_msg = {"text": gemini_resp, "files": []}
|
| 75 |
+
chatbot.append([user_msg, bot_msg])
|
| 76 |
+
except Exception as e:
|
| 77 |
+
# Handling exceptions and raising error to the modal
|
| 78 |
+
print(f"An error occurred: {e}")
|
| 79 |
+
raise gr.Error(e)
|
| 80 |
+
|
| 81 |
+
return chatbot, "", None
|
| 82 |
+
|
| 83 |
+
def greet(name):
|
| 84 |
+
return "Hello " + name + "!"
|
| 85 |
+
|
| 86 |
+
'''demo = gr.Interface(
|
| 87 |
+
fn=greet,
|
| 88 |
+
inputs="text",
|
| 89 |
+
outputs="text",
|
| 90 |
+
css="footer {visibility: hidden}"
|
| 91 |
+
)'''
|
| 92 |
+
|
| 93 |
+
# Define the Gradio Blocks interface
|
| 94 |
+
with gr.Blocks() as demo:
|
| 95 |
+
# Add a centered header using HTML
|
| 96 |
+
gr.HTML("<center><h1>Alpaca-PRO-Vision API</h1></center>")
|
| 97 |
+
|
| 98 |
+
# Initialize the MultimodalChatbot component
|
| 99 |
+
multi = MultimodalChatbot(value=[], height=800)
|
| 100 |
+
|
| 101 |
+
with gr.Row():
|
| 102 |
+
# Textbox for user input with increased scale for better visibility
|
| 103 |
+
tb = gr.Textbox(scale=4, placeholder='Input text and press Enter')
|
| 104 |
+
|
| 105 |
+
# Upload button for image files
|
| 106 |
+
up = gr.UploadButton("Upload Image", file_types=["image"], scale=1)
|
| 107 |
+
|
| 108 |
+
# Define the behavior on text submission
|
| 109 |
+
tb.submit(gemini, [tb, up, multi], [multi, tb, up])
|
| 110 |
+
|
| 111 |
+
# Define the behavior on image upload
|
| 112 |
+
# Using chained then() calls to update the upload button's state
|
| 113 |
+
up.upload(lambda: gr.UploadButton("Uploading Image..."), [], up) \
|
| 114 |
+
.then(lambda: gr.UploadButton("Image Uploaded"), [], up) \
|
| 115 |
+
.then(lambda: gr.UploadButton("Upload Image"), [], up)
|
| 116 |
+
|
| 117 |
+
# Launch the demo with a queue to handle multiple users
|
| 118 |
+
demo.queue().launch(share=True)
|