cjo93 commited on
Commit
aef8222
·
verified ·
1 Parent(s): dc7a38f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Here is the `app.py` for your **DEFRAG.APP // CORE** environment. This FastAPI backend securely manages your ElevenLabs credentials, serves the frontend, and acts as the bridge for your premium agent console. [elevenlabs](https://elevenlabs.io/app/agents/agents/agent_4101kfyny563e168da90yze4vva4?branchId=agtbrch_5801kfyny6tefd88a3krvcq388jz&tab=widget)
2
+
3
+ ```python
4
+ import os
5
+ import httpx
6
+ from fastapi import FastAPI, HTTPException, Request
7
+ from fastapi.responses import HTMLResponse, FileResponse
8
+ from fastapi.staticfiles import StaticFiles
9
+ from fastapi.middleware.cors import CORSMiddleware
10
+ from dotenv import load_dotenv
11
+
12
+ # Load environment variables (Local .env or HF Secrets)
13
+ load_dotenv()
14
+
15
+ app = FastAPI(title="DEFRAG.APP // CORE_SYSTEM")
16
+
17
+ # Enable CORS for local development
18
+ app.add_middleware(
19
+ CORSMiddleware,
20
+ allow_origins=["*"],
21
+ allow_credentials=True,
22
+ allow_methods=["*"],
23
+ allow_headers=["*"],
24
+ )
25
+
26
+ # 1. MOUNT STATIC ASSETS (Rive files, Icons, etc.)
27
+ # Create an 'assets' folder in your HF Space root
28
+ if os.path.exists("assets"):
29
+ app.mount("/assets", StaticFiles(directory="assets"), name="assets")
30
+
31
+ # 2. THE SECURE ELEVENLABS PROXY ROUTE
32
+ @app.get("/api/get-signed-url")
33
+ async def get_signed_url():
34
+ """
35
+ Fetches a signed URL from ElevenLabs to keep the API Key hidden from the client.
36
+ """
37
+ ELEVENLABS_API_KEY = os.environ.get("ELEVENLABS_API_KEY")
38
+ # Your confirmed Agent ID from the dashboard
39
+ AGENT_ID = "agent_4101kfyny563e168da90yze4vva4"
40
+
41
+ if not ELEVENLABS_API_KEY:
42
+ raise HTTPException(status_code=500, detail="ELEVENLABS_API_KEY not found in environment")
43
+
44
+ async with httpx.AsyncClient() as client:
45
+ try:
46
+ url = f"https://api.elevenlabs.io/v1/convai/conversation/get_signed_url?agent_id={AGENT_ID}"
47
+ response = await client.get(
48
+ url,
49
+ headers={"xi-api-key": ELEVENLABS_API_KEY},
50
+ timeout=10.0
51
+ )
52
+
53
+ if response.status_code != 200:
54
+ print(f"ElevenLabs Error: {response.text}")
55
+ raise HTTPException(status_code=response.status_code, detail="Failed to fetch signed URL")
56
+
57
+ return response.json()
58
+
59
+ except Exception as e:
60
+ print(f"Proxy Error: {str(e)}")
61
+ raise HTTPException(status_code=500, detail="Internal Server Error during proxy")
62
+
63
+ # 3. SERVE THE MAIN DEFRAG.APP INTERFACE
64
+ # For a Static JS/React build, you can serve an index.html directly
65
+ @app.get("/", response_class=HTMLResponse)
66
+ async def serve_dashboard():
67
+ """
68
+ Serves the main DEFRAG.APP dashboard template.
69
+ Ensure index.html is located in the root or a 'templates' folder.
70
+ """
71
+ if os.path.exists("index.html"):
72
+ with open("index.html", "r") as f:
73
+ return f.read()
74
+ return """
75
+ <html>
76
+ <body style="background: black; color: cyan; font-family: monospace; display: flex; align-items: center; justify-content: center; height: 100vh;">
77
+ <h1>DEFRAG.APP // CORE ERROR: index.html not found in root.</h1>
78
+ </body>
79
+ </html>
80
+ """
81
+
82
+ # 4. HEALTH CHECK
83
+ @app.get("/health")
84
+ async def health_check():
85
+ return {"status": "online", "system": "DEFRAG.APP // CORE"}
86
+
87
+ if __name__ == "__main__":
88
+ import uvicorn
89
+ # Use 7860 for Hugging Face Spaces compatibility
90
+ uvicorn.run(app, host="0.0.0.0", port=7860)
91
+ ```
92
+
93
+ ### Next Steps for Implementation:
94
+ 1. **HF Secrets**: In your Hugging Face Space settings, add a new variable `ELEVENLABS_API_KEY` with your secret key. [elevenlabs](https://elevenlabs.io/app/agents/agents/agent_4101kfyny563e168da90yze4vva4?branchId=agtbrch_5801kfyny6tefd88a3krvcq388jz&tab=widget)
95
+ 2. **Frontend Deployment**: Place your compiled React/Next.js `index.html` (the one containing the `DefragOS` component we built) in the root of the project. [elevenlabs](https://elevenlabs.io/app/agents/agents/agent_4101kfyny563e168da90yze4vva4?branchId=agtbrch_5801kfyny6tefd88a3krvcq388jz&tab=widget)
96
+ 3. **Asset Management**: Ensure your `defrag_agent.riv` file is in a folder named `assets/` so the backend can serve it to the Rive component. [elevenlabs](https://elevenlabs.io/app/agents/agents/agent_4101kfyny563e168da90yze4vva4?branchId=agtbrch_5801kfyny6tefd88a3krvcq388jz&tab=widget)
97
+
98
+ This `app.py` serves as the secure backbone of your premium agent-first OS. [elevenlabs](https://elevenlabs.io/app/agents/agents/agent_4101kfyny563e168da90yze4vva4?branchId=agtbrch_5801kfyny6tefd88a3krvcq388jz&tab=widget)