Hev832 commited on
Commit
6c5d3cb
1 Parent(s): f06c8f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -6
app.py CHANGED
@@ -42,7 +42,7 @@ def generate_response(user_input, chat_history):
42
  )
43
 
44
  # Add user input to history
45
- chat_history.append(user_input)
46
 
47
  # Limit history length to the last 10 messages
48
  chat_history = chat_history[-10:]
@@ -54,15 +54,28 @@ def generate_response(user_input, chat_history):
54
  chat_session = model.start_chat()
55
 
56
  # Send the entire chat history as the first message
57
- response = chat_session.send_message("\n".join(chat_history))
58
- return response.text, chat_history
 
59
 
60
  except Exception as e:
61
  if attempt < retry_attempts - 1:
62
  continue
63
  else:
64
- return f"Error after {retry_attempts} attempts: {str(e)}", chat_history
 
65
 
66
  # Build the Gradio interface using ChatInterface
67
- with gr.ChatInterface(fn=generate_response) as iface:
68
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
42
  )
43
 
44
  # Add user input to history
45
+ chat_history.append(("user", user_input))
46
 
47
  # Limit history length to the last 10 messages
48
  chat_history = chat_history[-10:]
 
54
  chat_session = model.start_chat()
55
 
56
  # Send the entire chat history as the first message
57
+ response = chat_session.send_message("\n".join([f"{role}: {msg}" for role, msg in chat_history]))
58
+ chat_history.append(("assistant", response.text))
59
+ return chat_history
60
 
61
  except Exception as e:
62
  if attempt < retry_attempts - 1:
63
  continue
64
  else:
65
+ chat_history.append(("assistant", f"Error after {retry_attempts} attempts: {str(e)}"))
66
+ return chat_history
67
 
68
  # Build the Gradio interface using ChatInterface
69
+ with gr.Blocks() as iface:
70
+ chatbot = gr.Chatbot() # Create a Chatbot component
71
+ user_input = gr.Textbox(label="Talk to AI", placeholder="Enter your message here...")
72
+ chat_history_state = gr.State([]) # State input for chat history
73
+
74
+ # Define the layout and components
75
+ user_input.submit(
76
+ fn=generate_response,
77
+ inputs=[user_input, chat_history_state],
78
+ outputs=chatbot
79
+ )
80
+
81
+ iface.launch()