NotASI commited on
Commit
fed1f3d
·
1 Parent(s): c0d0de3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -247
app.py CHANGED
@@ -1,223 +1,3 @@
1
- # """
2
- # References:
3
- # - https://medium.com/@turna.fardousi/building-a-multimodal-chatbot-with-gemini-api-8015bfbee538
4
- # """
5
-
6
- # import os
7
- # import time
8
- # from typing import List, Tuple, Optional
9
- # import google.generativeai as genai
10
- # import gradio as gr
11
- # from PIL import Image
12
- # from dotenv import load_dotenv
13
-
14
- # load_dotenv()
15
-
16
- # GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
17
-
18
- # TITLE = """<h1 align="center">🎮Chat with Gemini 1.5🔥 -- Beta Preview</h1>"""
19
- # NOTICE = """
20
- # Notices 📜:
21
- # - This app is still in development
22
- # - Some features may not work as expected
23
- # """
24
- # ABOUT = """
25
- # Updates (2024-8-12): Created the App
26
-
27
- # Info:
28
- # - Model: Gemini 1.5 Flash
29
- # """
30
- # ERRORS = """
31
- # Known errors ⚠️:
32
- # """
33
- # FUTURE_IMPLEMENTATIONS = """
34
- # To be implemented 🚀:
35
- # - Select other Gemini / Gemma models
36
- # - Upload files
37
- # - More tools other than web search
38
- # """
39
- # IMAGE_WIDTH = 512
40
-
41
- # def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
42
- # return [seq.strip() for seq in stop_sequences.split(",")] if stop_sequences else None
43
-
44
- # def preprocess_image(image: Image.Image) -> Image.Image:
45
- # image_height = int(image.height * IMAGE_WIDTH / image.width)
46
- # return image.resize((IMAGE_WIDTH, image_height))
47
-
48
- # def user(text_prompt: str, chatbot: List[Tuple[str, str]]):
49
- # return "", chatbot + [[text_prompt, None]]
50
-
51
- # def bot(
52
- # google_key: str,
53
- # image_prompt: Optional[Image.Image],
54
- # temperature: float,
55
- # max_output_tokens: int,
56
- # stop_sequences: str,
57
- # top_k: int,
58
- # top_p: float,
59
- # chatbot: List[Tuple[str, str]]
60
- # ):
61
- # google_key = google_key or GEMINI_API_KEY
62
- # if not google_key:
63
- # raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
64
-
65
- # text_prompt = chatbot[-1][0]
66
- # genai.configure(api_key=google_key)
67
- # generation_config = genai.types.GenerationConfig(
68
- # temperature=temperature,
69
- # max_output_tokens=max_output_tokens,
70
- # stop_sequences=preprocess_stop_sequences(stop_sequences),
71
- # top_k=top_k,
72
- # top_p=top_p,
73
- # )
74
-
75
- # model_name = "gemini-1.5-flash" # if image_prompt is None else "gemini-pro-vision"
76
- # model = genai.GenerativeModel(model_name)
77
- # inputs = [text_prompt] if image_prompt is None else [text_prompt, preprocess_image(image_prompt)]
78
-
79
- # response = model.generate_content(inputs, stream=True, generation_config=generation_config)
80
- # response.resolve()
81
-
82
- # chatbot[-1][1] = ""
83
- # for chunk in response:
84
- # for i in range(0, len(chunk.text), 10):
85
- # chatbot[-1][1] += chunk.text[i:i + 10]
86
- # time.sleep(0.01)
87
- # yield chatbot
88
-
89
- # google_key_component = gr.Textbox(
90
- # label = "GOOGLE API KEY",
91
- # type = "password",
92
- # placeholder = "...",
93
- # visible = GEMINI_API_KEY is None
94
- # )
95
-
96
- # image_prompt_component = gr.Image(
97
- # type = "pil",
98
- # label = "Image"
99
- # )
100
- # chatbot_component = gr.Chatbot(
101
- # # label = 'Gemini',
102
- # bubble_full_width = False
103
- # )
104
- # text_prompt_component = gr.Textbox(
105
- # placeholder = "Chat with Gemini",
106
- # label = "Ask me anything and press Enter"
107
- # )
108
- # run_button_component = gr.Button(
109
- # "Run"
110
- # )
111
- # temperature_component = gr.Slider(
112
- # minimum = 0,
113
- # maximum = 1.0,
114
- # value = 0.5,
115
- # step = 0.05,
116
- # label = "Temperature"
117
- # )
118
- # max_output_tokens_component = gr.Slider(
119
- # minimum = 1,
120
- # maximum = 8192,
121
- # value = 4096,
122
- # step = 1,
123
- # label = "Max Output Tokens"
124
- # )
125
- # stop_sequences_component = gr.Textbox(
126
- # label = "Add stop sequence",
127
- # placeholder = "STOP, END"
128
- # )
129
- # top_k_component = gr.Slider(
130
- # minimum = 1,
131
- # maximum = 40,
132
- # value = 32,
133
- # step = 1,
134
- # label = "Top-K"
135
- # )
136
- # top_p_component = gr.Slider(
137
- # minimum = 0,
138
- # maximum = 1,
139
- # value = 1,
140
- # step = 0.01,
141
- # label = "Top-P"
142
- # )
143
-
144
- # user_inputs = [
145
- # text_prompt_component,
146
- # chatbot_component
147
- # ]
148
- # bot_inputs = [
149
- # google_key_component,
150
- # image_prompt_component,
151
- # temperature_component,
152
- # max_output_tokens_component,
153
- # stop_sequences_component,
154
- # top_k_component,
155
- # top_p_component,
156
- # chatbot_component
157
- # ]
158
-
159
- # with gr.Blocks(theme = gr.themes.Soft()) as demo:
160
- # with gr.Tab("Chat with Gemini 1.5 Flash"):
161
- # gr.HTML(TITLE)
162
- # with gr.Row():
163
- # gr.Markdown(NOTICE)
164
- # gr.Markdown(ABOUT)
165
- # gr.Markdown(ERRORS)
166
- # gr.Markdown(FUTURE_IMPLEMENTATIONS)
167
- # with gr.Column():
168
- # google_key_component.render()
169
- # with gr.Row():
170
- # image_prompt_component.render()
171
- # chatbot_component.render()
172
- # text_prompt_component.render()
173
- # run_button_component.render()
174
- # with gr.Accordion("Parameters", open=False):
175
- # temperature_component.render()
176
- # max_output_tokens_component.render()
177
- # stop_sequences_component.render()
178
- # with gr.Accordion("Advanced", open=False):
179
- # top_k_component.render()
180
- # top_p_component.render()
181
-
182
- # run_button_component.click(
183
- # fn = user,
184
- # inputs = user_inputs,
185
- # outputs = [
186
- # text_prompt_component,
187
- # chatbot_component
188
- # ],
189
- # queue = False
190
- # ).then(
191
- # fn = bot,
192
- # inputs = bot_inputs,
193
- # outputs = [
194
- # chatbot_component
195
- # ]
196
- # )
197
- # text_prompt_component.submit(
198
- # fn = user,
199
- # inputs = user_inputs,
200
- # outputs = [
201
- # text_prompt_component,
202
- # chatbot_component
203
- # ],
204
- # queue = False
205
- # ).then(
206
- # fn = bot,
207
- # inputs = bot_inputs,
208
- # outputs = [
209
- # chatbot_component
210
- # ]
211
- # )
212
- # with gr.Tab("Chat with Gemma 2"):
213
- # gr.HTML(
214
- # """
215
- # <h1 align="center">Still in development</h1>
216
- # """
217
- # )
218
-
219
- # demo.queue().launch(debug = True, show_error = True)
220
-
221
  """
222
  References:
223
  - https://medium.com/@turna.fardousi/building-a-multimodal-chatbot-with-gemini-api-8015bfbee538
@@ -265,8 +45,8 @@ def preprocess_image(image: Image.Image) -> Image.Image:
265
  image_height = int(image.height * IMAGE_WIDTH / image.width)
266
  return image.resize((IMAGE_WIDTH, image_height))
267
 
268
- def user(text_prompt: str, history: List[Tuple[str, str]]):
269
- return text_prompt, history
270
 
271
  def bot(
272
  google_key: str,
@@ -276,13 +56,13 @@ def bot(
276
  stop_sequences: str,
277
  top_k: int,
278
  top_p: float,
279
- history: List[Tuple[str, str]]
280
  ):
281
  google_key = google_key or GEMINI_API_KEY
282
  if not google_key:
283
  raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
284
 
285
- text_prompt = history[-1][0]
286
  genai.configure(api_key=google_key)
287
  generation_config = genai.types.GenerationConfig(
288
  temperature=temperature,
@@ -292,19 +72,19 @@ def bot(
292
  top_p=top_p,
293
  )
294
 
295
- model_name = "gemini-1.5-flash"
296
  model = genai.GenerativeModel(model_name)
297
  inputs = [text_prompt] if image_prompt is None else [text_prompt, preprocess_image(image_prompt)]
298
 
299
  response = model.generate_content(inputs, stream=True, generation_config=generation_config)
300
  response.resolve()
301
 
302
- output_text = ""
303
  for chunk in response:
304
  for i in range(0, len(chunk.text), 10):
305
- output_text += chunk.text[i:i + 10]
306
  time.sleep(0.01)
307
- yield history + [(text_prompt, output_text)]
308
 
309
  google_key_component = gr.Textbox(
310
  label = "GOOGLE API KEY",
@@ -317,12 +97,17 @@ image_prompt_component = gr.Image(
317
  type = "pil",
318
  label = "Image"
319
  )
320
-
 
 
 
321
  text_prompt_component = gr.Textbox(
322
  placeholder = "Chat with Gemini",
323
  label = "Ask me anything and press Enter"
324
  )
325
-
 
 
326
  temperature_component = gr.Slider(
327
  minimum = 0,
328
  maximum = 1.0,
@@ -330,7 +115,6 @@ temperature_component = gr.Slider(
330
  step = 0.05,
331
  label = "Temperature"
332
  )
333
-
334
  max_output_tokens_component = gr.Slider(
335
  minimum = 1,
336
  maximum = 8192,
@@ -338,12 +122,10 @@ max_output_tokens_component = gr.Slider(
338
  step = 1,
339
  label = "Max Output Tokens"
340
  )
341
-
342
  stop_sequences_component = gr.Textbox(
343
  label = "Add stop sequence",
344
  placeholder = "STOP, END"
345
  )
346
-
347
  top_k_component = gr.Slider(
348
  minimum = 1,
349
  maximum = 40,
@@ -351,7 +133,6 @@ top_k_component = gr.Slider(
351
  step = 1,
352
  label = "Top-K"
353
  )
354
-
355
  top_p_component = gr.Slider(
356
  minimum = 0,
357
  maximum = 1,
@@ -362,9 +143,8 @@ top_p_component = gr.Slider(
362
 
363
  user_inputs = [
364
  text_prompt_component,
365
- gr.State([])
366
  ]
367
-
368
  bot_inputs = [
369
  google_key_component,
370
  image_prompt_component,
@@ -373,7 +153,7 @@ bot_inputs = [
373
  stop_sequences_component,
374
  top_k_component,
375
  top_p_component,
376
- gr.State([])
377
  ]
378
 
379
  with gr.Blocks(theme = gr.themes.Soft()) as demo:
@@ -388,7 +168,9 @@ with gr.Blocks(theme = gr.themes.Soft()) as demo:
388
  google_key_component.render()
389
  with gr.Row():
390
  image_prompt_component.render()
 
391
  text_prompt_component.render()
 
392
  with gr.Accordion("Parameters", open=False):
393
  temperature_component.render()
394
  max_output_tokens_component.render()
@@ -397,16 +179,36 @@ with gr.Blocks(theme = gr.themes.Soft()) as demo:
397
  top_k_component.render()
398
  top_p_component.render()
399
 
400
- chat_interface = gr.ChatInterface(
401
- fn=bot,
402
- user_fn=user,
403
- inputs=bot_inputs,
404
- outputs="chatbot",
405
- submit=text_prompt_component,
406
- state="chatbot",
407
- queue=False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
408
  )
409
-
410
  with gr.Tab("Chat with Gemma 2"):
411
  gr.HTML(
412
  """
