File size: 852 Bytes
3da5de2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import subprocess
import os

app = FastAPI()

# Allow all origins (replace with your website domain in production)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.post("/generate")
async def generate_text(prompt: str):
    try:
        # Run llama.cpp in interactive mode
        cmd = [
            "./llama.cpp/main",
            "-m", "model.gguf",
            "-p", prompt,
            "-n", "128",  # Max tokens
            "-t", "4"     # Threads (adjust based on CPU)
        ]
        result = subprocess.run(cmd, capture_output=True, text=True)
        return {"response": result.stdout}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))