Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import uuid | |
| import os | |
| from dotenv import load_dotenv | |
| # Load environment variables | |
| load_dotenv() | |
| # Define API endpoints - use localhost port for the FastAPI backend | |
| # In production, they're both on the same host, just different ports | |
| API_BASE_URL = "http://localhost:8000" if os.getenv("SPACE_ID") else "http://localhost:8000" | |
| def chat_with_api(message, state=None): | |
| """Send user message to the API and get a response""" | |
| if state is None: | |
| # Initialize new conversation with a unique session ID | |
| session_id = str(uuid.uuid4()) | |
| state = {"session_id": session_id, "history": []} | |
| else: | |
| session_id = state["session_id"] | |
| # Call the API | |
| response = requests.post( | |
| f"{API_BASE_URL}/chat", | |
| json={"message": message, "session_id": session_id} | |
| ) | |
| if response.status_code != 200: | |
| return f"Error: {response.status_code}", state | |
| data = response.json() | |
| api_response = data["response"] | |
| # Update conversation history | |
| state["history"].append((message, api_response)) | |
| # If the conversation is complete, display the final parameters | |
| if data["session_state"]["completed"]: | |
| params = data["session_state"]["params"] | |
| param_text = "\n\n**Test Parameters:**\n" | |
| for k, v in params.items(): | |
| param_text += f"- **{k.replace('_', ' ').title()}**: {v or 'Not provided'}\n" | |
| api_response += param_text | |
| return api_response, state | |
| def reset_conversation(): | |
| """Start a new conversation""" | |
| return "", {"session_id": str(uuid.uuid4()), "history": []} | |
| # Create the Gradio interface | |
| with gr.Blocks(title="Test Creation Agent") as app: | |
| gr.Markdown("# ๐ Test Creation Agent") | |
| gr.Markdown(""" | |
| This application helps you create educational tests through conversation. | |
| Simply describe your test requirements, and the AI will collect all necessary parameters: | |
| - Chapters/topics to include | |
| - Number of questions per chapter | |
| - Difficulty distribution | |
| - Test duration | |
| - Test date and time | |
| """) | |
| chatbot = gr.Chatbot(label="Conversation") | |
| msg = gr.Textbox(label="Your message", placeholder="Describe your test requirements...") | |
| with gr.Row(): | |
| submit_btn = gr.Button("Send") | |
| reset_btn = gr.Button("Reset") | |
| state = gr.State() | |
| # Set up event handlers | |
| msg.submit(chat_with_api, [msg, state], [chatbot, state]) | |
| submit_btn.click(chat_with_api, [msg, state], [chatbot, state], queue=False) | |
| reset_btn.click(reset_conversation, None, [chatbot, state], queue=False) | |
| # Start conversation automatically on page load | |
| gr.on_load(lambda: chat_with_api("", None)) | |
| # Launch the app | |
| if __name__ == "__main__": | |
| # Check if running in production (on Hugging Face Spaces) | |
| if os.getenv("SPACE_ID"): | |
| app.launch(server_name="0.0.0.0", server_port=7860) | |
| else: | |
| app.launch() |