prithivMLmods commited on
Commit
f347918
·
verified ·
1 Parent(s): e426d6e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -36
app.py CHANGED
@@ -1,41 +1,44 @@
1
  import gradio as gr
2
- import spaces
3
- from transformers import AutoImageProcessor
4
- from transformers import SiglipForImageClassification
5
- from transformers.image_utils import load_image
6
- from PIL import Image
7
- import torch
8
 
9
- # Load model and processor
10
- model_name = "prithivMLmods/Gender-Classifier-Mini"
11
- model = SiglipForImageClassification.from_pretrained(model_name)
12
- processor = AutoImageProcessor.from_pretrained(model_name)
13
 
14
- @spaces.GPU
15
- def gender_classification(image):
16
- """Predicts gender category for an image."""
17
- image = Image.fromarray(image).convert("RGB")
18
- inputs = processor(images=image, return_tensors="pt")
19
-
20
- with torch.no_grad():
21
- outputs = model(**inputs)
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
- # Create Gradio interface
31
- iface = gr.Interface(
32
- fn=gender_classification,
33
- inputs=gr.Image(type="numpy"),
34
- outputs=gr.Label(label="Prediction Scores"),
35
- title="Gender Classification",
36
- description="Upload an image to classify its gender."
37
- )
 
 
 
 
 
 
 
 
38
 
39
- # Launch the app
40
- if __name__ == "__main__":
41
- iface.launch()
 
 
 
 
 
 
 
 
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()