CoolFace
Apppublic

SHUBHAMOS/meta-pytorch-hackathon

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes
app.py128 linesDownload Raw Back to server
1import sys2from pathlib import Path3 4# Add project root to path so we can import root scripts like inference.py5ROOT_DIR = Path(__file__).parent.parent6if str(ROOT_DIR) not in sys.path:7    sys.path.append(str(ROOT_DIR))8 9import gradio as gr10import os11import json12import pandas as pd13from .server import app as fastapi_app14from .tasks import TASKS15# Note: inference is imported lazily below to ensure sys.path is updated16from fastapi.middleware.cors import CORSMiddleware17 18# ── Gradio Logic ──────────────────────────────────────────────────────────────19 20def run_benchmark_ui(hf_token, task_id):21    """Bridge for Gradio to call the inference agent."""22    if not hf_token:23        # Default to environment token if empty24        hf_token = os.environ.get("HF_TOKEN", "")25        if not hf_token:26            return "Error: Please provide a Hugging Face Token.", None27    28    try:29        # Set token in environment for backend stability30        os.environ["HF_TOKEN"] = hf_token31        32        # Lazy import of inference to avoid boot-time path issues33        from inference import run_agent34        35        # Run agent36        result = run_agent(task_id, hf_token=hf_token, verbose=False)37        38        if "error" in result:39            return f"Error: {result['error']}", None40            41        # Format results for display42        scores = result.get("scores", {})43        df_data = {44            "Metric": ["Final Score", "Classification Accuracy", "Priority Accuracy", "Resolution Rate", "Urgent Handling"],45            "Value": [46                f"{scores.get('final_score', 0):.3f}",47                f"{scores.get('classification_accuracy', 0):.3f}",48                f"{scores.get('priority_accuracy', 0):.3f}",49                f"{scores.get('resolution_rate', 0):.3f}",50                f"{scores.get('urgent_handling', 0):.3f}"51            ]52        }53        df = pd.DataFrame(df_data)54        55        summary = f"### Benchmark Complete! 🚀\n**Status:** {'PASS ✅' if result.get('passed') else 'FAIL ❌'}\n**Total Reward:** {result.get('total_reward', 0):.3f}"56        57        return summary, df58        59    except Exception as e:60        return f"Fatal Error: {str(e)}", None61 62# ── Build UI ──────────────────────────────────────────────────────────────────63 64with gr.Blocks(title="SHUBHAMOS: AI Email Triage Benchmarking") as demo:65    gr.Markdown("# 📈 SHUBHAMOS Benchmark Controller")66    gr.Markdown("Test your AI agent's email triage capabilities using the OpenEnv protocol.")67    68    with gr.Row():69        with gr.Column(scale=1):70            token_input = gr.Textbox(71                label="Hugging Face Token", 72                placeholder="hf_...", 73                type="password",74                info="Required to call the AI Router (Qwen/Qwen2.5-72B-Instruct)."75            )76            task_select = gr.Radio(77                choices=["easy", "medium", "hard"], 78                value="easy", 79                label="Simulation Task"80            )81            run_btn = gr.Button("Run Agent Benchmark 🚀", variant="primary")82            gr.Markdown("---")83            gr.Markdown("### API Endpoints")84            gr.Markdown("- [FastAPI Docs](/docs)")85            gr.Markdown("- [Environment Spec](/openenv.yaml)")86            gr.Markdown("- [Dashboard](/dashboard)")87 88        with gr.Column(scale=2):89            result_summary = gr.Markdown("### Results will appear here...")90            result_table = gr.Dataframe(label="Performance Metrics")91 92    run_btn.click(93        fn=run_benchmark_ui,94        inputs=[token_input, task_select],95        outputs=[result_summary, result_table]96    )97 98# ── Mount & Launch ────────────────────────────────────────────────────────────99 100# Combine FastAPI and Gradio101app = gr.mount_gradio_app(fastapi_app, demo, path="/")102 103def main():104    """CLI entry point for the OpenEnv 'server' command."""105    import uvicorn106    import threading107    import time108    109    # Start background diagnostics to avoid blocking uvicorn110    def run_diagnostics():111        time.sleep(5)112        try:113            from .health_check import run_preflight_checks114            print("\n[Diagnostic] Running background system check...")115            run_preflight_checks()116        except Exception as e:117            print(f"\n[Diagnostic] Background check failed: {e}")118 119    diag_thread = threading.Thread(target=run_diagnostics, daemon=True)120    diag_thread.start()121 122    port = int(os.environ.get("PORT", 7860))123    # Note: Use string import to avoid bootstrap issues124    uvicorn.run("server.app:app", host="0.0.0.0", port=port)125 126if __name__ == "__main__":127    main()128