Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# --- 1. Load Model from Hugging Face Hub ---
|
| 6 |
+
|
| 7 |
+
# Get the Hugging Face token from the Space's secrets
|
| 8 |
+
# This is crucial for accessing a private model
|
| 9 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
+
|
| 11 |
+
# Ensure the token is set
|
| 12 |
+
if HF_TOKEN is None:
|
| 13 |
+
raise ValueError(
|
| 14 |
+
"Hugging Face token not found. Please set the HF_TOKEN secret in your Space settings."
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# The ID of your private model on the Hub
|
| 18 |
+
MODEL_ID = "breadlicker45/bilingual-large-gender-v4-test"
|
| 19 |
+
|
| 20 |
+
print(f"Loading model: {MODEL_ID}...")
|
| 21 |
+
try:
|
| 22 |
+
# Explicitly load tokenizer and model to pass the token and trust_remote_code
|
| 23 |
+
# trust_remote_code=True is needed for models with custom architectures/code
|
| 24 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
| 25 |
+
|
| 26 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
| 27 |
+
MODEL_ID,
|
| 28 |
+
token=HF_TOKEN,
|
| 29 |
+
trust_remote_code=True # IMPORTANT for custom models
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Create the pipeline using the pre-loaded model and tokenizer
|
| 33 |
+
classifier = pipeline(
|
| 34 |
+
"text-classification",
|
| 35 |
+
model=model,
|
| 36 |
+
tokenizer=tokenizer
|
| 37 |
+
)
|
| 38 |
+
print("Model loaded successfully!")
|
| 39 |
+
|
| 40 |
+
except Exception as e:
|
| 41 |
+
# Provide a helpful error message if loading fails
|
| 42 |
+
print(f"Error loading model: {e}")
|
| 43 |
+
# You can display this error in the Gradio UI as well if you want
|
| 44 |
+
# For now, we'll just let the Space crash with a clear log message.
|
| 45 |
+
raise e
|
| 46 |
+
|
| 47 |
+
# --- 2. Define the Prediction Function ---
|
| 48 |
+
|
| 49 |
+
def classify_gender(text: str) -> dict:
|
| 50 |
+
"""
|
| 51 |
+
Takes a string of text and returns the model's predictions
|
| 52 |
+
in a format that Gradio's Label component can display.
|
| 53 |
+
"""
|
| 54 |
+
if not text or not text.strip():
|
| 55 |
+
# Handle empty or whitespace-only input gracefully
|
| 56 |
+
return None
|
| 57 |
+
|
| 58 |
+
# The pipeline will run the text through the model
|
| 59 |
+
# top_k=3 ensures we get scores for all 3 labels
|
| 60 |
+
predictions = classifier(text, top_k=3)
|
| 61 |
+
|
| 62 |
+
# Format the predictions into a {label: confidence} dictionary for the gr.Label component
|
| 63 |
+
formatted_predictions = {p['label']: p['score'] for p in predictions}
|
| 64 |
+
return formatted_predictions
|
| 65 |
+
|
| 66 |
+
# --- 3. Create the Gradio Interface ---
|
| 67 |
+
|
| 68 |
+
DESCRIPTION = """
|
| 69 |
+
## Bilingual Gender Classifier
|
| 70 |
+
This is a demo for the private model `breadlicker45/bilingual-large-gender-v4-test`.
|
| 71 |
+
Enter a sentence in **English or Spanish**, and the model will predict whether the text has a male, female, or neutral connotation.
|
| 72 |
+
**Disclaimer:** This model, like any AI, can have biases and may not always be accurate. It is intended for demonstration purposes.
|
| 73 |
+
"""
|
| 74 |
+
|
| 75 |
+
ARTICLE = """
|
| 76 |
+
<div style='text-align: center;'>
|
| 77 |
+
<p>Model based on <a href='https://huggingface.co/xlm-roberta-large' target='_blank'>XLM-RoBERTa-Large</a>, fine-tuned for gender classification.</p>
|
| 78 |
+
<p>This is a private model, but you can find more public models on the <a href='https://huggingface.co/models' target='_blank'>Hugging Face Hub</a>.</p>
|
| 79 |
+
</div>
|
| 80 |
+
"""
|
| 81 |
+
|
| 82 |
+
# Define some examples for users to try
|
| 83 |
+
examples = [
|
| 84 |
+
["He went to the store to buy a new hammer."],
|
| 85 |
+
["La doctora le recetó un medicamento a su paciente."],
|
| 86 |
+
["The development team will present their findings tomorrow."],
|
| 87 |
+
["My sister is the best programmer I know."],
|
| 88 |
+
["El futbolista marcó el gol decisivo."],
|
| 89 |
+
["The flight crew is preparing for takeoff."]
|
| 90 |
+
]
|
| 91 |
+
|
| 92 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 93 |
+
gr.Markdown(DESCRIPTION)
|
| 94 |
+
|
| 95 |
+
with gr.Row():
|
| 96 |
+
with gr.Column(scale=2):
|
| 97 |
+
text_input = gr.Textbox(
|
| 98 |
+
lines=5,
|
| 99 |
+
label="Input Text",
|
| 100 |
+
placeholder="Enter a sentence in English or Spanish here..."
|
| 101 |
+
)
|
| 102 |
+
submit_btn = gr.Button("Classify Text", variant="primary")
|
| 103 |
+
|
| 104 |
+
with gr.Column(scale=1):
|
| 105 |
+
output_label = gr.Label(
|
| 106 |
+
num_top_classes=3,
|
| 107 |
+
label="Classification Results"
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
gr.Examples(
|
| 111 |
+
examples=examples,
|
| 112 |
+
inputs=text_input,
|
| 113 |
+
outputs=output_label,
|
| 114 |
+
fn=classify_gender,
|
| 115 |
+
cache_examples=True
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
gr.Markdown(ARTICLE)
|
| 119 |
+
|
| 120 |
+
submit_btn.click(
|
| 121 |
+
fn=classify_gender,
|
| 122 |
+
inputs=text_input,
|
| 123 |
+
outputs=output_label,
|
| 124 |
+
api_name="classify" # You can add an API name for programmatic access
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
# --- 4. Launch the App ---
|
| 128 |
+
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
demo.launch()
|