NotASI commited on
Commit
2053de8
·
1 Parent(s): b5c01e4

Added nightly version in app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py CHANGED
@@ -15,6 +15,7 @@ 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 📜:
@@ -155,8 +156,39 @@ bot_inputs = [
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():
@@ -209,6 +241,7 @@ with gr.Blocks(theme = gr.themes.Soft()) as demo:
209
  chatbot_component
210
  ]
211
  )
 
212
 
213
  with gr.Tab("Chat with Gemma 2"):
214
  gr.HTML(
@@ -217,11 +250,24 @@ with gr.Blocks(theme = gr.themes.Soft()) as demo:
217
  """
218
  )
219
 
 
220
  with gr.Tab("Nightly -- Chat with Gemini 1.5"):
221
  gr.HTML(
222
  """
223
  <h1 align="center">This section will test out the next version of the stable version.</h1>
224
  """
225
  )
 
 
 
 
 
 
 
 
 
 
 
 
226
 
227
  demo.queue().launch(debug = True, show_error = True)
 
15
 
16
  GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
17
 
18
+ # ============================== Stable - START ==============================
19
  TITLE = """<h1 align="center">🎮Chat with Gemini 1.5🔥 -- Beta Preview</h1>"""
20
  NOTICE = """
21
  Notices 📜:
 
156
  top_p_component,
157
  chatbot_component
158
  ]
159
+ # ============================== Stable - END ==============================
160
+
161
+ # ============================== Nightly - START ==============================
162
+ """
163
+ References:
164
+ - https://medium.com/latinxinai/simple-chatbot-gradio-google-gemini-api-4ce02fbaf09f
165
+ """
166
+ GEMINI_API_KEY_NIGHTLY = os.getenv("GEMINI_API_KEY_NIGHTLY")
167
+ model_nightly_name = "gemini-1.5-flash"
168
+ model_nightly = genai.GenerativeModel(model_nightly_name)
169
+ chat = model_nightly.start_chat(history=[])
170
+
171
+ def transform_history(history):
172
+ new_history = []
173
+ for chat in history:
174
+ new_history.append({"parts": [{"text": chat[0]}], "role": "user"})
175
+ new_history.append({"parts": [{"text": chat[1]}], "role": "model"})
176
+ return new_history
177
+
178
+ def response(message, history):
179
+ global chat
180
+ chat.history = transform_history(history)
181
+ response = chat.send_message(message)
182
+ response.resolve()
183
+
184
+ for i in range(len(response.text)):
185
+ time.sleep(0.05)
186
+ yield response.text[:i+1]
187
+
188
+ # ============================== Nightly - END ==============================
189
 
190
  with gr.Blocks(theme = gr.themes.Soft()) as demo:
191
+ # ============================== Stable - START ==============================
192
  with gr.Tab("Chat with Gemini 1.5 Flash"):
193
  gr.HTML(TITLE)
194
  with gr.Row():
 
241
  chatbot_component
242
  ]
243
  )
244
+ # ============================== Stable - END ==============================
245
 
246
  with gr.Tab("Chat with Gemma 2"):
247
  gr.HTML(
 
250
  """
251
  )
252
 
253
+ # ============================== Nightly - START ==============================
254
  with gr.Tab("Nightly -- Chat with Gemini 1.5"):
255
  gr.HTML(
256
  """
257
  <h1 align="center">This section will test out the next version of the stable version.</h1>
258
  """
259
  )
260
+ gr.ChatInterface(
261
+ response,
262
+ title = 'Gemini Chat',
263
+ textbox = gr.Textbox(
264
+ placeholder = "Chat with Gemini"
265
+ ),
266
+ retry_btn = None,
267
+ undo_btn = None,
268
+ clear_btn = None,
269
+ fill_height = True
270
+ )
271
+ # ============================== Nightly - END ==============================
272
 
273
  demo.queue().launch(debug = True, show_error = True)