@@ -414,4 +216,4 @@ with gr.Blocks(theme = gr.themes.Soft()) as demo:
414
  """
415
  )
416
 
417
- demo.queue().launch(debug = True, show_error = True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  """
2
  References:
3
  - https://medium.com/@turna.fardousi/building-a-multimodal-chatbot-with-gemini-api-8015bfbee538
 
45
  image_height = int(image.height * IMAGE_WIDTH / image.width)
46
  return image.resize((IMAGE_WIDTH, image_height))
47
 
48
+ def user(text_prompt: str, chatbot: List[Tuple[str, str]]):
49
+ return "", chatbot + [[text_prompt, None]]
50
 
51
  def bot(
52
  google_key: str,
 
56
  stop_sequences: str,
57
  top_k: int,
58
  top_p: float,
59
+ chatbot: List[Tuple[str, str]]
60
  ):
61
  google_key = google_key or GEMINI_API_KEY
62
  if not google_key:
63
  raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
64
 
65
+ text_prompt = chatbot[-1][0]
66
  genai.configure(api_key=google_key)
67
  generation_config = genai.types.GenerationConfig(
68
  temperature=temperature,
 
72
  top_p=top_p,
73
  )
74
 
75
+ model_name = "gemini-1.5-flash" # if image_prompt is None else "gemini-pro-vision"
76
  model = genai.GenerativeModel(model_name)
