Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| from agent import PilatopiaAgentSystem | |
| from langchain_core.messages import HumanMessage, AIMessage | |
| agent_system = None | |
| try: | |
| api_key = os.getenv("OPENAI_API_KEY") | |
| agent_system = PilatopiaAgentSystem(api_key) | |
| print("✅ System Ready") | |
| except Exception as e: | |
| print(f"❌ System Error: {e}") | |
| conversation_state = { | |
| "messages": [], | |
| "current_plan": "", | |
| "language_detected": "", | |
| "response_draft": "", | |
| "manager_feedback": "", | |
| "final_response": "", | |
| "iteration_count": 0, | |
| "needs_planning": False, | |
| "plan_approved": False | |
| } | |
| def chat_with_agent(message, history): | |
| if not agent_system: | |
| return "System not available. Please check your API key." | |
| try: | |
| global conversation_state | |
| new_user_message = HumanMessage(content=message) | |
| conversation_state["messages"] = conversation_state["messages"] + [new_user_message] | |
| conversation_state.update({ | |
| "current_plan": "", | |
| "response_draft": "", | |
| "manager_feedback": "", | |
| "final_response": "", | |
| "needs_planning": False, | |
| "plan_approved": False, | |
| "iteration_count": 0 | |
| }) | |
| final_state = conversation_state | |
| for event in agent_system.graph.stream(conversation_state): | |
| for node_name, value in event.items(): | |
| final_state = value | |
| conversation_state = final_state | |
| return final_state.get("final_response", "عذراً، حدث خطأ.") | |
| except Exception as e: | |
| return f"خطأ: {str(e)}" | |
| def reset_chat(): | |
| """Reset conversation""" | |
| global conversation_state | |
| conversation_state = { | |
| "messages": [], | |
| "current_plan": "", | |
| "language_detected": "", | |
| "response_draft": "", | |
| "manager_feedback": "", | |
| "final_response": "", | |
| "iteration_count": 0, | |
| "needs_planning": False, | |
| "plan_approved": False | |
| } | |
| demo = gr.ChatInterface( | |
| fn=chat_with_agent, | |
| title="🏋️♀️ Pilatopia Customer Support", | |
| description="مرحباً بك في بيلاتوبيا! / Welcome to Pilatopia!", | |
| examples=[ | |
| "السلام عليكم", | |
| "I want to book a trial class", | |
| "ابغي اجل اشتراكي", | |
| "ابغي الغي اشتراكي" | |
| ], | |
| theme=gr.themes.Soft() | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |