Spaces:
Sleeping
Sleeping
File size: 5,648 Bytes
514921e 2d9b9ed 514921e 4a91e42 514921e 4a91e42 514921e 4a91e42 514921e 4a91e42 514921e 2d9b9ed 514921e 4a91e42 514921e 4a91e42 514921e 2d9b9ed 514921e ca49f0f 4a91e42 ca49f0f 514921e 4a91e42 514921e 2d9b9ed 514921e 4a91e42 514921e 2d9b9ed 514921e 4a91e42 514921e 4a91e42 514921e 4a91e42 514921e 4a91e42 514921e | 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | from fastapi import FastAPI, File, UploadFile, Form, HTTPException
from fastapi.responses import FileResponse, HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi import BackgroundTasks
import os
import tempfile
import re
import json
from pathlib import Path
# Import your conversion function from meta.py
from meta import process_excel_to_word
app = FastAPI(title="QCM Converter API - META")
# Enable CORS for all origins (you can restrict this in production)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def validate_hex_color(color: str) -> bool:
"""Validate hex color format"""
pattern = r'^[0-9A-Fa-f]{6}$'
return bool(re.match(pattern, color))
@app.get("/", response_class=HTMLResponse)
async def root():
"""Serve the HTML interface"""
html_path = Path(__file__).parent / "index.html"
if html_path.exists():
return html_path.read_text()
return """
<html>
<body>
<h1>QCM Converter API - META</h1>
<p>META Version: Answer tables only at the end of each module</p>
<p>Upload your Excel files at <a href="/docs">/docs</a></p>
</body>
</html>
"""
@app.post("/convert")
async def convert_file(
background_tasks: BackgroundTasks,
file: UploadFile = File(...),
images: UploadFile = File(None), # Optional ZIP file with images
use_two_columns: bool = Form(True),
add_separator_line: bool = Form(True),
theme_color: str = Form("5FFFDF"),
highlight_words: str = Form(None) # JSON string of words to highlight
):
"""
Convert Excel QCM file to Word document (META version)
META Version Features:
- NO empty tables after each course
- ONLY answer tables at the end of each module
Parameters:
- file: Excel file (.xlsx)
- images: Optional ZIP file containing images
- use_two_columns: Use two-column layout
- add_separator_line: Add separator line between columns
- theme_color: Hex color code (without #) e.g., "5FFFDF"
- highlight_words: JSON array of words to highlight (e.g., '["word1", "word2"]')
"""
# Validate file extension
if not file.filename.endswith('.xlsx'):
raise HTTPException(status_code=400, detail="Only .xlsx files are supported")
# Validate color
if not validate_hex_color(theme_color):
raise HTTPException(
status_code=400,
detail="Invalid color format. Use 6-character hex code (e.g., '5FFFDF')"
)
original_name = Path(file.filename).stem
temp_dir = tempfile.mkdtemp()
temp_input_path = os.path.join(temp_dir, f"{original_name}.xlsx")
# Save the Excel file
with open(temp_input_path, "wb") as f:
f.write(await file.read())
# Handle optional image ZIP file
temp_images_path = None
if images and images.filename:
if not images.filename.endswith('.zip'):
cleanup_files(temp_input_path)
raise HTTPException(status_code=400, detail="Images must be in a ZIP file")
temp_images_path = os.path.join(temp_dir, "images.zip")
with open(temp_images_path, "wb") as f:
f.write(await images.read())
output_filename = file.filename.replace('.xlsx', '_converted.docx')
temp_output_path = tempfile.mktemp(suffix='.docx')
# Parse highlight words from JSON string
highlight_words_list = []
if highlight_words:
try:
highlight_words_list = json.loads(highlight_words)
if not isinstance(highlight_words_list, list):
highlight_words_list = []
except json.JSONDecodeError:
# If it's not valid JSON, treat it as empty list
highlight_words_list = []
try:
process_excel_to_word(
excel_file_path=temp_input_path,
output_word_path=temp_output_path,
image_folder=temp_images_path, # Can be None
display_name=None,
use_two_columns=use_two_columns,
add_separator_line=add_separator_line,
balance_method="dynamic",
theme_hex=theme_color,
highlight_words=highlight_words_list
)
# Schedule cleanup as a background task
files_to_cleanup = [temp_input_path, temp_output_path]
if temp_images_path:
files_to_cleanup.append(temp_images_path)
background_tasks.add_task(cleanup_files, *files_to_cleanup)
return FileResponse(
temp_output_path,
media_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
filename=output_filename,
background=None
)
except Exception as e:
files_to_cleanup = [temp_input_path, temp_output_path]
if temp_images_path:
files_to_cleanup.append(temp_images_path)
cleanup_files(*files_to_cleanup)
raise HTTPException(status_code=500, detail=f"Conversion failed: {str(e)}")
def cleanup_files(*file_paths):
"""Clean up temporary files"""
for file_path in file_paths:
try:
if os.path.exists(file_path):
os.unlink(file_path)
except Exception as e:
print(f"Error cleaning up {file_path}: {e}")
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return {"status": "healthy", "message": "QCM Converter API - META is running"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860) |