Tomoniai commited on
Commit
35e37a6
1 Parent(s): 67c0c9c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # import required packages
3
+ import google.generativeai as genai
4
+ import os
5
+ import PIL.Image
6
+ import gradio as gr
7
+ from gradio_multimodalchatbot import MultimodalChatbot
8
+ from gradio.data_classes import FileData
9
+
10
+ # Fetch an environment variable.
11
+ GG_API_KEY = os.environ.get('GG_API_KEY')
12
+ genai.configure(api_key=GG_API_KEY)
13
+
14
+ model = genai.GenerativeModel('gemini-pro')
15
+ modelvis = genai.GenerativeModel('gemini-pro-vision')
16
+
17
+ def gemini(input, file, chatbot=[]):
18
+ messages = []
19
+ print(chatbot)
20
+
21
+ # Process previous chatbot messages if present
22
+ if len(chatbot) != 0:
23
+ for user, bot in chatbot:
24
+ user, bot = user.text, bot.text
25
+ messages.extend([
26
+ {'role': 'user', 'parts': [user]},
27
+ {'role': 'model', 'parts': [bot]}
28
+ ])
29
+ messages.append({'role': 'user', 'parts': [input]})
30
+ else:
31
+ messages.append({'role': 'user', 'parts': [input]})
32
+
33
+ try:
34
+ # Process image if file is provided
35
+ if file is not None:
36
+ with PIL.Image.open(file.name) as img:
37
+ message = [{'role': 'user', 'parts': [input, img]}]
38
+ response = modelvis.generate_content(message)
39
+ gemini_video_resp = response.text
40
+ messages.append({'role': 'model', 'parts': [gemini_video_resp]})
41
+
42
+ # Construct list of messages in the required format
43
+ user_msg = {"text": input, "files": [{"file": FileData(path=file.name)}]}
44
+ bot_msg = {"text": gemini_video_resp, "files": []}
45
+ chatbot.append([user_msg, bot_msg])
46
+ else:
47
+ response = model.generate_content(messages)
48
+ gemini_resp = response.text
49
+
50
+ # Construct list of messages in the required format
51
+ user_msg = {"text": input, "files": []}
52
+ bot_msg = {"text": gemini_resp, "files": []}
53
+ chatbot.append([user_msg, bot_msg])
54
+ except Exception as e:
55
+ # Handling exceptions and raising error to the modal
56
+ print(f"An error occurred: {e}")
57
+ raise gr.Error(e)
58
+
59
+ return chatbot, "", None
60
+
61
+ # Define the Gradio Blocks interface
62
+ with gr.Blocks() as demo:
63
+ # Add a centered header using HTML
64
+ gr.HTML("<center><h1>Gemini-PRO & Gemini-PRO-Vision API</h1></center>")
65
+
66
+ # Initialize the MultimodalChatbot component
67
+ multi = MultimodalChatbot(value=[], height=800)
68
+
69
+ with gr.Row():
70
+ # Textbox for user input with increased scale for better visibility
71
+ tb = gr.Textbox(scale=4)
72
+
73
+ # Upload button for image files
74
+ up = gr.UploadButton("Upload Image", file_types=["image"], scale=1)
75
+
76
+ # Define the behavior on text submission
77
+ tb.submit(gemini, [tb, up, multi], [multi, tb, up])
78
+
79
+ # Define the behavior on image upload
80
+ # Using chained then() calls to update the upload button's state
81
+ up.upload(lambda: gr.UploadButton("Uploading Image..."), [], up) \
82
+ .then(lambda: gr.UploadButton("Image Uploaded"), [], up) \
83
+ .then(lambda: gr.UploadButton("Upload Image"), [], up)
84
+
85
+ # Launch the demo with a queue to handle multiple users
86
+ demo.queue().launch()