Update main.py
Browse files
main.py
CHANGED
@@ -1,49 +1,52 @@
|
|
1 |
-
from fastapi import FastAPI, File, UploadFile,
|
2 |
from fastapi.responses import JSONResponse
|
|
|
3 |
from gradio_client import Client, handle_file
|
4 |
import os
|
|
|
5 |
import base64
|
6 |
|
7 |
-
# Initialize FastAPI
|
8 |
app = FastAPI()
|
9 |
|
10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
12 |
|
13 |
# Initialize the Gradio client with the token
|
14 |
-
client = Client("Makhinur/
|
15 |
|
16 |
@app.post("/upload/")
|
17 |
-
async def
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
f.write(await file.read())
|
23 |
-
|
24 |
-
# Use the Gradio client to send the image to the Gradio app
|
25 |
-
result = client.predict(
|
26 |
-
img=handle_file(file_location),
|
27 |
-
api_name="/predict"
|
28 |
-
)
|
29 |
-
|
30 |
-
# Clean up the temporary file
|
31 |
-
os.remove(file_location)
|
32 |
-
|
33 |
-
# Read the sketch image from the result
|
34 |
-
sketch_image_path = result[0] # Path to the sketch image
|
35 |
-
|
36 |
-
# Convert the sketch image to base64
|
37 |
-
with open(sketch_image_path, "rb") as img_file:
|
38 |
-
sketch_image_base64 = base64.b64encode(img_file.read()).decode('utf-8')
|
39 |
-
|
40 |
-
# Prepare the response
|
41 |
-
response = {
|
42 |
-
"sketch_image_base64": f"data:image/jpeg;base64,{sketch_image_base64}"
|
43 |
-
}
|
44 |
-
|
45 |
-
return JSONResponse(content=response)
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
except Exception as e:
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, Form
|
2 |
from fastapi.responses import JSONResponse
|
3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
4 |
from gradio_client import Client, handle_file
|
5 |
import os
|
6 |
+
import tempfile
|
7 |
import base64
|
8 |
|
|
|
9 |
app = FastAPI()
|
10 |
|
11 |
+
# Setup CORS
|
12 |
+
app.add_middleware(
|
13 |
+
CORSMiddleware,
|
14 |
+
allow_origins=["*"], # Adjust as needed
|
15 |
+
allow_credentials=True,
|
16 |
+
allow_methods=["*"],
|
17 |
+
allow_headers=["*"],
|
18 |
+
)
|
19 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
20 |
|
21 |
# Initialize the Gradio client with the token
|
22 |
+
client = Client("Makhinur/Image_Face_Upscale_Restoration-GFPGAN", hf_token=HF_TOKEN)
|
23 |
|
24 |
@app.post("/upload/")
|
25 |
+
async def upload_file(file: UploadFile = File(...), version: str = Form(...), scale: int = Form(...)):
|
26 |
+
# Save the uploaded file temporarily
|
27 |
+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
28 |
+
temp_file.write(await file.read())
|
29 |
+
temp_file_path = temp_file.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
try:
|
32 |
+
# Use handle_file to prepare the file for the Gradio client
|
33 |
+
result = client.predict(handle_file(temp_file_path), version, scale, api_name="/predict")
|
34 |
+
|
35 |
+
# Check if the result is valid
|
36 |
+
if result and len(result) == 2:
|
37 |
+
# Convert the image data to a base64 string
|
38 |
+
with open(result[0], "rb") as image_file:
|
39 |
+
image_data = base64.b64encode(image_file.read()).decode("utf-8")
|
40 |
+
|
41 |
+
return JSONResponse({
|
42 |
+
"sketch_image_base64": f"data:image/png;base64,{image_data}",
|
43 |
+
"result_file": result[1]
|
44 |
+
})
|
45 |
+
else:
|
46 |
+
return JSONResponse({"error": "Invalid result from the prediction API."}, status_code=500)
|
47 |
except Exception as e:
|
48 |
+
return JSONResponse({"error": str(e)}, status_code=500)
|
49 |
+
finally:
|
50 |
+
# Clean up the temporary file
|
51 |
+
if os.path.exists(temp_file_path):
|
52 |
+
os.unlink(temp_file_path)
|