candenizkocak commited on
Commit
d235be4
·
verified ·
1 Parent(s): fcac7c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -8
app.py CHANGED
@@ -7,12 +7,13 @@ import os
7
  # --- Constants for the API ---
8
  API_URL = "https://predict.ultralytics.com"
9
  # It's recommended to use Hugging Face secrets for API keys
10
- API_KEY = os.environ.get("ULTRALYTICS_API_KEY")
11
  MODEL_ID = "https://hub.ultralytics.com/models/RsLHnWMhiBPqy3iFZAgr"
12
 
13
  def classify_image(image):
14
  """
15
- Takes an image, sends it to the Ultralytics API, and returns the classification.
 
16
  """
17
  # Convert the Gradio image (numpy array) to a file-like object
18
  image_pil = Image.fromarray(image)
@@ -34,11 +35,25 @@ def classify_image(image):
34
  # Check for a successful response
35
  response.raise_for_status()
36
 
37
- # Return the JSON response from the API
38
- return response.json()
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  except requests.exceptions.RequestException as e:
41
- return f"API Request Error: {e}"
 
 
42
  finally:
43
  # Clean up the temporary image file
44
  if os.path.exists(image_path):
@@ -48,7 +63,11 @@ def classify_image(image):
48
 
49
  # Define the input and output components
50
  image_input = gr.Image(type="numpy", label="Upload an Image or Use Webcam")
51
- json_output = gr.JSON(label="Classification Results")
 
 
 
 
52
 
53
  # List of example images
54
  example_images = [
@@ -63,9 +82,9 @@ example_images = [
63
  iface = gr.Interface(
64
  fn=classify_image,
65
  inputs=image_input,
66
- outputs=json_output,
67
  title="Image Classification with Ultralytics API",
68
- description="Upload a picture or use your camera to classify an image using a pre-trained model. The results from the API will be displayed in JSON format.",
69
  examples=example_images
70
  )
71
 
 
7
  # --- Constants for the API ---
8
  API_URL = "https://predict.ultralytics.com"
9
  # It's recommended to use Hugging Face secrets for API keys
10
+ API_KEY = os.environ.get("ULTRALYTICS_API_KEY", "a6684f3c4a050ec709948f66db7a391633a2064fe7")
11
  MODEL_ID = "https://hub.ultralytics.com/models/RsLHnWMhiBPqy3iFZAgr"
12
 
13
  def classify_image(image):
14
  """
15
+ Takes an image, sends it to the Ultralytics API, and returns the
16
+ classification name and confidence score.
17
  """
18
  # Convert the Gradio image (numpy array) to a file-like object
19
  image_pil = Image.fromarray(image)
 
35
  # Check for a successful response
36
  response.raise_for_status()
37
 
38
+ # Parse the JSON response
39
+ result_data = response.json()
40
+
41
+ # Extract the relevant information
42
+ # The structure is data -> images -> [0] -> results -> [0]
43
+ if result_data.get("images") and result_data["images"][0].get("results"):
44
+ top_result = result_data["images"][0]["results"][0]
45
+ name = top_result.get("name")
46
+ confidence = top_result.get("confidence")
47
+
48
+ # Return the extracted values for the two output components
49
+ return name, f"{confidence:.2f}"
50
+ else:
51
+ return "No classification found", "N/A"
52
 
53
  except requests.exceptions.RequestException as e:
54
+ return f"API Error: {e}", ""
55
+ except (KeyError, IndexError):
56
+ return "Could not parse API response.", ""
57
  finally:
58
  # Clean up the temporary image file
59
  if os.path.exists(image_path):
 
63
 
64
  # Define the input and output components
65
  image_input = gr.Image(type="numpy", label="Upload an Image or Use Webcam")
66
+
67
+ # Define separate outputs for name and confidence
68
+ name_output = gr.Label(label="Classification")
69
+ confidence_output = gr.Textbox(label="Confidence Score")
70
+
71
 
72
  # List of example images
73
  example_images = [
 
82
  iface = gr.Interface(
83
  fn=classify_image,
84
  inputs=image_input,
85
+ outputs=[name_output, confidence_output],
86
  title="Image Classification with Ultralytics API",
87
+ description="Upload a picture or use your camera to classify an image using a pre-trained model. The model's prediction and confidence score will be displayed.",
88
  examples=example_images
89
  )
90