import gradio as gr from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification import os # --- 1. Load Model from Hugging Face Hub --- # Get the Hugging Face token from the Space's secrets # This is crucial for accessing a private model HF_TOKEN = os.getenv("HF_TOKEN") # Ensure the token is set if HF_TOKEN is None: raise ValueError( "Hugging Face token not found. Please set the HF_TOKEN secret in your Space settings." ) # The ID of your private model on the Hub MODEL_ID = "breadlicker45/bilingual-large-gender-v4-test" print(f"Loading model: {MODEL_ID}...") try: # Explicitly load tokenizer and model to pass the token and trust_remote_code # trust_remote_code=True is needed for models with custom architectures/code tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN) model = AutoModelForSequenceClassification.from_pretrained( MODEL_ID, token=HF_TOKEN, trust_remote_code=True # IMPORTANT for custom models ) # Create the pipeline using the pre-loaded model and tokenizer classifier = pipeline( "text-classification", model=model, tokenizer=tokenizer ) print("Model loaded successfully!") except Exception as e: # Provide a helpful error message if loading fails print(f"Error loading model: {e}") # You can display this error in the Gradio UI as well if you want # For now, we'll just let the Space crash with a clear log message. raise e # --- 2. Define the Prediction Function --- def classify_gender(text: str) -> dict: """ Takes a string of text and returns the model's predictions in a format that Gradio's Label component can display. """ if not text or not text.strip(): # Handle empty or whitespace-only input gracefully return None # The pipeline will run the text through the model # top_k=3 ensures we get scores for all 3 labels predictions = classifier(text, top_k=3) # Format the predictions into a {label: confidence} dictionary for the gr.Label component formatted_predictions = {p['label']: p['score'] for p in predictions} return formatted_predictions # --- 3. Create the Gradio Interface --- DESCRIPTION = """ ## Bilingual Gender Classifier This is a demo for the private model `breadlicker45/bilingual-large-gender-v4-test`. Enter a sentence in **English or Spanish**, and the model will predict whether the text has a male, female, or neutral connotation. **Disclaimer:** This model, like any AI, can have biases and may not always be accurate. It is intended for demonstration purposes. """ ARTICLE = """
Model based on XLM-RoBERTa-Large, fine-tuned for gender classification.
This is a private model, but you can find more public models on the Hugging Face Hub.