Spaces:
Build error
Build error
Update app.py
Browse files
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 |
-
|
|
|
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
if attempt < retry_attempts - 1:
|
| 62 |
continue
|
| 63 |
else:
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
# Build the Gradio interface using ChatInterface
|
| 67 |
-
with gr.
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|