Spaces:
Sleeping
Sleeping
File size: 1,617 Bytes
de94cf7 23514ec 560bcfb 23514ec de94cf7 23514ec de94cf7 23514ec 5886919 23514ec 7ff4758 560bcfb de94cf7 560bcfb 23514ec 560bcfb 5b5878b 560bcfb de94cf7 5886919 560bcfb 5886919 560bcfb 23514ec de94cf7 7ff4758 560bcfb de94cf7 560bcfb 8d023ef 5886919 |
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 |
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) ===
@app.get("/")
async def read_root():
return {"message": "Hello from Simple FastAPI! Send POST to /predict"}
# === Endpoint POST DUMMY (ini yang utama untuk diuji) ===
@app.post("/predict")
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) |