Upload 3 files
Browse files- Dockerfile +16 -0
- app.py +93 -0
- requirements.txt +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
RUN pip install --no-cache-dir gradio requests python-dotenv
|
| 6 |
+
|
| 7 |
+
COPY . .
|
| 8 |
+
|
| 9 |
+
RUN useradd -m -u 1000 user
|
| 10 |
+
USER user
|
| 11 |
+
ENV HOME=/home/user \
|
| 12 |
+
PATH=/home/user/.local/bin:$PATH
|
| 13 |
+
|
| 14 |
+
EXPOSE 7860
|
| 15 |
+
|
| 16 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
# Backend URLs (should be set via Environment Variables in HF Spaces)
|
| 7 |
+
# Example: https://user-engine-space.hf.space
|
| 8 |
+
CPU_BACKEND_URL = os.getenv("CPU_BACKEND_URL", "http://localhost:7860")
|
| 9 |
+
GPU_BACKEND_URL = os.getenv("GPU_BACKEND_URL", "http://localhost:7861")
|
| 10 |
+
API_KEY = os.getenv("API_KEY", "")
|
| 11 |
+
|
| 12 |
+
def get_backend_url(mode):
|
| 13 |
+
return GPU_BACKEND_URL if mode == "GPU (Faster)" else CPU_BACKEND_URL
|
| 14 |
+
|
| 15 |
+
def health_check(mode):
|
| 16 |
+
url = get_backend_url(mode)
|
| 17 |
+
try:
|
| 18 |
+
resp = requests.get(f"{url}/health", timeout=5)
|
| 19 |
+
return resp.json()
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return {"error": str(e)}
|
| 22 |
+
|
| 23 |
+
def upload_file(mode, file):
|
| 24 |
+
if file is None:
|
| 25 |
+
return "No file selected."
|
| 26 |
+
|
| 27 |
+
url = get_backend_url(mode)
|
| 28 |
+
headers = {"X-API-Key": API_KEY} if API_KEY else {}
|
| 29 |
+
|
| 30 |
+
with open(file.name, "rb") as f:
|
| 31 |
+
files = {"file": (os.path.basename(file.name), f)}
|
| 32 |
+
try:
|
| 33 |
+
resp = requests.post(f"{url}/ingest", files=files, headers=headers)
|
| 34 |
+
return resp.json()
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return {"error": str(e)}
|
| 37 |
+
|
| 38 |
+
def generate_syllabus(mode, course_name, description, constraints):
|
| 39 |
+
url = get_backend_url(mode)
|
| 40 |
+
headers = {"X-API-Key": API_KEY} if API_KEY else {}
|
| 41 |
+
payload = {
|
| 42 |
+
"course_name": course_name,
|
| 43 |
+
"description": description,
|
| 44 |
+
"constraints": constraints
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
resp = requests.post(f"{url}/generate_syllabus", json=payload, headers=headers)
|
| 49 |
+
data = resp.json()
|
| 50 |
+
if "syllabus" in data:
|
| 51 |
+
return data["syllabus"]
|
| 52 |
+
return json.dumps(data, indent=2)
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return f"Error: {str(e)}"
|
| 55 |
+
|
| 56 |
+
# UI Layout
|
| 57 |
+
with gr.Blocks(title="Open Source Syllabus Engine") as demo:
|
| 58 |
+
gr.Markdown("# 🎓 Open Source Syllabus Engine")
|
| 59 |
+
gr.Markdown("Ingest your documents and generate a production-grade syllabus using RAG.")
|
| 60 |
+
|
| 61 |
+
with gr.Row():
|
| 62 |
+
mode_radio = gr.Radio(
|
| 63 |
+
choices=["CPU (Free)", "GPU (Faster)"],
|
| 64 |
+
value="CPU (Free)",
|
| 65 |
+
label="Select Backend"
|
| 66 |
+
)
|
| 67 |
+
health_btn = gr.Button("Check Connection")
|
| 68 |
+
health_output = gr.JSON(label="Backend Status")
|
| 69 |
+
|
| 70 |
+
health_btn.click(health_check, inputs=[mode_radio], outputs=[health_output])
|
| 71 |
+
|
| 72 |
+
with gr.Tab("1. Ingest Documents"):
|
| 73 |
+
file_input = gr.File(label="Upload PDF/DOCX/TXT")
|
| 74 |
+
ingest_btn = gr.Button("Ingest")
|
| 75 |
+
ingest_output = gr.JSON(label="Ingestion Result")
|
| 76 |
+
ingest_btn.click(upload_file, inputs=[mode_radio, file_input], outputs=[ingest_output])
|
| 77 |
+
|
| 78 |
+
with gr.Tab("2. Generate Syllabus"):
|
| 79 |
+
with gr.Row():
|
| 80 |
+
course_name = gr.Textbox(label="Course Name", placeholder="e.g. Advanced Machine Learning")
|
| 81 |
+
course_desc = gr.Textbox(label="Description", placeholder="Describe what the course covers...")
|
| 82 |
+
constraints = gr.Textbox(label="Optional Constraints", placeholder="e.g. 10 weeks, include a final project")
|
| 83 |
+
gen_btn = gr.Button("Generate Syllabus", variant="primary")
|
| 84 |
+
gen_output = gr.Markdown(label="Generated Syllabus")
|
| 85 |
+
gen_btn.click(generate_syllabus, inputs=[mode_radio, course_name, course_desc, constraints], outputs=[gen_output])
|
| 86 |
+
|
| 87 |
+
gr.Markdown("---")
|
| 88 |
+
gr.Markdown("### Ops Notes")
|
| 89 |
+
gr.Markdown("- Ensure `CPU_BACKEND_URL` and `GPU_BACKEND_URL` are set correctly.")
|
| 90 |
+
gr.Markdown("- Use the API Key if authentication is enabled on the backends.")
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
requests
|
| 3 |
+
python-dotenv
|