Commit
·
d9d6b2c
1
Parent(s):
231cb7b
wdsa
Browse files- app.py +27 -4
- requirements.txt +3 -0
- 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
prompt: str
|
| 8 |
|
| 9 |
@app.post("/prompt")
|
| 10 |
-
async def
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|