ypedz commited on
Commit
d33fd6a
·
verified ·
1 Parent(s): 3fac350

Upload 2 files

Browse files
Files changed (2) hide show
  1. requirements.txt +4 -0
  2. server.py +39 -0
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ gunicorn
4
+ python-multipart
server.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from fastapi.responses import FileResponse
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ import os
5
+ import subprocess
6
+
7
+ app = FastAPI()
8
+
9
+ # Wajib: Mengizinkan YPEDZ TOOLS (Frontend) mengakses API ini (CORS)
10
+ app.add_middleware(
11
+ CORSMiddleware,
12
+ allow_origins=["*"], # Mengizinkan semua akses. Nanti bisa diganti dengan URL web Anda.
13
+ allow_credentials=True,
14
+ allow_methods=["*"],
15
+ allow_headers=["*"],
16
+ )
17
+
18
+ @app.post("/api/enhance")
19
+ async def enhance_image(file: UploadFile = File(...)):
20
+ # Buat folder penyimpanan sementara
21
+ os.makedirs("temp_input", exist_ok=True)
22
+ os.makedirs("temp_output", exist_ok=True)
23
+
24
+ input_path = f"temp_input/{file.filename}"
25
+ output_path = f"temp_output/enhanced_{file.filename}"
26
+
27
+ # Simpan foto mentah yang dikirim dari web
28
+ with open(input_path, "wb") as buffer:
29
+ buffer.write(await file.read())
30
+
31
+ # Jalankan Real-ESRGAN (Pastikan realesrgan-ncnn-vulkan.exe ada di folder yang sama)
32
+ # -n adalah nama model, -i input, -o output
33
+ command = f"realesrgan-ncnn-vulkan.exe -i {input_path} -o {output_path} -n realesrgan-x4plus"
34
+ subprocess.run(command, shell=True)
35
+
36
+ # Kirim foto yang sudah jernih kembali ke website
37
+ return FileResponse(output_path)
38
+
39
+ # Cara menjalankan server: uvicorn server:app --reload