Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,53 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
-
from pydantic import BaseModel
|
| 4 |
from huggingface_hub import InferenceClient
|
|
|
|
| 5 |
|
| 6 |
-
# Simple hosted model
|
| 7 |
MODEL_ID = "microsoft/Phi-3-mini-4k-instruct"
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
-
class HumanizeRequest(BaseModel):
|
| 13 |
-
text: str
|
| 14 |
-
|
| 15 |
-
# CORS so your Vercel frontend can call this API
|
| 16 |
app.add_middleware(
|
| 17 |
CORSMiddleware,
|
| 18 |
-
allow_origins=["*"],
|
| 19 |
allow_credentials=True,
|
| 20 |
allow_methods=["*"],
|
| 21 |
allow_headers=["*"],
|
| 22 |
)
|
| 23 |
|
| 24 |
@app.post("/api/humanize")
|
| 25 |
-
async def generate_humanized_text(request:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
prompt = (
|
| 27 |
"Humanize the following text, making it sound natural and engaging:\n\n"
|
| 28 |
-
f"{
|
| 29 |
)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
return {"result": response}
|
| 40 |
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Request
|
| 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 |
@app.post("/api/humanize")
|
| 21 |
+
async def generate_humanized_text(request: Request):
|
| 22 |
+
# 1. Read raw JSON body
|
| 23 |
+
try:
|
| 24 |
+
body = await request.json()
|
| 25 |
+
print("Incoming JSON:", body) # shows in Space logs
|
| 26 |
+
except Exception:
|
| 27 |
+
raise HTTPException(status_code=400, detail="Invalid JSON")
|
| 28 |
+
|
| 29 |
+
# 2. Extract "text"
|
| 30 |
+
text = body.get("text")
|
| 31 |
+
if not isinstance(text, str) or not text.strip():
|
| 32 |
+
raise HTTPException(status_code=400, detail="Field 'text' is required")
|
| 33 |
+
|
| 34 |
+
# 3. Build prompt and call model
|
| 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 |
+
print("HF error:", e)
|
| 50 |
+
raise HTTPException(status_code=500, detail=f"HF error: {e}")
|
| 51 |
|
| 52 |
return {"result": response}
|
| 53 |
|