eaglelandsonce commited on
Commit
e6b4540
1 Parent(s): 178cd49

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +280 -0
app.py ADDED
@@ -0,0 +1,280 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+ import uuid
4
+ from typing import List, Tuple, Optional, Dict, Union
5
+
6
+ import google.generativeai as genai
7
+ import gradio as gr
8
+ from PIL import Image
9
+
10
+ print("google-generativeai:", genai.__version__)
11
+
12
+ GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
13
+
14
+ TITLE = """<h1 align="center">Gemini Playground 💬</h1>"""
15
+ SUBTITLE = """<h2 align="center">Play with Gemini Pro and Gemini Pro Vision API</h2>"""
16
+ DUPLICATE = """
17
+ <div style="text-align: center; display: flex; justify-content: center; align-items: center;">
18
+ <a href="https://huggingface.co/spaces/SkalskiP/ChatGemini?duplicate=true">
19
+ <img src="https://bit.ly/3gLdBN6" alt="Duplicate Space" style="margin-right: 10px;">
20
+ </a>
21
+ <span>Duplicate the Space and run securely with your
22
+ <a href="https://makersuite.google.com/app/apikey">GOOGLE API KEY</a>.
23
+ </span>
24
+ </div>
25
+ """
26
+
27
+ AVATAR_IMAGES = (
28
+ None,
29
+ "https://media.roboflow.com/spaces/gemini-icon.png"
30
+ )
31
+
32
+ IMAGE_CACHE_DIRECTORY = "/tmp"
33
+ IMAGE_WIDTH = 512
34
+ CHAT_HISTORY = List[Tuple[Optional[Union[Tuple[str], str]], Optional[str]]]
35
+
36
+
37
+ def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
38
+ if not stop_sequences:
39
+ return None
40
+ return [sequence.strip() for sequence in stop_sequences.split(",")]
41
+
42
+
43
+ def preprocess_image(image: Image.Image) -> Optional[Image.Image]:
44
+ image_height = int(image.height * IMAGE_WIDTH / image.width)
45
+ return image.resize((IMAGE_WIDTH, image_height))
46
+
47
+
48
+ def cache_pil_image(image: Image.Image) -> str:
49
+ image_filename = f"{uuid.uuid4()}.jpeg"
50
+ os.makedirs(IMAGE_CACHE_DIRECTORY, exist_ok=True)
51
+ image_path = os.path.join(IMAGE_CACHE_DIRECTORY, image_filename)
52
+ image.save(image_path, "JPEG")
53
+ return image_path
54
+
55
+
56
+ def preprocess_chat_history(
57
+ history: CHAT_HISTORY
58
+ ) -> List[Dict[str, Union[str, List[str]]]]:
59
+ messages = []
60
+ for user_message, model_message in history:
61
+ if isinstance(user_message, tuple):
62
+ pass
63
+ elif user_message is not None:
64
+ messages.append({'role': 'user', 'parts': [user_message]})
65
+ if model_message is not None:
66
+ messages.append({'role': 'model', 'parts': [model_message]})
67
+ return messages
68
+
69
+
70
+ def upload(files: Optional[List[str]], chatbot: CHAT_HISTORY) -> CHAT_HISTORY:
71
+ for file in files:
72
+ image = Image.open(file).convert('RGB')
73
+ image = preprocess_image(image)
74
+ image_path = cache_pil_image(image)
75
+ chatbot.append(((image_path,), None))
76
+ return chatbot
77
+
78
+
79
+ def user(text_prompt: str, chatbot: CHAT_HISTORY):
80
+ if text_prompt:
81
+ chatbot.append((text_prompt, None))
82
+ return "", chatbot
83
+
84
+
85
+ def bot(
86
+ google_key: str,
87
+ files: Optional[List[str]],
88
+ temperature: float,
89
+ max_output_tokens: int,
90
+ stop_sequences: str,
91
+ top_k: int,
92
+ top_p: float,
93
+ chatbot: CHAT_HISTORY
94
+ ):
95
+ if len(chatbot) == 0:
96
+ return chatbot
97
+
98
+ google_key = google_key if google_key else GOOGLE_API_KEY
99
+ if not google_key:
100
+ raise ValueError(
101
+ "GOOGLE_API_KEY is not set. "
102
+ "Please follow the instructions in the README to set it up.")
103
+
104
+ genai.configure(api_key=google_key)
105
+ generation_config = genai.types.GenerationConfig(
106
+ temperature=temperature,
107
+ max_output_tokens=max_output_tokens,
108
+ stop_sequences=preprocess_stop_sequences(stop_sequences=stop_sequences),
109
+ top_k=top_k,
110
+ top_p=top_p)
111
+
112
+ if files:
113
+ text_prompt = [chatbot[-1][0]] \
114
+ if chatbot[-1][0] and isinstance(chatbot[-1][0], str) \
115
+ else []
116
+ image_prompt = [Image.open(file).convert('RGB') for file in files]
117
+ model = genai.GenerativeModel('gemini-pro-vision')
118
+ response = model.generate_content(
119
+ text_prompt + image_prompt,
120
+ stream=True,
121
+ generation_config=generation_config)
122
+ else:
123
+ messages = preprocess_chat_history(chatbot)
124
+ model = genai.GenerativeModel('gemini-pro')
125
+ response = model.generate_content(
126
+ messages,
127
+ stream=True,
128
+ generation_config=generation_config)
129
+
130
+ # streaming effect
131
+ chatbot[-1][1] = ""
132
+ for chunk in response:
133
+ for i in range(0, len(chunk.text), 10):
134
+ section = chunk.text[i:i + 10]
135
+ chatbot[-1][1] += section
136
+ time.sleep(0.01)
137
+ yield chatbot
138
+
139
+
140
+ google_key_component = gr.Textbox(
141
+ label="GOOGLE API KEY",
142
+ value="",
143
+ type="password",
144
+ placeholder="...",
145
+ info="You have to provide your own GOOGLE_API_KEY for this app to function properly",
146
+ visible=GOOGLE_API_KEY is None
147
+ )
148
+ chatbot_component = gr.Chatbot(
149
+ label='Gemini',
150
+ bubble_full_width=False,
151
+ avatar_images=AVATAR_IMAGES,
152
+ scale=2,
153
+ height=400
154
+ )
155
+ text_prompt_component = gr.Textbox(
156
+ placeholder="Hi there! [press Enter]", show_label=False, autofocus=True, scale=8
157
+ )
158
+ upload_button_component = gr.UploadButton(
159
+ label="Upload Images", file_count="multiple", file_types=["image"], scale=1
160
+ )
161
+ run_button_component = gr.Button(value="Run", variant="primary", scale=1)
162
+ temperature_component = gr.Slider(
163
+ minimum=0,
164
+ maximum=1.0,
165
+ value=0.4,
166
+ step=0.05,
167
+ label="Temperature",
168
+ info=(
169
+ "Temperature controls the degree of randomness in token selection. Lower "
170
+ "temperatures are good for prompts that expect a true or correct response, "
171
+ "while higher temperatures can lead to more diverse or unexpected results. "
172
+ ))
173
+ max_output_tokens_component = gr.Slider(
174
+ minimum=1,
175
+ maximum=2048,
176
+ value=1024,
177
+ step=1,
178
+ label="Token limit",
179
+ info=(
180
+ "Token limit determines the maximum amount of text output from one prompt. A "
181
+ "token is approximately four characters. The default value is 2048."
182
+ ))
183
+ stop_sequences_component = gr.Textbox(
184
+ label="Add stop sequence",
185
+ value="",
186
+ type="text",
187
+ placeholder="STOP, END",
188
+ info=(
189
+ "A stop sequence is a series of characters (including spaces) that stops "
190
+ "response generation if the model encounters it. The sequence is not included "
191
+ "as part of the response. You can add up to five stop sequences."
192
+ ))
193
+ top_k_component = gr.Slider(
194
+ minimum=1,
195
+ maximum=40,
196
+ value=32,
197
+ step=1,
198
+ label="Top-K",
199
+ info=(
200
+ "Top-k changes how the model selects tokens for output. A top-k of 1 means the "
201
+ "selected token is the most probable among all tokens in the model’s "
202
+ "vocabulary (also called greedy decoding), while a top-k of 3 means that the "
203
+ "next token is selected from among the 3 most probable tokens (using "
204
+ "temperature)."
205
+ ))
206
+ top_p_component = gr.Slider(
207
+ minimum=0,
208
+ maximum=1,
209
+ value=1,
210
+ step=0.01,
211
+ label="Top-P",
212
+ info=(
213
+ "Top-p changes how the model selects tokens for output. Tokens are selected "
214
+ "from most probable to least until the sum of their probabilities equals the "
215
+ "top-p value. For example, if tokens A, B, and C have a probability of .3, .2, "
216
+ "and .1 and the top-p value is .5, then the model will select either A or B as "
217
+ "the next token (using temperature). "
218
+ ))
219
+
220
+ user_inputs = [
221
+ text_prompt_component,
222
+ chatbot_component
223
+ ]
224
+
225
+ bot_inputs = [
226
+ google_key_component,
227
+ upload_button_component,
228
+ temperature_component,
229
+ max_output_tokens_component,
230
+ stop_sequences_component,
231
+ top_k_component,
232
+ top_p_component,
233
+ chatbot_component
234
+ ]
235
+
236
+ with gr.Blocks() as demo:
237
+ gr.HTML(TITLE)
238
+ gr.HTML(SUBTITLE)
239
+ gr.HTML(DUPLICATE)
240
+ with gr.Column():
241
+ google_key_component.render()
242
+ chatbot_component.render()
243
+ with gr.Row():
244
+ text_prompt_component.render()
245
+ upload_button_component.render()
246
+ run_button_component.render()
247
+ with gr.Accordion("Parameters", open=False):
248
+ temperature_component.render()
249
+ max_output_tokens_component.render()
250
+ stop_sequences_component.render()
251
+ with gr.Accordion("Advanced", open=False):
252
+ top_k_component.render()
253
+ top_p_component.render()
254
+
255
+ run_button_component.click(
256
+ fn=user,
257
+ inputs=user_inputs,
258
+ outputs=[text_prompt_component, chatbot_component],
259
+ queue=False
260
+ ).then(
261
+ fn=bot, inputs=bot_inputs, outputs=[chatbot_component],
262
+ )
263
+
264
+ text_prompt_component.submit(
265
+ fn=user,
266
+ inputs=user_inputs,
267
+ outputs=[text_prompt_component, chatbot_component],
268
+ queue=False
269
+ ).then(
270
+ fn=bot, inputs=bot_inputs, outputs=[chatbot_component],
271
+ )
272
+
273
+ upload_button_component.upload(
274
+ fn=upload,
275
+ inputs=[upload_button_component, chatbot_component],
276
+ outputs=[chatbot_component],
277
+ queue=False
278
+ )
279
+
280
+ demo.queue(max_size=99).launch(debug=False, show_error=True)