Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,58 @@
|
|
| 1 |
-
import os
|
| 2 |
from fastapi import FastAPI, HTTPException
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from pydantic import BaseModel
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
app = FastAPI()
|
| 14 |
|
| 15 |
-
# --- CORS ---
|
| 16 |
app.add_middleware(
|
| 17 |
CORSMiddleware,
|
| 18 |
-
allow_origins=["*"],
|
| 19 |
-
allow_credentials=True,
|
| 20 |
allow_methods=["*"],
|
| 21 |
allow_headers=["*"],
|
| 22 |
)
|
| 23 |
|
| 24 |
-
|
| 25 |
-
class HumanizeRequest(BaseModel):
|
| 26 |
text: str
|
| 27 |
|
| 28 |
-
# --- Endpoint ---
|
| 29 |
@app.post("/api/humanize")
|
| 30 |
-
async def
|
| 31 |
-
text = body.text
|
| 32 |
-
if not text
|
| 33 |
raise HTTPException(status_code=400, detail="Text cannot be empty")
|
| 34 |
|
| 35 |
prompt = (
|
| 36 |
-
"Humanize the following text
|
| 37 |
-
f"{text}"
|
| 38 |
)
|
| 39 |
|
| 40 |
try:
|
| 41 |
-
|
| 42 |
prompt,
|
| 43 |
-
|
| 44 |
temperature=0.7,
|
| 45 |
-
top_p=0.9,
|
| 46 |
-
repetition_penalty=1.1,
|
| 47 |
)
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
print("HF error:", e)
|
| 51 |
-
raise HTTPException(status_code=500, detail=f"HF error: {e}")
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
# --- Health check ---
|
| 56 |
@app.get("/")
|
| 57 |
def health():
|
| 58 |
-
return {"status": "ok"}
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
| 4 |
+
from llama_cpp import Llama
|
| 5 |
+
|
| 6 |
+
# GGUF MODEL
|
| 7 |
+
MODEL_REPO = "bartowski/Phi-3.1-mini-4k-instruct-GGUF"
|
| 8 |
+
MODEL_FILE = "Phi-3.1-mini-4k-instruct-Q4_K_M.gguf"
|
| 9 |
+
|
| 10 |
+
print("Loading Phi-3.1 Mini GGUF model...")
|
| 11 |
+
llm = Llama.from_pretrained(
|
| 12 |
+
repo_id=MODEL_REPO,
|
| 13 |
+
filename=MODEL_FILE,
|
| 14 |
+
n_threads=4,
|
| 15 |
+
n_ctx=4096,
|
| 16 |
+
verbose=True,
|
| 17 |
+
)
|
| 18 |
+
print("Model loaded successfully.")
|
| 19 |
|
| 20 |
app = FastAPI()
|
| 21 |
|
|
|
|
| 22 |
app.add_middleware(
|
| 23 |
CORSMiddleware,
|
| 24 |
+
allow_origins=["*"],
|
|
|
|
| 25 |
allow_methods=["*"],
|
| 26 |
allow_headers=["*"],
|
| 27 |
)
|
| 28 |
|
| 29 |
+
class RequestBody(BaseModel):
|
|
|
|
| 30 |
text: str
|
| 31 |
|
|
|
|
| 32 |
@app.post("/api/humanize")
|
| 33 |
+
async def humanize(body: RequestBody):
|
| 34 |
+
text = body.text.strip()
|
| 35 |
+
if not text:
|
| 36 |
raise HTTPException(status_code=400, detail="Text cannot be empty")
|
| 37 |
|
| 38 |
prompt = (
|
| 39 |
+
"Humanize the following text while keeping meaning:\n\n"
|
| 40 |
+
f"{text}\n\nHumanized version:\n"
|
| 41 |
)
|
| 42 |
|
| 43 |
try:
|
| 44 |
+
output = llm(
|
| 45 |
prompt,
|
| 46 |
+
max_tokens=300,
|
| 47 |
temperature=0.7,
|
|
|
|
|
|
|
| 48 |
)
|
| 49 |
+
result = output["choices"][0]["text"].strip()
|
| 50 |
+
return {"result": result}
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
except Exception as e:
|
| 53 |
+
print("Model error:", e)
|
| 54 |
+
raise HTTPException(status_code=500, detail="Model execution error")
|
| 55 |
|
|
|
|
| 56 |
@app.get("/")
|
| 57 |
def health():
|
| 58 |
+
return {"status": "ok"}
|