File size: 2,213 Bytes
c989e45
8d85a2c
 
 
c989e45
 
2fc68b4
deb83c9
c989e45
 
 
 
74d601a
6bf37cd
c989e45
8d85a2c
 
c989e45
8d85a2c
 
 
 
c989e45
8d85a2c
c989e45
 
 
deb83c9
c989e45
 
2fc68b4
c989e45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fc68b4
8d85a2c
2fc68b4
c989e45
8d85a2c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import curl_cffi
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import os

import uvicorn

# Load Gemini API key from secret
GEM_API_KEY = os.getenv('GEMINI_API_KEY')

# Init app
app = FastAPI()

# CORS (allow frontend)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Optional: restrict in prod
    allow_methods=["*"],
    allow_headers=["*"],
)

# Request model
class PromptRequest(BaseModel):
    structure_name: str
    structure_desc: str
    blocks_allowed: list[str]  # Enforce lowercase block names client-side

@app.post("/generate_structure")
async def generate_with_gemini(req: PromptRequest):
    try:
        # Compose the extremely strict prompt
        prompt = f"""You are a Minecraft-style structure planner. You have the curiosity to build almost anything you could think of. {req.structure_name}

{req.structure_desc}

Only output a JSON object describing a 3D structure using this format:

{{
  "width": <int>,
  "height": <int>,
  "depth": <int>,
  "layers": [
    [
      ["stone", "stone", "stone"],
      ["stone", "air",   "stone"],
      ["stone", "stone", "stone"]
    ],
    ...
  ]
}}

Only use lowercase Minecraft block IDs (e.g. "stone", "air", "glass", "planks").
You could only build this structure using {" ".join(req.blocks_allowed)}
Do not include any natural language or explanation.
Output strictly valid JSON only."""

        # Call Gemini API using curl_cffi
        session = curl_cffi.Session(impersonate="chrome110")  # Optional impersonation
        response = session.post(
            url="https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent",
            headers={
                "x-goog-api-key": GEM_API_KEY,
                "Content-Type": "application/json"
            },
            json={
                "contents": [{
                    "parts": [{
                        "text": prompt
                    }]
                }]
            }
        )

        return {"response": response.text}

    except Exception as e:
        return {"error": str(e)}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)