ThongCoding commited on
Commit
d9d6b2c
·
1 Parent(s): 231cb7b
Files changed (3) hide show
  1. app.py +27 -4
  2. requirements.txt +3 -0
  3. tester.py +12 -23
app.py CHANGED
@@ -1,11 +1,34 @@
1
- from fastapi import FastAPI
2
  from pydantic import BaseModel
 
 
 
3
 
4
  app = FastAPI()
5
 
6
- class PromptRequest(BaseModel):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  prompt: str
8
 
9
  @app.post("/prompt")
10
- async def handle_prompt(request: PromptRequest):
11
- return {"message": f"Prompt received: {request.prompt}"}
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
  from pydantic import BaseModel
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ from llama_cpp import Llama
5
+ import os
6
 
7
  app = FastAPI()
8
 
9
+ # Load model
10
+ MODEL_PATH = "./models/gemma-2b-it.gguf"
11
+ llm = Llama(model_path=MODEL_PATH, n_ctx=512)
12
+
13
+ # Allow CORS (so frontend or Swagger can work)
14
+ app.add_middleware(
15
+ CORSMiddleware,
16
+ allow_origins=["*"], # change to frontend origin in production
17
+ allow_credentials=True,
18
+ allow_methods=["*"],
19
+ allow_headers=["*"],
20
+ )
21
+
22
+ # Input model
23
+ class PromptInput(BaseModel):
24
  prompt: str
25
 
26
  @app.post("/prompt")
27
+ async def generate_response(data: PromptInput):
28
+ output = llm(data.prompt, max_tokens=512, stop=["</s>", "\n\n"], echo=False)
29
+ return {"response": output["choices"][0]["text"].strip()}
30
+
31
+ # Healthcheck
32
+ @app.get("/")
33
+ def read_root():
34
+ return {"message": "AI Builder Backend running"}
requirements.txt CHANGED
@@ -1,2 +1,5 @@
1
  fastapi
2
  uvicorn
 
 
 
 
1
  fastapi
2
  uvicorn
3
+ llama-cpp-python==0.2.72 # prebuilt versions now available from PyPI
4
+ python-dotenv
5
+ huggingface_hub
tester.py CHANGED
@@ -1,25 +1,14 @@
1
  import requests
2
 
3
- prompt = """
4
- You are an AI builder assistant. Your task is to generate a 3D Minecraft structure layout for a "wizard tower" using only the blocks listed below. The layout must be provided in strict JSON format as:
5
- layout[y][z][x] = block_name (3D array of strings)
6
-
7
- The JSON must be minimal, symmetrical if needed, and represent a small but visually meaningful structure. Avoid empty arrays, and make sure block names match exactly.
8
-
9
- You could only use these blocks for building wizard tower, including:
10
- Animated blocks: lava, fire, end_portal, magma_block, soul_fire, bubble_column
11
- Non-animated blocks: cobblestone, mossy_cobblestone, stone_bricks, cracked_stone_bricks, deepslate, bookshelves, oak_planks, oak_log, torch, stone_slab, obsidian, glass_pane, ladder
12
-
13
- Only return a valid JSON structure named `layout`. No explanation or extra text.
14
- """
15
-
16
-
17
- response = requests.post(
18
- url="http://thongcoder-minecraft-ai-builder-backend.hf.space/prompt",
19
- json={"prompt": prompt},
20
- headers={"Content-Type": 'application/json',
21
- 'accept': 'application/json'}
22
- )
23
-
24
- print(response.status_code)
25
- print(response.json())
 
1
  import requests
2
 
3
+ url = "http://thongcoder-minecraft-ai-builder-backend.hf.space/prompt"
4
+ headers = {
5
+ "Content-Type": "application/json"
6
+ }
7
+ data = {
8
+ "prompt": "Design a small medieval gate using stone bricks and oak wood."
9
+ }
10
+
11
+ response = requests.post(url, headers=headers, json=data)
12
+
13
+ print("Status Code:", response.status_code)
14
+ print("Response:", response.text)