Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,58 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
-
import os
|
| 5 |
|
|
|
|
| 6 |
MODEL_ID = "microsoft/Phi-3-mini-4k-instruct"
|
| 7 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
| 8 |
client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
|
|
| 12 |
app.add_middleware(
|
| 13 |
CORSMiddleware,
|
| 14 |
-
allow_origins=["*"],
|
| 15 |
allow_credentials=True,
|
| 16 |
allow_methods=["*"],
|
| 17 |
allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
try:
|
| 24 |
-
body = await request.json()
|
| 25 |
-
except:
|
| 26 |
-
raise HTTPException(status_code=400, detail="Invalid JSON")
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
# 4. Generate output
|
| 37 |
try:
|
| 38 |
response = client.text_generation(
|
| 39 |
prompt,
|
| 40 |
max_new_tokens=512,
|
| 41 |
temperature=0.7,
|
|
|
|
|
|
|
| 42 |
)
|
| 43 |
except Exception as e:
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
return {"result": response}
|
| 47 |
|
|
|
|
| 48 |
@app.get("/")
|
| 49 |
def health():
|
| 50 |
return {"status": "ok"}
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
from huggingface_hub import InferenceClient
|
|
|
|
| 6 |
|
| 7 |
+
# --- Model & client setup ---
|
| 8 |
MODEL_ID = "microsoft/Phi-3-mini-4k-instruct"
|
| 9 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # set in Space secrets
|
| 10 |
+
|
| 11 |
client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)
|
| 12 |
|
| 13 |
app = FastAPI()
|
| 14 |
|
| 15 |
+
# --- CORS ---
|
| 16 |
app.add_middleware(
|
| 17 |
CORSMiddleware,
|
| 18 |
+
allow_origins=["*"], # later restrict to your domain
|
| 19 |
allow_credentials=True,
|
| 20 |
allow_methods=["*"],
|
| 21 |
allow_headers=["*"],
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# --- Request body schema ---
|
| 25 |
+
class HumanizeRequest(BaseModel):
|
| 26 |
+
text: str
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# --- Endpoint ---
|
| 29 |
+
@app.post("/api/humanize")
|
| 30 |
+
async def generate_humanized_text(body: HumanizeRequest):
|
| 31 |
+
text = body.text
|
| 32 |
+
if not text.strip():
|
| 33 |
+
raise HTTPException(status_code=400, detail="Text cannot be empty")
|
| 34 |
|
| 35 |
+
prompt = (
|
| 36 |
+
"Humanize the following text, making it sound natural and engaging:\n\n"
|
| 37 |
+
f"{text}"
|
| 38 |
+
)
|
| 39 |
|
|
|
|
| 40 |
try:
|
| 41 |
response = client.text_generation(
|
| 42 |
prompt,
|
| 43 |
max_new_tokens=512,
|
| 44 |
temperature=0.7,
|
| 45 |
+
top_p=0.9,
|
| 46 |
+
repetition_penalty=1.1,
|
| 47 |
)
|
| 48 |
except Exception as e:
|
| 49 |
+
# This will show up in Space logs
|
| 50 |
+
print("HF error:", e)
|
| 51 |
+
raise HTTPException(status_code=500, detail=f"HF error: {e}")
|
| 52 |
|
| 53 |
return {"result": response}
|
| 54 |
|
| 55 |
+
# --- Health check ---
|
| 56 |
@app.get("/")
|
| 57 |
def health():
|
| 58 |
return {"status": "ok"}
|