Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
3 |
+
import subprocess
|
4 |
+
import os
|
5 |
+
|
6 |
+
app = FastAPI()
|
7 |
+
|
8 |
+
# Allow all origins (replace with your website domain in production)
|
9 |
+
app.add_middleware(
|
10 |
+
CORSMiddleware,
|
11 |
+
allow_origins=["*"],
|
12 |
+
allow_methods=["*"],
|
13 |
+
allow_headers=["*"],
|
14 |
+
)
|
15 |
+
|
16 |
+
@app.post("/generate")
|
17 |
+
async def generate_text(prompt: str):
|
18 |
+
try:
|
19 |
+
# Run llama.cpp in interactive mode
|
20 |
+
cmd = [
|
21 |
+
"./llama.cpp/main",
|
22 |
+
"-m", "model.gguf",
|
23 |
+
"-p", prompt,
|
24 |
+
"-n", "128", # Max tokens
|
25 |
+
"-t", "4" # Threads (adjust based on CPU)
|
26 |
+
]
|
27 |
+
result = subprocess.run(cmd, capture_output=True, text=True)
|
28 |
+
return {"response": result.stdout}
|
29 |
+
except Exception as e:
|
30 |
+
raise HTTPException(status_code=500, detail=str(e))
|