Spaces:
Sleeping
Sleeping
Commit ·
f1739be
1
Parent(s): 3c3372c
Upgrading gradio app
Browse files- .gitignore +1 -0
- app.py +18 -65
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.venv/
|
app.py
CHANGED
|
@@ -2,79 +2,32 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
|
| 4 |
# URL of the API created with FastAPI
|
| 5 |
-
API_URL = "https://
|
| 6 |
|
| 7 |
-
# Function to
|
| 8 |
-
def
|
| 9 |
try:
|
| 10 |
-
|
| 11 |
-
import io
|
| 12 |
-
from PIL import Image
|
| 13 |
|
| 14 |
-
|
| 15 |
-
image.
|
| 16 |
-
img_byte_arr.seek(0)
|
| 17 |
-
|
| 18 |
-
# Prepare the request
|
| 19 |
-
files = {"file": ("image.png", img_byte_arr, "image/png")}
|
| 20 |
-
data = {"class_names": class_names}
|
| 21 |
-
|
| 22 |
-
response = requests.post(f"{API_URL}/predict", files=files, data=data, timeout=10)
|
| 23 |
-
response.raise_for_status()
|
| 24 |
-
result = response.json()
|
| 25 |
-
return result.get("predicted_class")
|
| 26 |
-
except requests.exceptions.HTTPError as e:
|
| 27 |
-
return f"Error: {response.json().get('detail', str(e))}"
|
| 28 |
-
except Exception as e:
|
| 29 |
-
return f"Error: {str(e)}"
|
| 30 |
|
| 31 |
-
|
| 32 |
-
def resize_image(image, width, height):
|
| 33 |
-
try:
|
| 34 |
-
import io
|
| 35 |
-
|
| 36 |
-
img_byte_arr = io.BytesIO()
|
| 37 |
-
image.save(img_byte_arr, format='PNG')
|
| 38 |
-
img_byte_arr.seek(0)
|
| 39 |
-
|
| 40 |
-
files = {"file": ("image.png", img_byte_arr, "image/png")}
|
| 41 |
-
data = {"width": int(width), "height": int(height)}
|
| 42 |
-
|
| 43 |
-
response = requests.post(f"{API_URL}/resize", files=files, data=data, timeout=10)
|
| 44 |
response.raise_for_status()
|
| 45 |
-
|
| 46 |
-
return
|
| 47 |
-
except requests.exceptions.HTTPError as e:
|
| 48 |
-
return f"Error: {response.json().get('detail', str(e))}"
|
| 49 |
except Exception as e:
|
| 50 |
return f"Error: {str(e)}"
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
label="Class Names (comma-separated)"
|
| 62 |
-
)
|
| 63 |
-
predict_btn = gr.Button("Predict")
|
| 64 |
-
predict_output = gr.Textbox(label="Predicted Class")
|
| 65 |
-
|
| 66 |
-
predict_btn.click(predict_image, inputs=[img_input, class_input], outputs=predict_output)
|
| 67 |
-
|
| 68 |
-
with gr.Tab("Resize"):
|
| 69 |
-
with gr.Row():
|
| 70 |
-
img_resize = gr.Image(type="pil", label="Upload Image")
|
| 71 |
-
with gr.Row():
|
| 72 |
-
width_input = gr.Number(value=256, label="Width")
|
| 73 |
-
height_input = gr.Number(value=256, label="Height")
|
| 74 |
-
resize_btn = gr.Button("Resize")
|
| 75 |
-
resize_output = gr.Textbox(label="New Dimensions")
|
| 76 |
-
|
| 77 |
-
resize_btn.click(resize_image, inputs=[img_resize, width_input, height_input], outputs=resize_output)
|
| 78 |
|
| 79 |
# Launch the GUI
|
| 80 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import requests
|
| 3 |
|
| 4 |
# URL of the API created with FastAPI
|
| 5 |
+
API_URL = "https://lab3-nuj8.onrender.com"
|
| 6 |
|
| 7 |
+
# Function to execute when clicking the "Predict button"
|
| 8 |
+
def predict(image):
|
| 9 |
try:
|
| 10 |
+
image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
_, img_encoded = cv2.imencode(".jpg", image_bgr)
|
| 13 |
+
files = {"file": ("image.jpg", img_encoded.tobytes(), "image/jpeg")}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
response = requests.post(f"{API_URL}/predict", files=files, timeout=120)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
response.raise_for_status()
|
| 17 |
+
data = response.json()
|
| 18 |
+
return data.get("predicted_class")
|
|
|
|
|
|
|
| 19 |
except Exception as e:
|
| 20 |
return f"Error: {str(e)}"
|
| 21 |
|
| 22 |
+
|
| 23 |
+
# GUI creted using Gradio
|
| 24 |
+
iface = gr.Interface(
|
| 25 |
+
fn=predict,
|
| 26 |
+
inputs=gr.Image(label="Upload Image", type="numpy", height=400),
|
| 27 |
+
outputs=gr.Textbox(label="Predicted class"),
|
| 28 |
+
title="Cat/Dog predictor GUI",
|
| 29 |
+
description="Cat/Dog predictor GUI powered by Fastapi + Render + Docker",
|
| 30 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Launch the GUI
|
| 33 |
if __name__ == "__main__":
|