Spaces:
Sleeping
Sleeping
File size: 1,033 Bytes
3c3372c da0a632 3c3372c f1739be 3c3372c f1739be 3c3372c f1739be 3c3372c f1739be 3c3372c f1739be 3c3372c f1739be 3c3372c f1739be 3c3372c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import gradio as gr
import requests
import cv2
# URL of the API created with FastAPI
API_URL = "https://lab3-nuj8.onrender.com"
# Function to execute when clicking the "Predict button"
def predict(image):
try:
image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
_, img_encoded = cv2.imencode(".jpg", image_bgr)
files = {"file": ("image.jpg", img_encoded.tobytes(), "image/jpeg")}
response = requests.post(f"{API_URL}/predict", files=files, timeout=120)
response.raise_for_status()
data = response.json()
return data.get("predicted_class")
except Exception as e:
return f"Error: {str(e)}"
# GUI creted using Gradio
iface = gr.Interface(
fn=predict,
inputs=gr.Image(label="Upload Image", type="numpy", height=400),
outputs=gr.Textbox(label="Predicted class"),
title="Cat/Dog predictor GUI",
description="Cat/Dog predictor GUI powered by Fastapi + Render + Docker",
)
# Launch the GUI
if __name__ == "__main__":
iface.launch() |