Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import os
|
|
| 3 |
from agent import PilatopiaAgentSystem
|
| 4 |
from langchain_core.messages import HumanMessage, AIMessage
|
| 5 |
|
| 6 |
-
# Initialize system
|
| 7 |
agent_system = None
|
| 8 |
try:
|
| 9 |
api_key = os.getenv("OPENAI_API_KEY")
|
|
@@ -12,7 +11,6 @@ try:
|
|
| 12 |
except Exception as e:
|
| 13 |
print(f"❌ System Error: {e}")
|
| 14 |
|
| 15 |
-
# Global conversation state
|
| 16 |
conversation_state = {
|
| 17 |
"messages": [],
|
| 18 |
"current_plan": "",
|
|
@@ -26,17 +24,14 @@ conversation_state = {
|
|
| 26 |
}
|
| 27 |
|
| 28 |
def chat_with_agent(message, history):
|
| 29 |
-
"""Simple chat function for Gradio"""
|
| 30 |
if not agent_system:
|
| 31 |
-
return "
|
| 32 |
|
| 33 |
try:
|
| 34 |
-
# Add user message
|
| 35 |
global conversation_state
|
| 36 |
new_user_message = HumanMessage(content=message)
|
| 37 |
conversation_state["messages"] = conversation_state["messages"] + [new_user_message]
|
| 38 |
|
| 39 |
-
# Reset for new processing
|
| 40 |
conversation_state.update({
|
| 41 |
"current_plan": "",
|
| 42 |
"response_draft": "",
|
|
@@ -47,13 +42,11 @@ def chat_with_agent(message, history):
|
|
| 47 |
"iteration_count": 0
|
| 48 |
})
|
| 49 |
|
| 50 |
-
# Run agent system
|
| 51 |
final_state = conversation_state
|
| 52 |
for event in agent_system.graph.stream(conversation_state):
|
| 53 |
for node_name, value in event.items():
|
| 54 |
final_state = value
|
| 55 |
|
| 56 |
-
# Update global state and return response
|
| 57 |
conversation_state = final_state
|
| 58 |
return final_state.get("final_response", "عذراً، حدث خطأ.")
|
| 59 |
|
|
@@ -75,7 +68,6 @@ def reset_chat():
|
|
| 75 |
"plan_approved": False
|
| 76 |
}
|
| 77 |
|
| 78 |
-
# Create Gradio interface
|
| 79 |
demo = gr.ChatInterface(
|
| 80 |
fn=chat_with_agent,
|
| 81 |
title="🏋️♀️ Pilatopia Customer Support",
|
|
|
|
| 3 |
from agent import PilatopiaAgentSystem
|
| 4 |
from langchain_core.messages import HumanMessage, AIMessage
|
| 5 |
|
|
|
|
| 6 |
agent_system = None
|
| 7 |
try:
|
| 8 |
api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 11 |
except Exception as e:
|
| 12 |
print(f"❌ System Error: {e}")
|
| 13 |
|
|
|
|
| 14 |
conversation_state = {
|
| 15 |
"messages": [],
|
| 16 |
"current_plan": "",
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
def chat_with_agent(message, history):
|
|
|
|
| 27 |
if not agent_system:
|
| 28 |
+
return "System not available. Please check your API key."
|
| 29 |
|
| 30 |
try:
|
|
|
|
| 31 |
global conversation_state
|
| 32 |
new_user_message = HumanMessage(content=message)
|
| 33 |
conversation_state["messages"] = conversation_state["messages"] + [new_user_message]
|
| 34 |
|
|
|
|
| 35 |
conversation_state.update({
|
| 36 |
"current_plan": "",
|
| 37 |
"response_draft": "",
|
|
|
|
| 42 |
"iteration_count": 0
|
| 43 |
})
|
| 44 |
|
|
|
|
| 45 |
final_state = conversation_state
|
| 46 |
for event in agent_system.graph.stream(conversation_state):
|
| 47 |
for node_name, value in event.items():
|
| 48 |
final_state = value
|
| 49 |
|
|
|
|
| 50 |
conversation_state = final_state
|
| 51 |
return final_state.get("final_response", "عذراً، حدث خطأ.")
|
| 52 |
|
|
|
|
| 68 |
"plan_approved": False
|
| 69 |
}
|
| 70 |
|
|
|
|
| 71 |
demo = gr.ChatInterface(
|
| 72 |
fn=chat_with_agent,
|
| 73 |
title="🏋️♀️ Pilatopia Customer Support",
|