Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# server.py
|
| 2 |
+
from fastapi import FastAPI, Request
|
| 3 |
+
from fastapi.responses import JSONResponse
|
| 4 |
+
import httpx
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
HF_MODEL_URL = "https://api-inference.huggingface.co/models/facebook/opt-1.3b"
|
| 10 |
+
|
| 11 |
+
@app.post("/ai")
|
| 12 |
+
async def ai_endpoint(request: Request):
|
| 13 |
+
"""
|
| 14 |
+
Receives the Geometry Dash editor state as JSON:
|
| 15 |
+
{ "state": "<description of editor>" }
|
| 16 |
+
Returns a JSON action:
|
| 17 |
+
{ "mouse_x": float, "mouse_y": float, "click": bool }
|
| 18 |
+
"""
|
| 19 |
+
try:
|
| 20 |
+
data = await request.json()
|
| 21 |
+
editor_state = data.get("state", "No state provided")
|
| 22 |
+
|
| 23 |
+
# Build prompt to instruct the model to respond with JSON for mouse actions
|
| 24 |
+
prompt = (
|
| 25 |
+
f"You are an AI controlling the Geometry Dash editor.\n"
|
| 26 |
+
f"Respond ONLY with JSON in this exact format:\n"
|
| 27 |
+
f'{{"mouse_x": <float>, "mouse_y": <float>, "click": <true/false>}}\n'
|
| 28 |
+
f"Example: {{\"mouse_x\": 100.0, \"mouse_y\": 200.0, \"click\": true}}\n"
|
| 29 |
+
f"Editor state: {editor_state}"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Send POST request to Hugging Face inference endpoint
|
| 33 |
+
async with httpx.AsyncClient(timeout=30.0) as client:
|
| 34 |
+
response = await client.post(
|
| 35 |
+
HF_MODEL_URL,
|
| 36 |
+
headers={"Content-Type": "application/json"},
|
| 37 |
+
json={"inputs": prompt}
|
| 38 |
+
)
|
| 39 |
+
result = response.json()
|
| 40 |
+
|
| 41 |
+
# Extract text output from Hugging Face response
|
| 42 |
+
text_output = ""
|
| 43 |
+
if isinstance(result, list) and len(result) > 0 and "generated_text" in result[0]:
|
| 44 |
+
text_output = result[0]["generated_text"]
|
| 45 |
+
|
| 46 |
+
# Attempt to parse JSON from model output
|
| 47 |
+
try:
|
| 48 |
+
# Extract first JSON object from text output
|
| 49 |
+
json_start = text_output.find("{")
|
| 50 |
+
json_end = text_output.rfind("}") + 1
|
| 51 |
+
json_str = text_output[json_start:json_end]
|
| 52 |
+
action = json.loads(json_str)
|
| 53 |
+
except Exception:
|
| 54 |
+
# Fallback if parsing fails
|
| 55 |
+
action = {"mouse_x": 150.0, "mouse_y": 100.0, "click": True}
|
| 56 |
+
|
| 57 |
+
return JSONResponse(content=action)
|
| 58 |
+
|
| 59 |
+
except Exception as e:
|
| 60 |
+
# Return default safe action on error
|
| 61 |
+
return JSONResponse(content={"mouse_x": 0.0, "mouse_y": 0.0, "click": False, "error": str(e)})
|
| 62 |
+
|
| 63 |
+
# Run server locally for testing
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
import uvicorn
|
| 66 |
+
uvicorn.run("server:app", host="0.0.0.0", port=7860, reload=True)
|