Spaces:
Sleeping
Sleeping
Download app.py from andregprospero/api-pop-final: direct link, hf CLI and curl.
- Browser
- Download file 1.15 kB
-
https://huggingface.co/spaces/andregprospero/api-pop-final/resolve/main/app.py
- Command line
-
hf download hf://spaces/andregprospero/api-pop-final/app.py
-
curl -L -o app.py https://huggingface.co/spaces/andregprospero/api-pop-final/resolve/main/app.py
1.15 kB
| from fastapi import FastAPI, Request | |
| from fastapi.responses import PlainTextResponse, JSONResponse, FileResponse | |
| from docxtpl import DocxTemplate | |
| from pathlib import Path | |
| import uuid | |
| app = FastAPI() | |
| def root(): | |
| return PlainTextResponse("OK") | |
| async def generate_pop(request: Request): | |
| data = await request.json() | |
| # Gera o arquivo no /tmp | |
| filename = f"POP_{uuid.uuid4().hex[:8]}.docx" | |
| filepath = Path("/tmp") / filename | |
| doc = DocxTemplate("Modelo_POP_v1.docx") | |
| doc.render(data) | |
| doc.save(filepath) | |
| # Retorna a URL para o arquivo | |
| file_url = f"https://andregprospero-api-pop-final.hf.space/files/{filename}" | |
| return JSONResponse(content={"url": file_url}) | |
| def serve_file(filename: str): | |
| filepath = Path("/tmp") / filename | |
| if filepath.exists(): | |
| return FileResponse( | |
| path=filepath, | |
| media_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document", | |
| filename=filename | |
| ) | |
| else: | |
| return PlainTextResponse("Arquivo não encontrado", status_code=404) | |