Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Dateparser Server for HuggingFace Spaces."""
|
| 2 |
+
import os
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
import dateparser
|
| 6 |
+
|
| 7 |
+
LOCALE = os.environ.get("LOCALE", "pt-BR")
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class ParseRequest(BaseModel):
|
| 13 |
+
text: str
|
| 14 |
+
locale: str | None = None
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class ParseBatchRequest(BaseModel):
|
| 18 |
+
texts: list[str]
|
| 19 |
+
locale: str | None = None
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@app.get("/health")
|
| 23 |
+
def health():
|
| 24 |
+
return {"status": "ok", "locale": LOCALE}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@app.post("/parse")
|
| 28 |
+
def parse(req: ParseRequest):
|
| 29 |
+
locale = req.locale or LOCALE
|
| 30 |
+
result = dateparser.parse(req.text, languages=[locale.split("-")[0]])
|
| 31 |
+
return {
|
| 32 |
+
"input": req.text,
|
| 33 |
+
"parsed": result.isoformat() if result else None,
|
| 34 |
+
"locale": locale,
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@app.post("/parse_batch")
|
| 39 |
+
def parse_batch(req: ParseBatchRequest):
|
| 40 |
+
locale = req.locale or LOCALE
|
| 41 |
+
lang = locale.split("-")[0]
|
| 42 |
+
results = []
|
| 43 |
+
for text in req.texts:
|
| 44 |
+
result = dateparser.parse(text, languages=[lang])
|
| 45 |
+
results.append({
|
| 46 |
+
"input": text,
|
| 47 |
+
"parsed": result.isoformat() if result else None,
|
| 48 |
+
})
|
| 49 |
+
return {"results": results, "locale": locale}
|