Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, File, UploadFile | |
| from fastapi.responses import JSONResponse | |
| from fastapi.middleware.cors import CORSMiddleware | |
| import uvicorn # Tetap diperlukan untuk uvicorn.run | |
| # === Inisialisasi FastAPI === | |
| app = FastAPI() | |
| # === Middleware CORS agar bisa diakses dari frontend (JS) === | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # === Endpoint GET untuk testing sederhana (opsional tapi disarankan) === | |
| async def read_root(): | |
| return {"message": "Hello from Simple FastAPI! Send POST to /predict"} | |
| # === Endpoint POST DUMMY (ini yang utama untuk diuji) === | |
| async def predict_image(file: UploadFile = File(...)): | |
| # Tidak ada pemrosesan file atau model. Hanya respons JSON. | |
| try: | |
| # Coba baca file untuk memastikan penerimaan file tidak error, | |
| # tapi tidak akan diproses lebih lanjut. | |
| await file.read() | |
| response = { | |
| "filename": file.filename, | |
| "title": "Prediction Result (DUMMY)", | |
| "message": "API berfungsi! Ini respons tanpa model.", | |
| "confidence": "99.99", | |
| "details": ["Class: dummy", "Confidence: 99.99%"] | |
| } | |
| return JSONResponse(content=response) | |
| except Exception as e: | |
| return JSONResponse(content={"error": f"Error processing dummy file: {e}"}, status_code=500) | |
| # === Untuk menjalankan secara lokal (biarkan seperti ini) === | |
| if __name__ == "__main__": | |
| uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=False) |