Update app.py
Browse files
app.py
CHANGED
|
@@ -2,11 +2,13 @@ import os
|
|
| 2 |
import re
|
| 3 |
import json
|
| 4 |
import time
|
| 5 |
-
from typing import Dict, Any, List
|
| 6 |
from urllib.parse import urlparse, parse_qs
|
| 7 |
|
|
|
|
| 8 |
from fastapi import FastAPI, Request, HTTPException
|
| 9 |
from fastapi.responses import JSONResponse
|
|
|
|
| 10 |
|
| 11 |
try:
|
| 12 |
from huggingface_hub import HfApi
|
|
@@ -22,6 +24,15 @@ os.makedirs(UPLOAD_DIR, exist_ok=True)
|
|
| 22 |
|
| 23 |
app = FastAPI(title="Data Collection Server", description="Receives text/URLs from captioning/image servers, groups by course, compiles JSON and optionally uploads to HuggingFace.")
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# In-memory store for course data
|
| 26 |
courses: Dict[str, Dict[str, Any]] = {}
|
| 27 |
|
|
@@ -227,7 +238,100 @@ async def debug_course(course: str):
|
|
| 227 |
return courses[course]
|
| 228 |
|
| 229 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
if __name__ == "__main__":
|
| 231 |
import uvicorn
|
| 232 |
port = int(os.getenv("PORT", "8000"))
|
|
|
|
|
|
|
|
|
|
| 233 |
uvicorn.run(app, host="0.0.0.0", port=port)
|
|
|
|
| 2 |
import re
|
| 3 |
import json
|
| 4 |
import time
|
| 5 |
+
from typing import Dict, Any, List, Optional
|
| 6 |
from urllib.parse import urlparse, parse_qs
|
| 7 |
|
| 8 |
+
import gradio as gr
|
| 9 |
from fastapi import FastAPI, Request, HTTPException
|
| 10 |
from fastapi.responses import JSONResponse
|
| 11 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 12 |
|
| 13 |
try:
|
| 14 |
from huggingface_hub import HfApi
|
|
|
|
| 24 |
|
| 25 |
app = FastAPI(title="Data Collection Server", description="Receives text/URLs from captioning/image servers, groups by course, compiles JSON and optionally uploads to HuggingFace.")
|
| 26 |
|
| 27 |
+
# Enable CORS for Gradio
|
| 28 |
+
app.add_middleware(
|
| 29 |
+
CORSMiddleware,
|
| 30 |
+
allow_origins=["*"],
|
| 31 |
+
allow_credentials=True,
|
| 32 |
+
allow_methods=["*"],
|
| 33 |
+
allow_headers=["*"],
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
# In-memory store for course data
|
| 37 |
courses: Dict[str, Dict[str, Any]] = {}
|
| 38 |
|
|
|
|
| 238 |
return courses[course]
|
| 239 |
|
| 240 |
|
| 241 |
+
# Gradio Interface
|
| 242 |
+
def get_course_summary():
|
| 243 |
+
"""Get a formatted summary of all courses and their items"""
|
| 244 |
+
if not courses:
|
| 245 |
+
return "No courses available"
|
| 246 |
+
|
| 247 |
+
summary = []
|
| 248 |
+
for course, data in courses.items():
|
| 249 |
+
items = data["items"]
|
| 250 |
+
last_updated = data["last_updated"]
|
| 251 |
+
last_updated_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(last_updated)) if last_updated else "never"
|
| 252 |
+
|
| 253 |
+
summary.append(f"Course: {course}")
|
| 254 |
+
summary.append(f"Items: {len(items)}")
|
| 255 |
+
summary.append(f"Last Updated: {last_updated_str}")
|
| 256 |
+
if items:
|
| 257 |
+
summary.append("\nLast 3 captions:")
|
| 258 |
+
for item in items[-3:]:
|
| 259 |
+
text = item.get("text", "").strip()
|
| 260 |
+
urls = item.get("urls", [])
|
| 261 |
+
summary.append(f"- {text[:200]}... ({len(urls)} URLs)")
|
| 262 |
+
summary.append("\n" + "-"*40 + "\n")
|
| 263 |
+
|
| 264 |
+
return "\n".join(summary)
|
| 265 |
+
|
| 266 |
+
def compile_course_ui(course_name: str) -> str:
|
| 267 |
+
"""Compile a course from the Gradio UI"""
|
| 268 |
+
if not course_name:
|
| 269 |
+
return "Please enter a course name"
|
| 270 |
+
try:
|
| 271 |
+
path = compile_course(course_name)
|
| 272 |
+
return f"Successfully compiled course to {path}"
|
| 273 |
+
except Exception as e:
|
| 274 |
+
return f"Error compiling course: {str(e)}"
|
| 275 |
+
|
| 276 |
+
def add_test_caption(course_name: str, caption_text: str) -> str:
|
| 277 |
+
"""Add a test caption through the Gradio UI"""
|
| 278 |
+
if not course_name or not caption_text:
|
| 279 |
+
return "Please enter both course name and caption text"
|
| 280 |
+
|
| 281 |
+
entry = {
|
| 282 |
+
"timestamp": now_ts(),
|
| 283 |
+
"text": caption_text,
|
| 284 |
+
"urls": extract_urls(caption_text),
|
| 285 |
+
"json": None,
|
| 286 |
+
"form": {},
|
| 287 |
+
"headers": {"user-agent": "Gradio UI"},
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
add_entry(course_name, entry)
|
| 291 |
+
return f"Added caption to course '{course_name}'. Total items: {len(courses[course_name]['items'])}"
|
| 292 |
+
|
| 293 |
+
def create_gradio_interface():
|
| 294 |
+
"""Create the Gradio interface tabs and components"""
|
| 295 |
+
with gr.Blocks(title="Data Collection Server UI") as interface:
|
| 296 |
+
gr.Markdown("# Data Collection Server Monitor")
|
| 297 |
+
|
| 298 |
+
with gr.Tab("Course Status"):
|
| 299 |
+
status_output = gr.Textbox(label="Current Status", value="Loading...", lines=15)
|
| 300 |
+
refresh_btn = gr.Button("Refresh Status")
|
| 301 |
+
refresh_btn.click(get_course_summary, outputs=status_output)
|
| 302 |
+
|
| 303 |
+
with gr.Tab("Manual Controls"):
|
| 304 |
+
gr.Markdown("### Compile Course")
|
| 305 |
+
with gr.Row():
|
| 306 |
+
compile_course_input = gr.Textbox(label="Course Name")
|
| 307 |
+
compile_btn = gr.Button("Compile to JSON")
|
| 308 |
+
compile_output = gr.Textbox(label="Compile Result", lines=3)
|
| 309 |
+
compile_btn.click(compile_course_ui, inputs=compile_course_input, outputs=compile_output)
|
| 310 |
+
|
| 311 |
+
gr.Markdown("### Add Test Caption")
|
| 312 |
+
with gr.Row():
|
| 313 |
+
test_course_input = gr.Textbox(label="Course Name")
|
| 314 |
+
test_caption_input = gr.Textbox(label="Caption Text", lines=3)
|
| 315 |
+
test_btn = gr.Button("Add Caption")
|
| 316 |
+
test_output = gr.Textbox(label="Result", lines=2)
|
| 317 |
+
test_btn.click(
|
| 318 |
+
add_test_caption,
|
| 319 |
+
inputs=[test_course_input, test_caption_input],
|
| 320 |
+
outputs=test_output
|
| 321 |
+
)
|
| 322 |
+
|
| 323 |
+
# Update status on load
|
| 324 |
+
interface.load(get_course_summary, outputs=status_output)
|
| 325 |
+
return interface
|
| 326 |
+
|
| 327 |
+
# Mount Gradio app to FastAPI
|
| 328 |
+
interface = create_gradio_interface()
|
| 329 |
+
app = gr.mount_gradio_app(app, interface, path="/ui")
|
| 330 |
+
|
| 331 |
if __name__ == "__main__":
|
| 332 |
import uvicorn
|
| 333 |
port = int(os.getenv("PORT", "8000"))
|
| 334 |
+
print(f"[INFO] Server starting on port {port}")
|
| 335 |
+
print(f"[INFO] FastAPI docs: http://localhost:{port}/docs")
|
| 336 |
+
print(f"[INFO] Gradio UI: http://localhost:{port}/ui")
|
| 337 |
uvicorn.run(app, host="0.0.0.0", port=port)
|