ysharma HF staff commited on
Commit
da443a0
·
1 Parent(s): 6152e25

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # For better security practices, retrieve sensitive information like API keys from environment variables.
11
+
12
+ # Fetch an environment variable.
13
+ GOOGLE_API_KEY = os.environ.get('GOOGLE_API_KEY')
14
+ genai.configure(api_key=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
+ # Define the Gradio Blocks interface
84
+ with gr.Blocks() as demo:
85
+ # Add a centered header using HTML
86
+ gr.HTML("<center><h1>Gemini-PRO & Gemini-PRO-Vision API</h1></center>")
87
+
88
+ # Initialize the MultimodalChatbot component
89
+ multi = MultimodalChatbot(value=[], height=800)
90
+
91
+ with gr.Row():
92
+ # Textbox for user input with increased scale for better visibility
93
+ tb = gr.Textbox(scale=4)
94
+
95
+ # Upload button for image files
96
+ up = gr.UploadButton("Upload Image", file_types=["image"], scale=1)
97
+
98
+ # Define the behavior on text submission
99
+ tb.submit(gemini, [tb, up, multi], [multi, tb, up])
100
+
101
+ # Define the behavior on image upload
102
+ # Using chained then() calls to update the upload button's state
103
+ up.upload(lambda: gr.UploadButton("Uploading Image..."), [], up) \
104
+ .then(lambda: gr.UploadButton("Image Uploaded"), [], up) \
105
+ .then(lambda: gr.UploadButton("Upload Image"), [], up)
106
+
107
+ # Launch the demo with a queue to handle multiple users
108
+ demo.queue().launch()