Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
HuggingFace Space - ESS Variable Classification Demo
|
| 3 |
+
Interactive Gradio interface for the XLM-RoBERTa ESS classifier.
|
| 4 |
+
"""
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
|
| 8 |
+
# Load the model
|
| 9 |
+
MODEL_NAME = "benjaminBeuster/xlm-roberta-base-ess-classification"
|
| 10 |
+
classifier = pipeline("text-classification", model=MODEL_NAME)
|
| 11 |
+
|
| 12 |
+
# Category descriptions
|
| 13 |
+
CATEGORY_INFO = {
|
| 14 |
+
"DEMOGRAPHY (POPULATION, VITAL STATISTICS, AND CENSUSES)": "Demographics, population statistics, age, gender",
|
| 15 |
+
"ECONOMICS": "Economic issues, finance, income",
|
| 16 |
+
"EDUCATION": "Education, schooling, qualifications",
|
| 17 |
+
"HEALTH": "Healthcare, medical services, health satisfaction",
|
| 18 |
+
"POLITICS": "Political systems, trust in government, parliament",
|
| 19 |
+
"SOCIETY AND CULTURE": "Social issues, cultural topics, religion",
|
| 20 |
+
"LABOUR AND EMPLOYMENT": "Work, occupation, employment status",
|
| 21 |
+
"PSYCHOLOGY": "Mental health, psychological wellbeing",
|
| 22 |
+
"OTHER": "General or uncategorized topics"
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
def classify_text(text):
|
| 26 |
+
"""Classify survey question/variable."""
|
| 27 |
+
if not text.strip():
|
| 28 |
+
return "Please enter some text to classify."
|
| 29 |
+
|
| 30 |
+
result = classifier(text)[0]
|
| 31 |
+
label = result['label']
|
| 32 |
+
score = result['score']
|
| 33 |
+
|
| 34 |
+
# Format output
|
| 35 |
+
output = f"**Category:** {label}\n\n"
|
| 36 |
+
output += f"**Confidence:** {score:.2%}\n\n"
|
| 37 |
+
|
| 38 |
+
if label in CATEGORY_INFO:
|
| 39 |
+
output += f"**Description:** {CATEGORY_INFO[label]}"
|
| 40 |
+
|
| 41 |
+
return output
|
| 42 |
+
|
| 43 |
+
# Example questions
|
| 44 |
+
examples = [
|
| 45 |
+
["What is your age?"],
|
| 46 |
+
["How satisfied are you with the healthcare system?"],
|
| 47 |
+
["Trust in country's parliament"],
|
| 48 |
+
["What is your highest level of education?"],
|
| 49 |
+
["How often do you pray?"],
|
| 50 |
+
["What is your occupation?"],
|
| 51 |
+
["Do you feel safe walking alone at night?"],
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
# Create Gradio interface
|
| 55 |
+
demo = gr.Interface(
|
| 56 |
+
fn=classify_text,
|
| 57 |
+
inputs=gr.Textbox(
|
| 58 |
+
lines=3,
|
| 59 |
+
placeholder="Enter a survey question or variable description...",
|
| 60 |
+
label="Input Text"
|
| 61 |
+
),
|
| 62 |
+
outputs=gr.Markdown(label="Classification Result"),
|
| 63 |
+
title="ESS Variable Classification",
|
| 64 |
+
description="""
|
| 65 |
+
Classify European Social Survey (ESS) variables into 19 subject categories.
|
| 66 |
+
|
| 67 |
+
This model is fine-tuned from XLM-RoBERTa-Base and achieves 83.8% accuracy on the test set.
|
| 68 |
+
""",
|
| 69 |
+
examples=examples,
|
| 70 |
+
article="""
|
| 71 |
+
### About
|
| 72 |
+
|
| 73 |
+
This classifier helps organize survey variables by automatically categorizing them into subject areas.
|
| 74 |
+
Built on [XLM-RoBERTa-Base](https://huggingface.co/FacebookAI/xlm-roberta-base),
|
| 75 |
+
trained on European Social Survey metadata.
|
| 76 |
+
|
| 77 |
+
**Model:** [benjaminBeuster/xlm-roberta-base-ess-classification](https://huggingface.co/benjaminBeuster/xlm-roberta-base-ess-classification)
|
| 78 |
+
|
| 79 |
+
**Performance:** 83.8% accuracy | F1: 0.796 (weighted)
|
| 80 |
+
""",
|
| 81 |
+
theme=gr.themes.Soft(),
|
| 82 |
+
allow_flagging="never"
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
demo.launch()
|