Katieearl1513 commited on
Commit
c2ffdc0
·
verified ·
1 Parent(s): bf9bb44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -7
app.py CHANGED
@@ -23,9 +23,9 @@ model = genai.GenerativeModel(
23
  system_instruction=system_instruction
24
  )
25
 
26
- def chatbot(user_input, history):
27
  # Initialize a new chat session for Gemini AI if history is empty
28
- if len(history) == 0:
29
  chat_session = model.start_chat()
30
 
31
  # Send the user query to Gemini AI
@@ -38,18 +38,21 @@ def chatbot(user_input, history):
38
 
39
  # Convert to Gradio's format
40
  gradio_history = [[msg["role"], msg["content"]] for msg in history]
41
- return gradio_history, history
42
 
43
- #user interface of Gradio web app is created structured in blocks.
44
- #containing typical chatbot setting, with history saved and textbox to make entries
45
- #If the user makes an input “txt.sumit()”, the function “chatbot()” is called and the output is displayed in the chat window “chatbot_interface”
 
46
 
 
47
  with gr.Blocks() as demo:
48
  chatbot_interface = gr.Chatbot()
49
  state = gr.State([])
 
50
 
51
  with gr.Row():
52
  txt = gr.Textbox(show_label=False, placeholder="Type your question here...")
53
- txt.submit(chatbot, [txt, state], [chatbot_interface, state])
54
 
55
  demo.launch()
 
23
  system_instruction=system_instruction
24
  )
25
 
26
+ def chatbot(user_input, history, chat_session):
27
  # Initialize a new chat session for Gemini AI if history is empty
28
+ if chat_session is None:
29
  chat_session = model.start_chat()
30
 
31
  # Send the user query to Gemini AI
 
38
 
39
  # Convert to Gradio's format
40
  gradio_history = [[msg["role"], msg["content"]] for msg in history]
41
+ return gradio_history, history, chat_session
42
 
43
+ #Handling error ---> except Exception as e
44
+ #error_message = f"An error occurred: {str(e)}"
45
+ #history.append({"role": "assistant", "content": error_message})
46
+ #gradio_history = [[msg["role"], msg["content"]] for msg in history]
47
 
48
+ #user interface
49
  with gr.Blocks() as demo:
50
  chatbot_interface = gr.Chatbot()
51
  state = gr.State([])
52
+ session_state = gr.State(None)
53
 
54
  with gr.Row():
55
  txt = gr.Textbox(show_label=False, placeholder="Type your question here...")
56
+ txt.submit(chatbot, [txt, state, session_state], [chatbot_interface, state, session_state])
57
 
58
  demo.launch()