endrol commited on
Commit
bc17efc
·
1 Parent(s): 2608575

update api

Browse files
Files changed (1) hide show
  1. app.py +22 -6
app.py CHANGED
@@ -6,14 +6,21 @@ import time
6
  import os
7
  from PIL import Image
8
 
9
- # Get SR API endpoint from environment
10
- ENHANCE_API = os.getenv("img_enhance_api")
 
11
 
12
- def apply_super_resolution(input_image, scale_factor):
13
  """Apply super-resolution to input image"""
14
  if input_image is None:
15
  return None, "❌ Please upload an image first", gr.update(visible=False)
16
 
 
 
 
 
 
 
17
  try:
18
  # Convert PIL image to bytes
19
  img_buffer = io.BytesIO()
@@ -22,7 +29,7 @@ def apply_super_resolution(input_image, scale_factor):
22
 
23
  # Call super-resolution API
24
  response = requests.post(
25
- f"{ENHANCE_API}/invocations",
26
  headers={"Content-Type": "application/octet-stream"},
27
  data=img_bytes,
28
  params={
@@ -44,8 +51,9 @@ def apply_super_resolution(input_image, scale_factor):
44
  # Create download file
45
  timestamp = int(time.time())
46
  download_path = f"enhanced_image_{timestamp}.png"
 
47
 
48
- status = f"✅ Enhancement successful!\n"
49
  status += f"Model: {result.get('model', 'RealESRGAN_x4plus')}\n"
50
  status += f"Scale: {result.get('outscale', 4.0)}x\n"
51
  status += f"Input: {result.get('input_img_width', 0)}x{result.get('input_img_height', 0)}\n"
@@ -75,6 +83,14 @@ def main():
75
 
76
  with gr.Column(scale=1):
77
  gr.Markdown("### Enhancement Settings")
 
 
 
 
 
 
 
 
78
  scale_dropdown = gr.Dropdown(
79
  choices=[1, 2, 4],
80
  value=4,
@@ -117,7 +133,7 @@ def main():
117
  # Event handlers
118
  enhance_button.click(
119
  fn=apply_super_resolution,
120
- inputs=[input_image, scale_dropdown],
121
  outputs=[image_slider, status_text, download_button],
122
  show_progress=True
123
  )
 
6
  import os
7
  from PIL import Image
8
 
9
+ # Get API endpoints from environment
10
+ SR_API = os.getenv("sr_api")
11
+ ENHANCE_API = os.getenv("enhance_api")
12
 
13
+ def apply_super_resolution(input_image, scale_factor, api_choice):
14
  """Apply super-resolution to input image"""
15
  if input_image is None:
16
  return None, "❌ Please upload an image first", gr.update(visible=False)
17
 
18
+ # Select API endpoint based on user choice
19
+ api_endpoint = SR_API if api_choice == "SR API" else ENHANCE_API
20
+
21
+ if not api_endpoint:
22
+ return None, f"❌ {api_choice} endpoint not configured", gr.update(visible=False)
23
+
24
  try:
25
  # Convert PIL image to bytes
26
  img_buffer = io.BytesIO()
 
29
 
30
  # Call super-resolution API
31
  response = requests.post(
32
+ f"{api_endpoint}/invocations",
33
  headers={"Content-Type": "application/octet-stream"},
34
  data=img_bytes,
35
  params={
 
51
  # Create download file
52
  timestamp = int(time.time())
53
  download_path = f"enhanced_image_{timestamp}.png"
54
+ enhanced_image.save(download_path, format='PNG')
55
 
56
+ status = f"✅ Enhancement successful using {api_choice}!\n"
57
  status += f"Model: {result.get('model', 'RealESRGAN_x4plus')}\n"
58
  status += f"Scale: {result.get('outscale', 4.0)}x\n"
59
  status += f"Input: {result.get('input_img_width', 0)}x{result.get('input_img_height', 0)}\n"
 
83
 
84
  with gr.Column(scale=1):
85
  gr.Markdown("### Enhancement Settings")
86
+
87
+ api_dropdown = gr.Dropdown(
88
+ choices=["SR API", "ENHANCE API"],
89
+ value="SR API",
90
+ label="API Choice",
91
+ info="Choose which enhancement API to use"
92
+ )
93
+
94
  scale_dropdown = gr.Dropdown(
95
  choices=[1, 2, 4],
96
  value=4,
 
133
  # Event handlers
134
  enhance_button.click(
135
  fn=apply_super_resolution,
136
+ inputs=[input_image, scale_dropdown, api_dropdown],
137
  outputs=[image_slider, status_text, download_button],
138
  show_progress=True
139
  )