akhaliq HF Staff commited on
Commit
b71dea6
·
verified ·
1 Parent(s): 1b1b9e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -8
app.py CHANGED
@@ -5,29 +5,32 @@ import google.generativeai as genai
5
  genai.configure(api_key='YOUR_API_KEY')
6
 
7
  # Initialize the model
8
- model = genai.GenerativeModel(model='gemini-1.5-flash')
9
 
10
  def respond(user_message, history, chat_state):
11
  if chat_state is None:
12
  # Start a new chat with an initial greeting
13
  chat_state = model.start_chat(
14
  history=[
15
- {"role": "user", "content": "Hello"},
16
- {"role": "assistant", "content": "Great to meet you. What would you like to know?"},
17
  ]
18
  )
19
  # Initialize history if it's empty
20
  if not history:
21
  history = [["Hello", "Great to meet you. What would you like to know?"]]
22
- # Send the user's message to the model
23
- response = chat_state.send_message(user_message)
24
- # Append the user's message and model's response to the history
25
- history.append([user_message, response.text])
 
 
 
26
  return history, chat_state, ''
27
 
28
  with gr.Blocks() as demo:
29
  gr.Markdown("<h1 align='center'>Gemini 1.5 Flash Chatbot Demo</h1>")
30
- chatbot = gr.Chatbot([[ "Hello", "Great to meet you. What would you like to know?"]])
31
  msg = gr.Textbox(placeholder="Type your message here...", show_label=False)
32
  state = gr.State() # To store the chat_state object
33
 
 
5
  genai.configure(api_key='YOUR_API_KEY')
6
 
7
  # Initialize the model
8
+ model = genai.GenerativeModel('gemini-1.5-flash')
9
 
10
  def respond(user_message, history, chat_state):
11
  if chat_state is None:
12
  # Start a new chat with an initial greeting
13
  chat_state = model.start_chat(
14
  history=[
15
+ {"author": "user", "content": "Hello"},
16
+ {"author": "assistant", "content": "Great to meet you. What would you like to know?"},
17
  ]
18
  )
19
  # Initialize history if it's empty
20
  if not history:
21
  history = [["Hello", "Great to meet you. What would you like to know?"]]
22
+ else:
23
+ # Continue the conversation
24
+ chat_state.messages.append({"author": "user", "content": user_message})
25
+ # Generate a response
26
+ response = chat_state.generate_message()
27
+ # Append the user's message and model's response to the history
28
+ history.append([user_message, response])
29
  return history, chat_state, ''
30
 
31
  with gr.Blocks() as demo:
32
  gr.Markdown("<h1 align='center'>Gemini 1.5 Flash Chatbot Demo</h1>")
33
+ chatbot = gr.Chatbot([["Hello", "Great to meet you. What would you like to know?"]])
34
  msg = gr.Textbox(placeholder="Type your message here...", show_label=False)
35
  state = gr.State() # To store the chat_state object
36