akhaliq HF staff commited on
Commit
ab8a15b
·
verified ·
1 Parent(s): 770a47d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -4
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  import google.generativeai as genai
3
  import gradio as gr
 
4
 
5
  # Configure the API key
6
  genai.configure(api_key=os.environ["YOUR_API_KEY"])
@@ -25,8 +26,15 @@ model = genai.GenerativeModel(
25
  def chat_with_model(user_input, history):
26
  # Update the history with the user's input
27
  history.append({"role": "user", "content": user_input})
28
- # Start or continue the chat session
29
- chat_session = model.start_chat(history=history)
 
 
 
 
 
 
 
30
  response = chat_session.send_message(user_input)
31
  # Add the assistant's response to the history
32
  history.append({"role": "assistant", "content": response.text})
@@ -48,7 +56,7 @@ with gr.Blocks() as demo:
48
  show_label=False,
49
  placeholder="Type your message and press Enter"
50
  )
51
- send_btn = gr.Button("Send") # Ensure this line uses 'send_btn'
52
  # Event handlers
53
  send_btn.click(chat_with_model, [user_input, state], [chatbot, state])
54
  user_input.submit(chat_with_model, [user_input, state], [chatbot, state])
@@ -56,4 +64,4 @@ with gr.Blocks() as demo:
56
  send_btn.click(lambda: "", None, user_input)
57
  user_input.submit(lambda: "", None, user_input)
58
 
59
- demo.launch()
 
1
  import os
2
  import google.generativeai as genai
3
  import gradio as gr
4
+ from google.generativeai.protos import content_types as protos
5
 
6
  # Configure the API key
7
  genai.configure(api_key=os.environ["YOUR_API_KEY"])
 
26
  def chat_with_model(user_input, history):
27
  # Update the history with the user's input
28
  history.append({"role": "user", "content": user_input})
29
+
30
+ # Convert history to expected format (list of protos.Content)
31
+ formatted_history = []
32
+ for entry in history:
33
+ content = protos.Content(text=entry["content"], role=entry["role"])
34
+ formatted_history.append(content)
35
+
36
+ # Start or continue the chat session using the formatted history
37
+ chat_session = model.start_chat(history=formatted_history)
38
  response = chat_session.send_message(user_input)
39
  # Add the assistant's response to the history
40
  history.append({"role": "assistant", "content": response.text})
 
56
  show_label=False,
57
  placeholder="Type your message and press Enter"
58
  )
59
+ send_btn = gr.Button("Send")
60
  # Event handlers
61
  send_btn.click(chat_with_model, [user_input, state], [chatbot, state])
62
  user_input.submit(chat_with_model, [user_input, state], [chatbot, state])
 
64
  send_btn.click(lambda: "", None, user_input)
65
  user_input.submit(lambda: "", None, user_input)
66
 
67
+ demo.launch()