Spaces:
Sleeping
Sleeping
Darwin Agent
fix: resolve CONFIG_ERROR - remove invalid sdk_version and static dir references
3e4e144 | import os | |
| import json | |
| from fastapi import FastAPI, UploadFile, File, HTTPException, Form | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.responses import FileResponse, JSONResponse | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pathlib import Path | |
| import shutil | |
| import uuid | |
| app = FastAPI(title="HanSoo Office Editor", version="1.0.0") | |
| # CORS μ€μ | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # μ λ‘λ λλ ν 리 | |
| UPLOAD_DIR = Path("/tmp/hansoo_uploads") | |
| UPLOAD_DIR.mkdir(parents=True, exist_ok=True) | |
| # μ μ νμΌ μ 곡 (static λλ ν°λ¦¬κ° μμΌλ―λ‘ μ£Όμ μ²λ¦¬) | |
| # app.mount("/static", StaticFiles(directory="static"), name="static") | |
| def read_root(): | |
| """μΈλ±μ€ νμ΄μ§ μ 곡""" | |
| return FileResponse("index.html") | |
| def health_check(): | |
| """ν¬μ€ μ²΄ν¬ μλν¬μΈνΈ""" | |
| return {"status": "healthy", "service": "HanSoo Office Editor"} | |
| async def upload_file(file: UploadFile = File(...)): | |
| """νμΌ μ λ‘λ μλν¬μΈνΈ""" | |
| try: | |
| file_id = str(uuid.uuid4()) | |
| file_path = UPLOAD_DIR / f"{file_id}_{file.filename}" | |
| with open(file_path, "wb") as buffer: | |
| shutil.copyfileobj(file.file, buffer) | |
| return { | |
| "success": True, | |
| "file_id": file_id, | |
| "filename": file.filename, | |
| "size": file_path.stat().st_size, | |
| "content_type": file.content_type | |
| } | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def get_file(file_id: str): | |
| """μ λ‘λλ νμΌ μ 보 μ‘°ν""" | |
| files = list(UPLOAD_DIR.glob(f"{file_id}_*")) | |
| if not files: | |
| raise HTTPException(status_code=404, detail="File not found") | |
| file_path = files[0] | |
| return { | |
| "file_id": file_id, | |
| "filename": file_path.name.replace(f"{file_id}_", ""), | |
| "size": file_path.stat().st_size | |
| } | |
| async def process_file( | |
| file_id: str = Form(...), | |
| operation: str = Form("extract") | |
| ): | |
| """νμΌ μ²λ¦¬ μλν¬μΈνΈ (μΆμΆ, λ³ν λ±)""" | |
| files = list(UPLOAD_DIR.glob(f"{file_id}_*")) | |
| if not files: | |
| raise HTTPException(status_code=404, detail="File not found") | |
| file_path = files[0] | |
| try: | |
| if operation == "extract": | |
| # ν μ€νΈ μΆμΆ λ‘μ§ (νμ₯μμ λ°λΌ μ²λ¦¬) | |
| content = extract_text(file_path) | |
| return {"success": True, "content": content} | |
| elif operation == "convert": | |
| # λ³ν λ‘μ§ | |
| return {"success": True, "message": "Conversion completed"} | |
| else: | |
| raise HTTPException(status_code=400, detail="Unknown operation") | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| def extract_text(file_path: Path) -> str: | |
| """νμΌμμ ν μ€νΈ μΆμΆ""" | |
| suffix = file_path.suffix.lower() | |
| if suffix in [".txt", ".md", ".json", ".csv"]: | |
| return file_path.read_text(encoding="utf-8") | |
| elif suffix == ".pdf": | |
| # PDF ν μ€νΈ μΆμΆ (PyPDF2 λλ pdfplumber νμ) | |
| return "PDF text extraction requires additional dependencies" | |
| elif suffix in [".docx", ".xlsx", ".pptx"]: | |
| return "Office file extraction requires python-docx/openpyxl/python-pptx" | |
| elif suffix in [".hwp", ".hwpx"]: | |
| return "HWP extraction requires rhwp or pyhwp" | |
| else: | |
| return "Unsupported file format" | |
| def cleanup(): | |
| """μλ² μ’ λ£ μ μ λ‘λ λλ ν 리 μ 리""" | |
| try: | |
| shutil.rmtree(UPLOAD_DIR) | |
| except: | |
| pass | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 7860))) | |