Spaces:
Sleeping
Sleeping
Delete gradio_app.py
#3
by
Rakshitjan
- opened
- gradio_app.py +0 -88
gradio_app.py
DELETED
|
@@ -1,88 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
-
import uuid
|
| 4 |
-
import os
|
| 5 |
-
from dotenv import load_dotenv
|
| 6 |
-
|
| 7 |
-
# Load environment variables
|
| 8 |
-
load_dotenv()
|
| 9 |
-
|
| 10 |
-
# Define API endpoints - use localhost port for the FastAPI backend
|
| 11 |
-
# In production, they're both on the same host, just different ports
|
| 12 |
-
API_BASE_URL = "http://localhost:8000" if os.getenv("SPACE_ID") else "http://localhost:8000"
|
| 13 |
-
|
| 14 |
-
def chat_with_api(message, state=None):
|
| 15 |
-
"""Send user message to the API and get a response"""
|
| 16 |
-
if state is None:
|
| 17 |
-
# Initialize new conversation with a unique session ID
|
| 18 |
-
session_id = str(uuid.uuid4())
|
| 19 |
-
state = {"session_id": session_id, "history": []}
|
| 20 |
-
else:
|
| 21 |
-
session_id = state["session_id"]
|
| 22 |
-
|
| 23 |
-
# Call the API
|
| 24 |
-
response = requests.post(
|
| 25 |
-
f"{API_BASE_URL}/chat",
|
| 26 |
-
json={"message": message, "session_id": session_id}
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
if response.status_code != 200:
|
| 30 |
-
return f"Error: {response.status_code}", state
|
| 31 |
-
|
| 32 |
-
data = response.json()
|
| 33 |
-
api_response = data["response"]
|
| 34 |
-
|
| 35 |
-
# Update conversation history
|
| 36 |
-
state["history"].append((message, api_response))
|
| 37 |
-
|
| 38 |
-
# If the conversation is complete, display the final parameters
|
| 39 |
-
if data["session_state"]["completed"]:
|
| 40 |
-
params = data["session_state"]["params"]
|
| 41 |
-
param_text = "\n\n**Test Parameters:**\n"
|
| 42 |
-
for k, v in params.items():
|
| 43 |
-
param_text += f"- **{k.replace('_', ' ').title()}**: {v or 'Not provided'}\n"
|
| 44 |
-
api_response += param_text
|
| 45 |
-
|
| 46 |
-
return api_response, state
|
| 47 |
-
|
| 48 |
-
def reset_conversation():
|
| 49 |
-
"""Start a new conversation"""
|
| 50 |
-
return "", {"session_id": str(uuid.uuid4()), "history": []}
|
| 51 |
-
|
| 52 |
-
# Create the Gradio interface
|
| 53 |
-
with gr.Blocks(title="Test Creation Agent") as app:
|
| 54 |
-
gr.Markdown("# 📝 Test Creation Agent")
|
| 55 |
-
gr.Markdown("""
|
| 56 |
-
This application helps you create educational tests through conversation.
|
| 57 |
-
|
| 58 |
-
Simply describe your test requirements, and the AI will collect all necessary parameters:
|
| 59 |
-
- Chapters/topics to include
|
| 60 |
-
- Number of questions per chapter
|
| 61 |
-
- Difficulty distribution
|
| 62 |
-
- Test duration
|
| 63 |
-
- Test date and time
|
| 64 |
-
""")
|
| 65 |
-
|
| 66 |
-
chatbot = gr.Chatbot(label="Conversation")
|
| 67 |
-
msg = gr.Textbox(label="Your message", placeholder="Describe your test requirements...")
|
| 68 |
-
with gr.Row():
|
| 69 |
-
submit_btn = gr.Button("Send")
|
| 70 |
-
reset_btn = gr.Button("Reset")
|
| 71 |
-
|
| 72 |
-
state = gr.State()
|
| 73 |
-
|
| 74 |
-
# Set up event handlers
|
| 75 |
-
msg.submit(chat_with_api, [msg, state], [chatbot, state])
|
| 76 |
-
submit_btn.click(chat_with_api, [msg, state], [chatbot, state], queue=False)
|
| 77 |
-
reset_btn.click(reset_conversation, None, [chatbot, state], queue=False)
|
| 78 |
-
|
| 79 |
-
# Start conversation automatically on page load
|
| 80 |
-
gr.on_load(lambda: chat_with_api("", None))
|
| 81 |
-
|
| 82 |
-
# Launch the app
|
| 83 |
-
if __name__ == "__main__":
|
| 84 |
-
# Check if running in production (on Hugging Face Spaces)
|
| 85 |
-
if os.getenv("SPACE_ID"):
|
| 86 |
-
app.launch(server_name="0.0.0.0", server_port=7860)
|
| 87 |
-
else:
|
| 88 |
-
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|