Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,23 +7,39 @@ models = {
|
|
7 |
"ModernBERT Large (gender)": "breadlicker45/ModernBERT-large-gender"
|
8 |
}
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
# Function to load the selected model and classify text
|
11 |
def classify_text(model_name, text):
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
# Map the numerical labels to human-readable labels
|
16 |
-
label_mapping = {"0": "Male", "1": "Female"}
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
return output_predictions
|
27 |
|
28 |
# Create the Gradio interface
|
29 |
interface = gr.Interface(
|
@@ -32,18 +48,20 @@ interface = gr.Interface(
|
|
32 |
gr.Dropdown(
|
33 |
list(models.keys()),
|
34 |
label="Select Model",
|
35 |
-
value="ModernBERT Base (gender)"
|
36 |
),
|
37 |
gr.Textbox(
|
38 |
lines=2,
|
39 |
-
placeholder="Enter text to
|
40 |
-
value="
|
41 |
)
|
42 |
],
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
46 |
)
|
47 |
|
48 |
# Launch the app
|
49 |
-
|
|
|
|
7 |
"ModernBERT Large (gender)": "breadlicker45/ModernBERT-large-gender"
|
8 |
}
|
9 |
|
10 |
+
# Define the mapping for user-friendly labels
|
11 |
+
# Note: Transformers pipelines often output 'LABEL_0', 'LABEL_1'.
|
12 |
+
# We handle potential variations like just '0', '1'.
|
13 |
+
label_map = {
|
14 |
+
"LABEL_0": "Male (0)",
|
15 |
+
"0": "Male (0)",
|
16 |
+
"LABEL_1": "Female (1)",
|
17 |
+
"1": "Female (1)"
|
18 |
+
}
|
19 |
+
|
20 |
# Function to load the selected model and classify text
|
21 |
def classify_text(model_name, text):
|
22 |
+
try:
|
23 |
+
classifier = pipeline("text-classification", model=models[model_name], top_k=None)
|
24 |
+
predictions = classifier(text)
|
|
|
|
|
25 |
|
26 |
+
# Process predictions to use friendly labels
|
27 |
+
processed_results = {}
|
28 |
+
if predictions and isinstance(predictions, list) and predictions[0]:
|
29 |
+
# predictions[0] should be a list of label dicts like [{'label': 'LABEL_1', 'score': 0.9...}, ...]
|
30 |
+
for pred in predictions[0]:
|
31 |
+
raw_label = pred["label"]
|
32 |
+
score = pred["score"]
|
33 |
+
# Use the map to get a friendly name, fallback to the raw label if not found
|
34 |
+
friendly_label = label_map.get(raw_label, raw_label)
|
35 |
+
processed_results[friendly_label] = score
|
36 |
+
return processed_results
|
37 |
+
except Exception as e:
|
38 |
+
# Handle potential errors during model loading or inference
|
39 |
+
print(f"Error: {e}")
|
40 |
+
# Return an error message suitable for gr.Label
|
41 |
+
return {"Error": f"Failed to process: {e}"}
|
42 |
|
|
|
43 |
|
44 |
# Create the Gradio interface
|
45 |
interface = gr.Interface(
|
|
|
48 |
gr.Dropdown(
|
49 |
list(models.keys()),
|
50 |
label="Select Model",
|
51 |
+
value="ModernBERT Base (gender)" # Default model
|
52 |
),
|
53 |
gr.Textbox(
|
54 |
lines=2,
|
55 |
+
placeholder="Enter text to classify for perceived gender...", # Corrected placeholder
|
56 |
+
value="This is an example sentence." # Changed example text
|
57 |
)
|
58 |
],
|
59 |
+
# The gr.Label component works well for showing classification scores
|
60 |
+
outputs=gr.Label(num_top_classes=2), # Show both classes explicitly
|
61 |
+
title="ModernBERT Gender Classifier",
|
62 |
+
description="Select a model and enter a sentence to see the perceived gender classification (Male=0, Female=1) and confidence scores. Note: Text-based gender classification can be unreliable and reflect societal biases.", # Updated description
|
63 |
)
|
64 |
|
65 |
# Launch the app
|
66 |
+
if __name__ == "__main__":
|
67 |
+
interface.launch()
|