Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,44 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
from
|
4 |
-
from transformers import SiglipForImageClassification
|
5 |
-
from transformers.image_utils import load_image
|
6 |
-
from PIL import Image
|
7 |
-
import torch
|
8 |
|
9 |
-
#
|
10 |
-
model_name
|
11 |
-
|
12 |
-
processor = AutoImageProcessor.from_pretrained(model_name)
|
13 |
|
14 |
-
|
15 |
-
def
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
logits = outputs.logits
|
23 |
-
probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist()
|
24 |
-
|
25 |
-
labels = {"0": "Female ♀", "1": "Male ♂"}
|
26 |
-
predictions = {labels[str(i)]: round(probs[i], 3) for i in range(len(probs))}
|
27 |
-
|
28 |
-
return predictions
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from gender_classification import gender_classification
|
3 |
+
from emotion_classification import emotion_classification
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# Function to update the selected model state when a button is clicked.
|
6 |
+
def select_model(model_name):
|
7 |
+
return model_name
|
|
|
8 |
|
9 |
+
# Main classification function that calls the appropriate model based on selection.
|
10 |
+
def classify(image, model_name):
|
11 |
+
if model_name == "gender":
|
12 |
+
return gender_classification(image)
|
13 |
+
elif model_name == "emotion":
|
14 |
+
return emotion_classification(image)
|
15 |
+
else:
|
16 |
+
return {"Error": "No model selected"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
with gr.Blocks() as demo:
|
19 |
+
# Sidebar with title and model selection buttons
|
20 |
+
with gr.Sidebar():
|
21 |
+
gr.Markdown("# SigLIP2 Classification")
|
22 |
+
with gr.Row():
|
23 |
+
gender_btn = gr.Button("Gender Classification")
|
24 |
+
emotion_btn = gr.Button("Emotion Classification")
|
25 |
+
# State to hold the current model choice
|
26 |
+
selected_model = gr.State("gender")
|
27 |
+
# Set model state when buttons are clicked
|
28 |
+
gender_btn.click(fn=select_model, inputs=[], outputs=selected_model, _js="() => 'gender'")
|
29 |
+
emotion_btn.click(fn=select_model, inputs=[], outputs=selected_model, _js="() => 'emotion'")
|
30 |
+
gr.Markdown("### Current Model:")
|
31 |
+
model_display = gr.Textbox(value="gender", interactive=False)
|
32 |
+
# Update display when state changes
|
33 |
+
selected_model.change(lambda m: m, selected_model, model_display)
|
34 |
|
35 |
+
# Main interface: image input, analyze button, and prediction output.
|
36 |
+
with gr.Column():
|
37 |
+
image_input = gr.Image(type="numpy", label="Upload Image")
|
38 |
+
analyze_btn = gr.Button("Analyze")
|
39 |
+
output_label = gr.Label(label="Prediction Scores")
|
40 |
+
|
41 |
+
# When the "Analyze" button is clicked, use the selected model to classify the image.
|
42 |
+
analyze_btn.click(fn=classify, inputs=[image_input, selected_model], outputs=output_label)
|
43 |
+
|
44 |
+
demo.launch()
|