Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- main.py +97 -0
- requirements.txt +6 -0
main.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from fastapi.responses import FileResponse, JSONResponse
|
3 |
+
from pydantic import BaseModel
|
4 |
+
import uuid
|
5 |
+
import os
|
6 |
+
import glob
|
7 |
+
import re
|
8 |
+
import pypandoc
|
9 |
+
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
# Control de descargas por archivo
|
13 |
+
descargas = {}
|
14 |
+
|
15 |
+
|
16 |
+
class MarkdownInput(BaseModel):
|
17 |
+
content: str
|
18 |
+
|
19 |
+
|
20 |
+
def limpiar_lineas_hr(markdown_text: str) -> str:
|
21 |
+
return re.sub(r'^\s*---\s*$', '\n', markdown_text, flags=re.MULTILINE)
|
22 |
+
|
23 |
+
|
24 |
+
def save_markdown_to_file(content: str, path: str):
|
25 |
+
with open(path, "w", encoding="utf-8") as f:
|
26 |
+
f.write(content)
|
27 |
+
|
28 |
+
|
29 |
+
def convert_markdown_to_docx(input_md: str, output_docx: str):
|
30 |
+
pypandoc.convert_file(
|
31 |
+
source_file=input_md,
|
32 |
+
to="docx",
|
33 |
+
format="md",
|
34 |
+
outputfile=output_docx,
|
35 |
+
extra_args=["--mathjax"]
|
36 |
+
)
|
37 |
+
|
38 |
+
|
39 |
+
def limpieza():
|
40 |
+
for ext in ["*.docx", "*.md"]:
|
41 |
+
for file in glob.glob(ext):
|
42 |
+
try:
|
43 |
+
os.remove(file)
|
44 |
+
except Exception as e:
|
45 |
+
print(f"No se pudo eliminar {file}: {e}")
|
46 |
+
|
47 |
+
|
48 |
+
@app.post("/convert/")
|
49 |
+
def convert(data: MarkdownInput):
|
50 |
+
try:
|
51 |
+
limpieza()
|
52 |
+
uid = str(uuid.uuid4())
|
53 |
+
input_md = f"{uid}.md"
|
54 |
+
output_docx = f"{uid}.docx"
|
55 |
+
|
56 |
+
contenido_limpio = limpiar_lineas_hr(data.content)
|
57 |
+
save_markdown_to_file(contenido_limpio, input_md)
|
58 |
+
convert_markdown_to_docx(input_md, output_docx)
|
59 |
+
os.remove(input_md)
|
60 |
+
|
61 |
+
descargas[uid] = 0
|
62 |
+
return {"message": "Archivo generado exitosamente", "url": f"/download/{uid}"}
|
63 |
+
|
64 |
+
except Exception as e:
|
65 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
66 |
+
|
67 |
+
|
68 |
+
@app.get("/download/{file_id}")
|
69 |
+
def download(file_id: str):
|
70 |
+
output_docx = f"{file_id}.docx"
|
71 |
+
|
72 |
+
if not os.path.exists(output_docx):
|
73 |
+
return JSONResponse(
|
74 |
+
content={"error": "El archivo no existe o ya fue eliminado."},
|
75 |
+
status_code=404
|
76 |
+
)
|
77 |
+
|
78 |
+
if file_id not in descargas:
|
79 |
+
return JSONResponse(
|
80 |
+
content={"error": "ID de archivo no válido."},
|
81 |
+
status_code=400
|
82 |
+
)
|
83 |
+
|
84 |
+
if descargas[file_id] >= 2:
|
85 |
+
os.remove(output_docx)
|
86 |
+
del descargas[file_id]
|
87 |
+
return JSONResponse(
|
88 |
+
content={"error": "El archivo ya no está disponible. Genere uno nuevo."},
|
89 |
+
status_code=410
|
90 |
+
)
|
91 |
+
|
92 |
+
descargas[file_id] += 1
|
93 |
+
return FileResponse(
|
94 |
+
path=output_docx,
|
95 |
+
filename="material_educativo.docx",
|
96 |
+
media_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
97 |
+
)
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
pydantic
|
4 |
+
pypandoc
|
5 |
+
markdown
|
6 |
+
beautifulsoup4
|