Makhinur commited on
Commit
4c802ed
·
verified ·
1 Parent(s): b18ac6b

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +14 -6
main.py CHANGED
@@ -4,6 +4,7 @@ from gradio_client import Client, handle_file
4
  import shutil
5
  import base64
6
  import os
 
7
 
8
  app = FastAPI()
9
 
@@ -12,7 +13,6 @@ HF_TOKEN = os.getenv("HF_TOKEN")
12
  # Initialize the Gradio client with the token
13
  client = Client("Makhinur/Image_Face_Upscale_Restoration-GFPGAN", hf_token=HF_TOKEN)
14
 
15
-
16
  # Version mapping from HTML to Gradio API
17
  version_map = {
18
  "M1": "v1.2",
@@ -43,17 +43,25 @@ async def enhance_image(
43
  api_name="/predict"
44
  )
45
 
46
- # Read the result image and encode it in base64
47
- with open(result[0], "rb") as img_file:
 
 
 
 
 
 
 
 
48
  b64_string = base64.b64encode(img_file.read()).decode('utf-8')
49
 
50
- # Clean up the temporary file
51
  os.remove(temp_file_path)
 
52
 
53
  return JSONResponse(content={"sketch_image_base64": f"data:image/png;base64,{b64_string}"})
54
 
55
  except Exception as e:
56
  # Log the error message for debugging
57
  print(f"Error processing image: {e}")
58
- return JSONResponse(status_code=500, content={"message": "Internal Server Error"})
59
-
 
4
  import shutil
5
  import base64
6
  import os
7
+ from PIL import Image # Import the Pillow library
8
 
9
  app = FastAPI()
10
 
 
13
  # Initialize the Gradio client with the token
14
  client = Client("Makhinur/Image_Face_Upscale_Restoration-GFPGAN", hf_token=HF_TOKEN)
15
 
 
16
  # Version mapping from HTML to Gradio API
17
  version_map = {
18
  "M1": "v1.2",
 
43
  api_name="/predict"
44
  )
45
 
46
+ # Assuming the Gradio app outputs a WebP file
47
+ result_image_path = result[0] # This path should be a WebP file
48
+
49
+ # Convert the WebP image to PNG using Pillow
50
+ with Image.open(result_image_path) as img:
51
+ png_image_path = "output_image.png"
52
+ img.save(png_image_path, format="PNG")
53
+
54
+ # Read the PNG image and encode it in base64
55
+ with open(png_image_path, "rb") as img_file:
56
  b64_string = base64.b64encode(img_file.read()).decode('utf-8')
57
 
58
+ # Clean up the temporary files
59
  os.remove(temp_file_path)
60
+ os.remove(png_image_path)
61
 
62
  return JSONResponse(content={"sketch_image_base64": f"data:image/png;base64,{b64_string}"})
63
 
64
  except Exception as e:
65
  # Log the error message for debugging
66
  print(f"Error processing image: {e}")
67
+ return JSONResponse(status_code=500, content={"message": "Internal Server Error"})