77
  inputs = [text_prompt] if image_prompt is None else [text_prompt, preprocess_image(image_prompt)]
78
 
79
  response = model.generate_content(inputs, stream=True, generation_config=generation_config)
80
  response.resolve()
81
 
82
+ chatbot[-1][1] = ""
83
  for chunk in response:
84
  for i in range(0, len(chunk.text), 10):
85
+ chatbot[-1][1] += chunk.text[i:i + 10]
86
  time.sleep(0.01)
87
+ yield chatbot
88
 
89
  google_key_component = gr.Textbox(
90
  label = "GOOGLE API KEY",
 
97
  type = "pil",
98
  label = "Image"
99
  )
100
+ chatbot_component = gr.Chatbot(
101
+ # label = 'Gemini',
102
+ bubble_full_width = False
103
+ )
104
  text_prompt_component = gr.Textbox(
105
  placeholder = "Chat with Gemini",
106
  label = "Ask me anything and press Enter"
107
  )
108
+ run_button_component = gr.Button(
109
+ "Run"
110
+ )
111
  temperature_component = gr.Slider(
112
  minimum = 0,
113
  maximum = 1.0,
 
115
  step = 0.05,
116
  label = "Temperature"
117
  )
 
118
  max_output_tokens_component = gr.Slider(
119
  minimum = 1,
120
  maximum = 8192,
 
122
  step = 1,
123
  label = "Max Output Tokens"
124
  )
 
