Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -15,7 +15,10 @@ processor = AutoProcessor.from_pretrained(checkpoint)
|
|
15 |
|
16 |
def classify_image(image, candidate_labels):
|
17 |
messages = []
|
18 |
-
candidate_labels = [label.strip() for label in candidate_labels.split(",")] + ["other"]
|
|
|
|
|
|
|
19 |
|
20 |
# Blur the image
|
21 |
image = image.filter(ImageFilter.GaussianBlur(radius=5))
|
@@ -42,11 +45,11 @@ def classify_image(image, candidate_labels):
|
|
42 |
|
43 |
# Decision-making logic
|
44 |
top_label = results[0]["label"]
|
45 |
-
second_label = results[1]["label"]
|
46 |
|
47 |
# Add messages to understand the scores
|
48 |
messages.append(f"Top label: {top_label} with score: {results[0]['score']:.2f}")
|
49 |
-
messages.append(f"Second label: {second_label} with score: {results[1]['score']:.2f}")
|
50 |
|
51 |
# Example decision logic for specific scenarios (can be customized further)
|
52 |
if top_label == candidate_labels[0] and results[0]["score"] >= 0.58 and second_label != "other":
|
@@ -63,11 +66,19 @@ def classify_image(image, candidate_labels):
|
|
63 |
|
64 |
return result, top_label, results_for_df, messages
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
iface = gr.Interface(
|
67 |
fn=classify_image,
|
68 |
inputs=[
|
69 |
-
gr.Image(type="pil", label="Upload an Image"),
|
70 |
-
gr.Textbox(label="Candidate Labels (comma separated)")
|
71 |
],
|
72 |
outputs=[
|
73 |
gr.Label(label="Result"),
|
@@ -79,12 +90,13 @@ iface = gr.Interface(
|
|
79 |
description="""
|
80 |
**Instructions:**
|
81 |
|
82 |
-
1. **Upload an Image**: Drag and drop an image or click to upload an image file.
|
83 |
|
84 |
2. **Enter Candidate Labels**:
|
85 |
- Provide candidate labels separated by commas.
|
86 |
- For example: `human with beverage,human,beverage`
|
87 |
- The label "other" will automatically be added to the list of candidate labels.
|
|
|
88 |
|
89 |
3. **View Results**:
|
90 |
- The result will indicate whether the specified action (top label) is present in the image.
|
@@ -94,4 +106,4 @@ iface = gr.Interface(
|
|
94 |
)
|
95 |
|
96 |
if __name__ == "__main__":
|
97 |
-
iface.launch()
|
|
|
15 |
|
16 |
def classify_image(image, candidate_labels):
|
17 |
messages = []
|
18 |
+
candidate_labels = [label.strip() for label in candidate_labels.split(",") if label.strip()] + ["other"]
|
19 |
+
|
20 |
+
if len(candidate_labels) == 1:
|
21 |
+
candidate_labels.append("other")
|
22 |
|
23 |
# Blur the image
|
24 |
image = image.filter(ImageFilter.GaussianBlur(radius=5))
|
|
|
45 |
|
46 |
# Decision-making logic
|
47 |
top_label = results[0]["label"]
|
48 |
+
second_label = results[1]["label"] if len(results) > 1 else "None"
|
49 |
|
50 |
# Add messages to understand the scores
|
51 |
messages.append(f"Top label: {top_label} with score: {results[0]['score']:.2f}")
|
52 |
+
messages.append(f"Second label: {second_label} with score: {results[1]['score']:.2f}" if len(results) > 1 else "")
|
53 |
|
54 |
# Example decision logic for specific scenarios (can be customized further)
|
55 |
if top_label == candidate_labels[0] and results[0]["score"] >= 0.58 and second_label != "other":
|
|
|
66 |
|
67 |
return result, top_label, results_for_df, messages
|
68 |
|
69 |
+
# Default values
|
70 |
+
default_labels = "human with beverage,human,beverage"
|
71 |
+
default_image_url = "F50tIRvbsAAKHzW.jpeg"
|
72 |
+
|
73 |
+
# Download default image
|
74 |
+
response = requests.get(default_image_url)
|
75 |
+
default_image = Image.open(BytesIO(response.content))
|
76 |
+
|
77 |
iface = gr.Interface(
|
78 |
fn=classify_image,
|
79 |
inputs=[
|
80 |
+
gr.Image(type="pil", label="Upload an Image", value=default_image),
|
81 |
+
gr.Textbox(label="Candidate Labels (comma separated)", value=default_labels)
|
82 |
],
|
83 |
outputs=[
|
84 |
gr.Label(label="Result"),
|
|
|
90 |
description="""
|
91 |
**Instructions:**
|
92 |
|
93 |
+
1. **Upload an Image**: Drag and drop an image or click to upload an image file. A default image is provided.
|
94 |
|
95 |
2. **Enter Candidate Labels**:
|
96 |
- Provide candidate labels separated by commas.
|
97 |
- For example: `human with beverage,human,beverage`
|
98 |
- The label "other" will automatically be added to the list of candidate labels.
|
99 |
+
- You can enter just one label, and "other" will still be added automatically. Default labels are provided.
|
100 |
|
101 |
3. **View Results**:
|
102 |
- The result will indicate whether the specified action (top label) is present in the image.
|
|
|
106 |
)
|
107 |
|
108 |
if __name__ == "__main__":
|
109 |
+
iface.launch()
|