Makhinur commited on
Commit
b2bc1d4
·
verified ·
1 Parent(s): 4f2123c

Update main.py

Browse files

from fastapi import FastAPI, File, UploadFile, Form
from fastapi.responses import JSONResponse
from gradio_client import Client, handle_file
import shutil
import base64
import os

app = FastAPI()

HF_TOKEN = os.getenv("HF_TOKEN")

# Initialize the Gradio client with the token
client = Client("Makhinur/Image_Face_Upscale_Restoration-GFPGAN", hf_token=HF_TOKEN)


# Version mapping from HTML to Gradio API
version_map = {
"M1": "v1.2",
"M2": "v1.3",
"M3": "v1.4"
}



@app
.post("/upload/")
async def enhance_image(
file: UploadFile = File(...),
version: str = Form(...),
scale: int = Form(...)
):
# Map version from HTML to Gradio expected value
gradio_version = version_map.get(version, "v1.4")

# Save the uploaded image to a temporary file
temp_file_path = "temp_image.png"
with open(temp_file_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)

try:
# Use the Gradio client to process the image
result = client.predict(
img=handle_file(temp_file_path),
version=gradio_version,
scale=scale,
api_name="/predict"
)

# Read the result image and encode it in base64
with open(result[0], "rb") as img_file:
b64_string = base64.b64encode(img_file.read()).decode('utf-8')

# Clean up the temporary file
os.remove(temp_file_path)

return JSONResponse(content={"sketch_image_base64": f"data:image/png;base64,{b64_string}"})

except Exception as e:
# Log the error message for debugging
print(f"Error processing image: {e}")
return JSONResponse(status_code=500, content={"message": "Internal Server Error"})

Files changed (1) hide show
  1. main.py +18 -15
main.py CHANGED
@@ -1,8 +1,8 @@
1
- from fastapi import FastAPI, UploadFile, File, HTTPException
2
  from fastapi.responses import JSONResponse
3
  from gradio_client import Client, handle_file
 
4
  import base64
5
-
6
  import os
7
 
8
  app = FastAPI()
@@ -15,23 +15,26 @@ client = Client("Makhinur/Bringingoldphotoliveagain", hf_token=HF_TOKEN)
15
  @app.post("/upload/")
16
  async def upload_image(file: UploadFile = File(...)):
17
  try:
18
- # Save the uploaded file temporarily
19
- with open(file.filename, "wb") as buffer:
20
- buffer.write(await file.read())
21
-
22
  # Use the Gradio client to process the image
23
- result = gradio_client.predict(
24
- img=handle_file(file.filename),
25
  api_name="/predict"
26
  )
27
 
28
- # Read the output image and encode it in base64
29
- with open(result[0], "rb") as result_file:
30
- encoded_string = base64.b64encode(result_file.read()).decode('utf-8')
31
 
32
- # Create a JSON response with the base64 encoded image
33
- return JSONResponse(content={"sketch_image_base64": f"data:image/jpeg;base64,{encoded_string}"})
34
 
35
- except Exception as e:
36
- raise HTTPException(status_code=500, detail=str(e))
37
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile, Form
2
  from fastapi.responses import JSONResponse
3
  from gradio_client import Client, handle_file
4
+ import shutil
5
  import base64
 
6
  import os
7
 
8
  app = FastAPI()
 
15
  @app.post("/upload/")
16
  async def upload_image(file: UploadFile = File(...)):
17
  try:
18
+ temporary file
19
+ temp_file_path = "temp_image.png"
20
+ with open(temp_file_path, "wb") as buffer:
21
+ shutil.copyfileobj(file.file, buffer)
22
  # Use the Gradio client to process the image
23
+ result = client.predict(
24
+ img=handle_file(temp_file_path),
25
  api_name="/predict"
26
  )
27
 
28
+ # Read the result image and encode it in base64
29
+ with open(result[0], "rb") as img_file:
30
+ b64_string = base64.b64encode(img_file.read()).decode('utf-8')
31
 
32
+ # Clean up the temporary file
33
+ os.remove(temp_file_path)
34
 
35
+ return JSONResponse(content={"sketch_image_base64": f"data:image/png;base64,{b64_string}"})
 
36
 
37
+ except Exception as e:
38
+ # Log the error message for debugging
39
+ print(f"Error processing image: {e}")
40
+ return JSONResponse(status_code=500, content={"message": "Internal Server Error"})