Spaces:
Runtime error
Runtime error
File size: 1,234 Bytes
9e7dc23 ea2133c ff38dbb ea2133c ff38dbb ea2133c ff38dbb ea2133c 9e7dc23 ea2133c ff38dbb ea2133c ff38dbb ea2133c |
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import os
from fastapi import FastAPI, Header, HTTPException, Depends
from pydantic import BaseModel
from text_humanizer import TextHumanizer, download_nltk_resources
import spacy
API_KEY = os.environ.get("API_KEY", "dev-key")
PORT = int(os.environ.get("PORT", 7860))
app = FastAPI()
humanizer = None
class HumanizeReq(BaseModel):
text: str
use_passive: bool = False
use_synonyms: bool = False
def verify_key(x_api_key: str = Header(None)):
if x_api_key != API_KEY:
raise HTTPException(status_code=403, detail="Forbidden")
return True
@app.get("/")
def greet_json():
return {"Hello": "World!"}
@app.on_event("startup")
def startup():
# ensure NLTK resources and spacy model are available at runtime
download_nltk_resources()
try:
spacy.load("en_core_web_sm")
except OSError:
import spacy.cli
spacy.cli.download("en_core_web_sm")
global humanizer
humanizer = TextHumanizer()
@app.post("/humanize")
def humanize(req: HumanizeReq, _=Depends(verify_key)):
return {"humanized": humanizer.humanize_text(req.text, req.use_passive, req.use_synonyms)}
# if __name__ == "__main__":
# import uvicorn
# uvicorn.run(app, host="0.0.0.0", port=PORT) |