Rishi Desai commited on
Commit
e8c9f0d
·
1 Parent(s): 9af3c99

some error handling

Browse files
Files changed (1) hide show
  1. gradio_demo.py +55 -46
gradio_demo.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import os
3
  import tempfile
4
  from main import process_face
 
5
 
6
  def enhance_face_gradio(input_image, ref_image):
7
  """
@@ -12,7 +13,7 @@ def enhance_face_gradio(input_image, ref_image):
12
  ref_image: Reference face image from Gradio
13
 
14
  Returns:
15
- str: Path to the enhanced image
16
  """
17
  # Create temporary files for input, reference, and output
18
  with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as input_file, \
@@ -27,57 +28,65 @@ def enhance_face_gradio(input_image, ref_image):
27
  input_image.save(input_path)
28
  ref_image.save(ref_path)
29
 
30
- # Process the face
31
- process_face(
32
- input_path=input_path,
33
- ref_path=ref_path,
34
- crop=False,
35
- upscale=False,
36
- output_path=output_path
37
- )
38
-
39
- # Clean up temporary input and reference files
40
- os.unlink(input_path)
41
- os.unlink(ref_path)
42
-
43
- return output_path
44
 
45
- # Create the Gradio interface
46
- with gr.Blocks(title="Face Enhancement Demo") as demo:
47
- gr.Markdown("# Face Enhancement Demo")
48
- gr.Markdown("Upload an input image and a reference face image to enhance the input.")
49
 
50
- with gr.Row():
51
- with gr.Column():
52
- input_image = gr.Image(label="Input Image", type="pil")
53
- ref_image = gr.Image(label="Reference Face", type="pil")
54
- enhance_button = gr.Button("Enhance Face")
 
 
55
 
56
- with gr.Column():
57
- output_image = gr.Image(label="Enhanced Result")
58
-
59
- enhance_button.click(
60
- fn=enhance_face_gradio,
61
- inputs=[input_image, ref_image],
62
- outputs=output_image,
63
- queue=True # Enable queue for sequential processing
64
- )
65
-
66
- gr.Markdown("""
67
- ## Instructions
68
- 1. Upload an image you want to enhance
69
- 2. Upload a reference face image
70
- 3. Click 'Enhance Face' to start the process
71
- 4. Processing takes about 60 seconds
72
- """)
 
 
 
 
 
 
73
 
74
- # Launch the Gradio app with queue
75
- if __name__ == "__main__":
76
- # Set up queue with max_size=20 and concurrency=1
77
  demo.queue(max_size=20) # Configure queue size
78
  demo.launch(
79
  share=False, # Set to True if you want a public link
80
  server_name="0.0.0.0", # Make available on all network interfaces
81
  server_port=7860, # Default Gradio port
82
- # concurrency_count=1 # Process one job at a time
83
- )
 
 
 
 
2
  import os
3
  import tempfile
4
  from main import process_face
5
+ from PIL import Image
6
 
7
  def enhance_face_gradio(input_image, ref_image):
8
  """
 
13
  ref_image: Reference face image from Gradio
14
 
15
  Returns:
16
+ PIL Image: Enhanced image
17
  """
18
  # Create temporary files for input, reference, and output
19
  with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as input_file, \
 
28
  input_image.save(input_path)
29
  ref_image.save(ref_path)
30
 
31
+ try:
32
+ # Process the face
33
+ process_face(
34
+ input_path=input_path,
35
+ ref_path=ref_path,
36
+ crop=False,
37
+ upscale=False,
38
+ output_path=output_path
39
+ )
40
+ except Exception as e:
41
+ # Handle the error, log it, and return an error message
42
+ print(f"Error processing face: {e}")
43
+ return "An error occurred while processing the face. Please try again."
 
44
 
45
+ finally:
46
+ # Clean up temporary input and reference files
47
+ os.unlink(input_path)
48
+ os.unlink(ref_path)
49
 
50
+ return Image.open(output_path)
51
+
52
+ def create_gradio_interface():
53
+ # Create the Gradio interface
54
+ with gr.Blocks(title="Face Enhancement Demo") as demo:
55
+ gr.Markdown("# Face Enhancement Demo")
56
+ gr.Markdown("Upload an input image and a reference face image to enhance the input.")
57
 
58
+ with gr.Row():
59
+ with gr.Column():
60
+ input_image = gr.Image(label="Input Image", type="pil")
61
+ ref_image = gr.Image(label="Reference Face", type="pil")
62
+ enhance_button = gr.Button("Enhance Face")
63
+
64
+ with gr.Column():
65
+ output_image = gr.Image(label="Enhanced Result")
66
+
67
+ enhance_button.click(
68
+ fn=enhance_face_gradio,
69
+ inputs=[input_image, ref_image],
70
+ outputs=output_image,
71
+ queue=True # Enable queue for sequential processing
72
+ )
73
+
74
+ gr.Markdown("""
75
+ ## Instructions
76
+ 1. Upload an image you want to enhance
77
+ 2. Upload a reference face image
78
+ 3. Click 'Enhance Face' to start the process
79
+ 4. Processing takes about 60 seconds
80
+ """)
81
 
82
+ # Launch the Gradio app with queue
 
 
83
  demo.queue(max_size=20) # Configure queue size
84
  demo.launch(
85
  share=False, # Set to True if you want a public link
86
  server_name="0.0.0.0", # Make available on all network interfaces
87
  server_port=7860, # Default Gradio port
88
+ )
89
+
90
+
91
+ if __name__ == "__main__":
92
+ create_gradio_interface()