Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +37 -21
- requirements.txt.bak +1 -0
app.py
CHANGED
|
@@ -1,30 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import onnxruntime as ort
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Load the ONNX model
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
# Run inference
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
#
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Define the Gradio interface
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Launch the Gradio app
|
| 30 |
-
launch(
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import onnxruntime as ort
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
|
| 6 |
# Load the ONNX model
|
| 7 |
+
model_path = "modelSR.onnx" # Replace with your model path
|
| 8 |
+
ort_session = ort.InferenceSession(model_path)
|
| 9 |
+
|
| 10 |
+
# Define the superresolution function
|
| 11 |
+
def superresolve(image):
|
| 12 |
+
# Preprocess the image
|
| 13 |
+
image = np.array(image.resize((256, 256))) # Resize to expected input size
|
| 14 |
+
image = image[:, :, ::-1] # Convert to BGR (some models expect this)
|
| 15 |
+
image = np.transpose(image, (2, 0, 1)) # Transpose to NHWC format
|
| 16 |
+
image = image[np.newaxis, ...] # Add batch dimension
|
| 17 |
+
|
| 18 |
# Run inference
|
| 19 |
+
ort_inputs = {ort_session.get_inputs()[0].name: image.astype(np.float32)}
|
| 20 |
+
ort_outs = ort_session.run(None, ort_inputs)
|
| 21 |
+
output = ort_outs[0][0]
|
| 22 |
+
|
| 23 |
+
# Postprocess the output
|
| 24 |
+
output = output.transpose(1, 2, 0) # Transpose back to HWC
|
| 25 |
+
output = np.clip(output, 0, 1) # Clip values to [0, 1]
|
| 26 |
+
output = (output * 255).astype(np.uint8) # Convert to uint8 for PIL
|
| 27 |
+
output = Image.fromarray(output[:, :, ::-1]) # Convert back to RGB
|
| 28 |
+
|
| 29 |
+
return output
|
| 30 |
|
| 31 |
# Define the Gradio interface
|
| 32 |
+
interface = gr.Interface(
|
| 33 |
+
fn=superresolve,
|
| 34 |
+
inputs="image",
|
| 35 |
+
outputs="image",
|
| 36 |
+
title="Super Resolution",
|
| 37 |
+
description="Upload an image to upscale its resolution.",
|
| 38 |
+
allow_flagging=False,
|
| 39 |
+
thumbnail="https://i.imgur.com/nXhVq3y.png", # Optional thumbnail image
|
| 40 |
+
theme="huggingface", # Choose a theme (optional)
|
| 41 |
+
)
|
| 42 |
|
| 43 |
# Launch the Gradio app
|
| 44 |
+
interface.launch(server_port=12345) # Change port if needed
|
| 45 |
+
|
| 46 |
+
print("App launched! Visit http://localhost:12345 to use it.")
|
requirements.txt.bak
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
onnxruntime
|