zerovic commited on
Commit
99e790f
·
verified ·
1 Parent(s): f90f2b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -24
app.py CHANGED
@@ -43,42 +43,56 @@ import cv2
43
  import gradio as gr
44
  import numpy as np
45
  import onnxruntime as ort
 
46
 
47
- # Local file configuration path generated by our conversion script
48
- MODEL_PATH = "RealESRGAN_x4plus_anime_6B.onnx"
 
 
 
 
 
 
 
49
  ort_session = None
50
 
51
- def get_inference_session():
52
  """
53
- Loads the locally baked dynamic shape ONNX model instance into memory.
 
54
  """
55
- global ort_session
56
- if ort_session is not None:
57
  return ort_session
58
-
59
- if not os.path.exists(MODEL_PATH):
60
- # Auto-compile file right now if the build step wasn't run separately
61
- print(f"{MODEL_PATH} not found. Running inline build conversion...")
62
- import subprocess
63
- # Assumes export_onnx.py is placed in the same working directory
64
- subprocess.run([sys.executable, "export_onnx.py"], check=True)
 
 
 
65
 
66
  session_options = ort.SessionOptions()
67
  session_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
68
 
69
- ort_session = ort.InferenceSession(MODEL_PATH, session_options, providers=['CPUExecutionProvider'])
 
 
70
  return ort_session
71
 
72
- def upscale_image(input_img):
73
  """
74
- Dynamic inference pipeline. Sends the full image array to the custom ONNX graph
75
- without splitting into tiles, maximizing multi-threaded CPU efficiency.
76
  """
77
  if input_img is None:
78
  return None
79
 
80
  try:
81
- session = get_inference_session()
82
 
83
  # Preprocessing: Normalize image array data to float32 range [0.0, 1.0]
84
  img_float = input_img.astype(np.float32) / 255.0
@@ -89,15 +103,15 @@ def upscale_image(input_img):
89
  # Add batch dimension: (1, C, H, W)
90
  img_batch = np.expand_dims(img_chw, axis=0)
91
 
92
- # Bind input/output keys dynamically matching the graph properties
93
  input_name = session.get_inputs()[0].name
94
  output_name = session.get_outputs()[0].name
95
 
96
- # Run raw CPU forward tensor calculations on the entire canvas in one step
97
  ort_outs = session.run([output_name], {input_name: img_batch})
98
  output_tensor = ort_outs[0]
99
 
100
- # Postprocessing: Drop the batch dimension and clip bounds safely back to valid channels
101
  output_tensor = np.squeeze(output_tensor, axis=0)
102
  output_tensor = np.clip(output_tensor, 0.0, 1.0)
103
 
@@ -118,19 +132,24 @@ def upscale_image(input_img):
118
  # Define the user interface layout
119
  with gr.Blocks(title="AI Lightweight Image Upscaler (ONNX)") as demo:
120
  gr.Markdown("# 🖼️ AI Image Resizer & Upscaler (ONNX Engine)")
121
- gr.Markdown("Running locally on Hugging Face Free CPU hardware using custom compiled dynamic weights.")
122
 
123
  with gr.Row():
124
  with gr.Column():
125
  input_image = gr.Image(label="Source Image", type="numpy")
126
- submit_btn = gr.Button("Upscale Image (4x Super Fast)", variant="primary")
 
 
 
 
 
127
 
128
  with gr.Column():
129
  output_image = gr.Image(label="Enhanced Super-Resolution Result", type="numpy")
130
 
131
  submit_btn.click(
132
  fn=upscale_image,
133
- inputs=input_image,
134
  outputs=output_image
135
  )
136
 
 
43
  import gradio as gr
44
  import numpy as np
45
  import onnxruntime as ort
46
+ from huggingface_hub import hf_hub_download
47
 
48
+ # Verified public model tracks configured with fully dynamic shape axes natively
49
+ MODELS = {
50
+ "RealESRGAN_x4plus (Dynamic - Super Fast)": {
51
+ "repo_id": "fastnlp/RealESRGAN_x4plus_onnx",
52
+ "filename": "model.onnx"
53
+ }
54
+ }
55
+
56
+ current_model_name = None
57
  ort_session = None
58
 
59
+ def load_model(model_choice):
60
  """
61
+ Downloads the official pre-compiled dynamic-shape ONNX engine directly.
62
+ Bypasses structural weight remapping issues entirely.
63
  """
64
+ global current_model_name, ort_session
65
+ if current_model_name == model_choice and ort_session is not None:
66
  return ort_session
67
+
68
+ cfg = MODELS[model_choice]
69
+ print(f"Loading weights for {model_choice}...")
70
+ token = os.environ.get("HF_TOKEN")
71
+
72
+ model_path = hf_hub_download(
73
+ repo_id=cfg["repo_id"],
74
+ filename=cfg["filename"],
75
+ token=token
76
+ )
77
 
78
  session_options = ort.SessionOptions()
79
  session_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
80
 
81
+ # Initialize session on CPU with dynamic array dimensions enabled
82
+ ort_session = ort.InferenceSession(model_path, session_options, providers=['CPUExecutionProvider'])
83
+ current_model_name = model_choice
84
  return ort_session
85
 
86
+ def upscale_image(input_img, model_choice):
87
  """
88
+ Dynamic inference pipeline. Transposes and runs full image dimensions
89
+ in a single, fast forward operation on your CPU layer.
90
  """
91
  if input_img is None:
92
  return None
93
 
94
  try:
95
+ session = load_model(model_choice)
96
 
97
  # Preprocessing: Normalize image array data to float32 range [0.0, 1.0]
98
  img_float = input_img.astype(np.float32) / 255.0
 
103
  # Add batch dimension: (1, C, H, W)
104
  img_batch = np.expand_dims(img_chw, axis=0)
105
 
106
+ # Bind input/output identifiers matching the schema map
107
  input_name = session.get_inputs()[0].name
108
  output_name = session.get_outputs()[0].name
109
 
110
+ # Run raw CPU forward calculation on the entire canvas in one go
111
  ort_outs = session.run([output_name], {input_name: img_batch})
112
  output_tensor = ort_outs[0]
113
 
114
+ # Postprocessing: Drop batch dimension and clip tensor bounds safely
115
  output_tensor = np.squeeze(output_tensor, axis=0)
116
  output_tensor = np.clip(output_tensor, 0.0, 1.0)
117
 
 
132
  # Define the user interface layout
133
  with gr.Blocks(title="AI Lightweight Image Upscaler (ONNX)") as demo:
134
  gr.Markdown("# 🖼️ AI Image Resizer & Upscaler (ONNX Engine)")
135
+ gr.Markdown("Running locally on Hugging Face Free CPU hardware using dynamic tensor shape processing.")
136
 
137
  with gr.Row():
138
  with gr.Column():
139
  input_image = gr.Image(label="Source Image", type="numpy")
140
+ model_dropdown = gr.Dropdown(
141
+ choices=list(MODELS.keys()),
142
+ value="RealESRGAN_x4plus (Dynamic - Super Fast)",
143
+ label="Select AI Upscaling Engine"
144
+ )
145
+ submit_btn = gr.Button("Upscale Image (Dynamic 4x)", variant="primary")
146
 
147
  with gr.Column():
148
  output_image = gr.Image(label="Enhanced Super-Resolution Result", type="numpy")
149
 
150
  submit_btn.click(
151
  fn=upscale_image,
152
+ inputs=[input_image, model_dropdown],
153
  outputs=output_image
154
  )
155