|
|
|
|
|
import google.generativeai as genai |
|
import os |
|
import PIL.Image |
|
import gradio as gr |
|
from gradio_multimodalchatbot import MultimodalChatbot |
|
from gradio.data_classes import FileData |
|
|
|
|
|
GG_API_KEY = os.environ.get('GG_API_KEY') |
|
oaiusr = os.environ.get("OAI_USR") |
|
oaipwd = os.environ.get("OAI_PWD") |
|
genai.configure(api_key=GG_API_KEY) |
|
|
|
model = genai.GenerativeModel('gemini-pro') |
|
modelvis = genai.GenerativeModel('gemini-pro-vision') |
|
|
|
def gemini(input, file, chatbot=[]): |
|
messages = [] |
|
print(chatbot) |
|
|
|
|
|
if len(chatbot) != 0: |
|
for user, bot in chatbot: |
|
user, bot = user.text, bot.text |
|
messages.extend([ |
|
{'role': 'user', 'parts': [user]}, |
|
{'role': 'model', 'parts': [bot]} |
|
]) |
|
messages.append({'role': 'user', 'parts': [input]}) |
|
else: |
|
messages.append({'role': 'user', 'parts': [input]}) |
|
|
|
try: |
|
|
|
if file is not None: |
|
with PIL.Image.open(file.name) as img: |
|
message = [{'role': 'user', 'parts': [input, img]}] |
|
response = modelvis.generate_content(message) |
|
gemini_video_resp = response.text |
|
messages.append({'role': 'model', 'parts': [gemini_video_resp]}) |
|
|
|
|
|
user_msg = {"text": input, "files": [{"file": FileData(path=file.name)}]} |
|
bot_msg = {"text": gemini_video_resp, "files": []} |
|
chatbot.append([user_msg, bot_msg]) |
|
else: |
|
response = model.generate_content(messages) |
|
gemini_resp = response.text |
|
|
|
|
|
user_msg = {"text": input, "files": []} |
|
bot_msg = {"text": gemini_resp, "files": []} |
|
chatbot.append([user_msg, bot_msg]) |
|
except Exception as e: |
|
|
|
print(f"An error occurred: {e}") |
|
raise gr.Error(e) |
|
|
|
return chatbot, "", None |
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
|
gr.HTML("<center><h1>Tomoniai's Gemini Chat</h1></center>") |
|
|
|
|
|
multi = MultimodalChatbot(value=[], show_label=False, height=400, show_copy_button=True, bubble_full_width=False, likeable=True) |
|
|
|
multi.avatar_images=["user.png", "botg.png"] |
|
|
|
with gr.Row(): |
|
|
|
tb = gr.Textbox(show_label=False, scale=4) |
|
|
|
|
|
up = gr.UploadButton("Upload Image", file_types=["image"], scale=1) |
|
|
|
|
|
tb.submit(gemini, [tb, up, multi], [multi, tb, up]) |
|
|
|
|
|
|
|
up.upload(lambda: gr.UploadButton("Uploading Image..."), [], up) \ |
|
.then(lambda: gr.UploadButton("Image Uploaded"), [], up) \ |
|
.then(lambda: gr.UploadButton("Upload Image"), [], up) |
|
|
|
demo.queue().launch(auth=(oaiusr, oaipwd),show_api=False) |