125
  stop_sequences_component = gr.Textbox(
126
  label = "Add stop sequence",
127
  placeholder = "STOP, END"
128
  )
 
129
  top_k_component = gr.Slider(
130
  minimum = 1,
131
  maximum = 40,
 
133
  step = 1,
134
  label = "Top-K"
135
  )
 
136
  top_p_component = gr.Slider(
137
  minimum = 0,
138
  maximum = 1,
 
143
 
144
  user_inputs = [
145
  text_prompt_component,
146
+ chatbot_component
147
  ]
 
148
  bot_inputs = [
149
  google_key_component,
150
  image_prompt_component,
 
153
  stop_sequences_component,
154
  top_k_component,
155
  top_p_component,
156
+ chatbot_component
157
  ]
158
 
159
  with gr.Blocks(theme = gr.themes.Soft()) as demo:
 
168
  google_key_component.render()
169
  with gr.Row():
170
  image_prompt_component.render()
171
+ chatbot_component.render()
172
  text_prompt_component.render()
173
+ run_button_component.render()
174
  with gr.Accordion("Parameters", open=False):
175
  temperature_component.render()
176
  max_output_tokens_component.render()
 
179
  top_k_component.render()
180
  top_p_component.render()
181
 
182
+ run_button_component.click(
183
+ fn = user,
184
+ inputs = user_inputs,
185
+ outputs = [
186
+ text_prompt_component,
187
+ chatbot_component
188
+ ],
189
+ queue = False
190
+ ).then(
191
+ fn = bot,
192
+ inputs = bot_inputs,
193
+ outputs = [
194
+ chatbot_component
195
+ ]
196
+ )
197
+ text_prompt_component.submit(
198
+ fn = user,
199
+ inputs = user_inputs,
200
+ outputs = [
201
+ text_prompt_component,
202
+ chatbot_component
203
+ ],
204
+ queue = False
205
+ ).then(
206
+ fn = bot,
207
+ inputs = bot_inputs,
208
+ outputs = [
209
+ chatbot_component
210
+ ]
211
  )
 
212
  with gr.Tab("Chat with Gemma 2"):
213
  gr.HTML(
214
  """
 
216
  """
217
  )
218
 
219
+ demo.queue().launch(debug = True, show_error = True)