Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,169 +0,0 @@
|
|
| 1 |
-
import os, httpx
|
| 2 |
-
from fastapi import FastAPI, Request
|
| 3 |
-
from fastapi.responses import FileResponse, JSONResponse
|
| 4 |
-
from fastapi.staticfiles import StaticFiles
|
| 5 |
-
|
| 6 |
-
app = FastAPI()
|
| 7 |
-
|
| 8 |
-
# ── API Keys from HF Secrets ──
|
| 9 |
-
GROQ_KEY = os.getenv("GROQ_API_KEY", "")
|
| 10 |
-
NEMOTRON_KEY = os.getenv("NEMOTRON_API_KEY", "")
|
| 11 |
-
OPENROUTER_KEY = os.getenv("OPENROUTER_API_KEY", "")
|
| 12 |
-
|
| 13 |
-
# ── System prompts for each AI role ──
|
| 14 |
-
|
| 15 |
-
GROQ_SYSTEM = """You are FORGE3D Speed AI. Generate fast, simple Three.js scene code.
|
| 16 |
-
ONLY output raw JavaScript. No markdown, no explanation, no backticks.
|
| 17 |
-
|
| 18 |
-
Available functions:
|
| 19 |
-
- addObj('cube'|'sphere'|'cylinder'|'plane'|'cone'|'torus'|'ico') → obj
|
| 20 |
-
- addLight('point'|'sun'|'spot') → obj
|
| 21 |
-
- obj.mesh.position.set(x, y, z)
|
| 22 |
-
- obj.mesh.scale.set(x, y, z)
|
| 23 |
-
- obj.mesh.rotation.set(x, y, z)
|
| 24 |
-
- obj.mesh.material.color.set('#hex')
|
| 25 |
-
- obj.mesh.material.metalness = 0..1
|
| 26 |
-
- obj.mesh.material.roughness = 0..1
|
| 27 |
-
- obj.mesh.material.emissiveIntensity = 0..3
|
| 28 |
-
- obj.mesh.material.emissive.set('#hex')
|
| 29 |
-
- act('del') — delete selected
|
| 30 |
-
- toast('message')
|
| 31 |
-
|
| 32 |
-
Rules:
|
| 33 |
-
- Max 5 objects for speed
|
| 34 |
-
- Always end with toast() describing what you made
|
| 35 |
-
- Use realistic positions (y=1 for objects above ground)"""
|
| 36 |
-
|
| 37 |
-
NEMOTRON_SYSTEM = """You are FORGE3D Scene Designer AI powered by NVIDIA Nemotron.
|
| 38 |
-
You design complex, multi-object 3D scenes and environments.
|
| 39 |
-
ONLY output raw JavaScript. No markdown, no explanation, no backticks.
|
| 40 |
-
|
| 41 |
-
Available functions:
|
| 42 |
-
- addObj('cube'|'sphere'|'cylinder'|'plane'|'cone'|'torus'|'ico') → obj
|
| 43 |
-
- addLight('point'|'sun'|'spot') → obj
|
| 44 |
-
- obj.mesh.position.set(x, y, z)
|
| 45 |
-
- obj.mesh.scale.set(x, y, z)
|
| 46 |
-
- obj.mesh.rotation.set(x, y, z)
|
| 47 |
-
- obj.mesh.material.color.set('#hex')
|
| 48 |
-
- obj.mesh.material.metalness = 0..1
|
| 49 |
-
- obj.mesh.material.roughness = 0..1
|
| 50 |
-
- obj.mesh.material.emissiveIntensity = 0..3
|
| 51 |
-
- obj.mesh.material.emissive.set('#hex')
|
| 52 |
-
- obj.mesh.material.transparent = true
|
| 53 |
-
- obj.mesh.material.opacity = 0..1
|
| 54 |
-
- act('del')
|
| 55 |
-
- toast('message')
|
| 56 |
-
|
| 57 |
-
Rules:
|
| 58 |
-
- Create complete scenes with 10-30 objects
|
| 59 |
-
- Use loops for repetitive elements (buildings, trees, pillars)
|
| 60 |
-
- Plan layout carefully: ground plane, multiple object types, varied scales
|
| 61 |
-
- Add at least 2 lights for atmosphere
|
| 62 |
-
- Always end with toast() summarizing the scene
|
| 63 |
-
- Use THREE.MathUtils.degToRad() for rotations if needed"""
|
| 64 |
-
|
| 65 |
-
OPENROUTER_SYSTEM = """You are FORGE3D High-Detail AI. You generate complex procedural 3D geometry using Three.js math.
|
| 66 |
-
ONLY output raw JavaScript. No markdown, no explanation, no backticks.
|
| 67 |
-
|
| 68 |
-
Available globals: THREE, SCENE, addObj, addLight, toast, OBJS
|
| 69 |
-
|
| 70 |
-
You CAN also use raw Three.js for high-detail work:
|
| 71 |
-
- new THREE.BufferGeometry()
|
| 72 |
-
- THREE.BufferAttribute
|
| 73 |
-
- new THREE.Mesh(geo, mat)
|
| 74 |
-
- SCENE.add(mesh)
|
| 75 |
-
- Math functions: Math.sin, Math.cos, Math.PI, Math.random
|
| 76 |
-
|
| 77 |
-
For standard objects use addObj(), for custom geometry use THREE directly.
|
| 78 |
-
|
| 79 |
-
Material setup for custom meshes:
|
| 80 |
-
const mat = new THREE.MeshStandardMaterial({color:'#hex', metalness:0, roughness:0.5});
|
| 81 |
-
|
| 82 |
-
Push custom meshes to OBJS so they appear in outliner:
|
| 83 |
-
OBJS.push({mesh, light:null, name:'MyShape', type:'MESH', vis:true, kf:{}});
|
| 84 |
-
|
| 85 |
-
Rules:
|
| 86 |
-
- For 'high detail' requests: generate procedural geometry with 500-5000 vertices
|
| 87 |
-
- Use parametric equations for organic shapes
|
| 88 |
-
- Create proper UV maps when possible
|
| 89 |
-
- Always call SCENE.add(mesh) for custom geometry
|
| 90 |
-
- End with toast() describing what was generated
|
| 91 |
-
- For spiral/helix/fractal/organic shapes, use vertex manipulation loops"""
|
| 92 |
-
|
| 93 |
-
# ── Proxy endpoints ──
|
| 94 |
-
|
| 95 |
-
@app.post("/api/groq")
|
| 96 |
-
async def proxy_groq(request: Request):
|
| 97 |
-
if not GROQ_KEY:
|
| 98 |
-
return JSONResponse({"error": "GROQ_API_KEY not set in Space secrets"}, status_code=500)
|
| 99 |
-
body = await request.json()
|
| 100 |
-
body.setdefault("model", "llama-3.3-70b-versatile")
|
| 101 |
-
body.setdefault("max_tokens", 500)
|
| 102 |
-
body.setdefault("temperature", 0.3)
|
| 103 |
-
# Inject system prompt
|
| 104 |
-
body["messages"] = [{"role":"system","content":GROQ_SYSTEM}] + body.get("messages",[])
|
| 105 |
-
async with httpx.AsyncClient(timeout=30) as client:
|
| 106 |
-
r = await client.post(
|
| 107 |
-
"https://api.groq.com/openai/v1/chat/completions",
|
| 108 |
-
headers={"Authorization": f"Bearer {GROQ_KEY}", "Content-Type": "application/json"},
|
| 109 |
-
json=body
|
| 110 |
-
)
|
| 111 |
-
return JSONResponse(r.json(), status_code=r.status_code)
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
@app.post("/api/nemotron")
|
| 115 |
-
async def proxy_nemotron(request: Request):
|
| 116 |
-
if not NEMOTRON_KEY:
|
| 117 |
-
return JSONResponse({"error": "NEMOTRON_API_KEY not set in Space secrets"}, status_code=500)
|
| 118 |
-
body = await request.json()
|
| 119 |
-
body.setdefault("model", "nvidia/llama-3.1-nemotron-70b-instruct")
|
| 120 |
-
body.setdefault("max_tokens", 1200)
|
| 121 |
-
body.setdefault("temperature", 0.4)
|
| 122 |
-
body["messages"] = [{"role":"system","content":NEMOTRON_SYSTEM}] + body.get("messages",[])
|
| 123 |
-
async with httpx.AsyncClient(timeout=60) as client:
|
| 124 |
-
r = await client.post(
|
| 125 |
-
"https://integrate.api.nvidia.com/v1/chat/completions",
|
| 126 |
-
headers={"Authorization": f"Bearer {NEMOTRON_KEY}", "Content-Type": "application/json"},
|
| 127 |
-
json=body
|
| 128 |
-
)
|
| 129 |
-
return JSONResponse(r.json(), status_code=r.status_code)
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
@app.post("/api/openrouter")
|
| 133 |
-
async def proxy_openrouter(request: Request):
|
| 134 |
-
if not OPENROUTER_KEY:
|
| 135 |
-
return JSONResponse({"error": "OPENROUTER_API_KEY not set in Space secrets"}, status_code=500)
|
| 136 |
-
body = await request.json()
|
| 137 |
-
body.setdefault("model", "deepseek/deepseek-r1:free")
|
| 138 |
-
body.setdefault("max_tokens", 2000)
|
| 139 |
-
body.setdefault("temperature", 0.2)
|
| 140 |
-
body["messages"] = [{"role":"system","content":OPENROUTER_SYSTEM}] + body.get("messages",[])
|
| 141 |
-
async with httpx.AsyncClient(timeout=90) as client:
|
| 142 |
-
r = await client.post(
|
| 143 |
-
"https://openrouter.ai/api/v1/chat/completions",
|
| 144 |
-
headers={
|
| 145 |
-
"Authorization": f"Bearer {OPENROUTER_KEY}",
|
| 146 |
-
"Content-Type": "application/json",
|
| 147 |
-
"HTTP-Referer": "https://huggingface.co/spaces",
|
| 148 |
-
"X-Title": "FORGE3D"
|
| 149 |
-
},
|
| 150 |
-
json=body
|
| 151 |
-
)
|
| 152 |
-
return JSONResponse(r.json(), status_code=r.status_code)
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
@app.get("/api/status")
|
| 156 |
-
async def status():
|
| 157 |
-
return {
|
| 158 |
-
"groq": "✅ Ready" if GROQ_KEY else "❌ Missing GROQ_API_KEY",
|
| 159 |
-
"nemotron": "✅ Ready" if NEMOTRON_KEY else "❌ Missing NEMOTRON_API_KEY",
|
| 160 |
-
"openrouter": "✅ Ready" if OPENROUTER_KEY else "❌ Missing OPENROUTER_API_KEY",
|
| 161 |
-
}
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
# ── Serve static files ──
|
| 165 |
-
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 166 |
-
|
| 167 |
-
@app.get("/")
|
| 168 |
-
async def root():
|
| 169 |
-
return FileResponse("static/index.html")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|