breadlicker45 commited on
Commit
65c4e1b
·
verified ·
1 Parent(s): d8982cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -31
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
 
3
  import torch
4
  import os
5
 
@@ -10,20 +11,21 @@ HF_TOKEN = os.getenv("HF_TOKEN")
10
  if HF_TOKEN is None:
11
  raise ValueError("Hugging Face token not found. Please set the HF_TOKEN secret in your Space settings.")
12
 
13
- # The ID of your private model on the Hub
14
  MODEL_ID = "breadlicker45/bilingual-large-gender-v4-test"
15
-
16
- # Set up device (use GPU if available, otherwise CPU)
17
  device = "cuda" if torch.cuda.is_available() else "cpu"
18
  print(f"Using device: {device}")
19
 
20
  print(f"Loading model: {MODEL_ID}...")
21
  try:
22
- # Load tokenizer and model, providing the token for private access
23
- tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN, trust_remote_code=True)
24
- model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID, token=HF_TOKEN,trust_remote_code=True, num_labels=3)
 
 
 
 
25
 
26
- # Move the model to the selected device ONCE for efficiency
27
  model.to(device)
28
  print("Model loaded successfully!")
29
 
@@ -32,38 +34,19 @@ except Exception as e:
32
  raise e
33
 
34
  # --- 2. Define the Manual Prediction Function ---
35
-
36
  def classify_gender(text: str) -> dict:
37
- """
38
- Performs manual inference on the input text and returns a dictionary
39
- of label probabilities suitable for Gradio's Label component.
40
- """
41
  if not text or not text.strip():
42
- return None # Handle empty input
43
 
44
- # 1. Tokenize the input text
45
- # The tokenizer prepares the text in the format the model expects.
46
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
47
-
48
- # 2. Move tokenized inputs to the same device as the model
49
  inputs = {k: v.to(device) for k, v in inputs.items()}
50
 
51
- # 3. Get model predictions
52
- # `torch.no_grad()` is used for inference to disable gradient calculations,
53
- # which saves memory and speeds up computation.
54
  with torch.no_grad():
55
  logits = model(**inputs).logits
56
 
57
- # 4. Convert logits to probabilities
58
- # The softmax function converts the raw output scores (logits) into a
59
- # probability distribution across all labels.
60
  probabilities = torch.nn.functional.softmax(logits, dim=-1)
61
-
62
- # 5. Format the output for Gradio's Label component
63
- # We create a dictionary mapping each label name to its probability score.
64
- # `model.config.id2label` provides the mapping from class index to label name
65
- # e.g., {0: 'female', 1: 'male', 2: 'neutral'}
66
- scores = probabilities.squeeze().tolist() # Convert tensor to a simple list
67
 
68
  results = {}
69
  for i, score in enumerate(scores):
@@ -73,7 +56,7 @@ def classify_gender(text: str) -> dict:
73
  return results
74
 
75
  # --- 3. Create the Gradio Interface ---
76
- # (This part remains the same as it correctly displays the dictionary output)
77
 
78
  DESCRIPTION = """
79
  ## Bilingual Gender Classifier
 
1
  import gradio as gr
2
+ # BE EXPLICIT: Import the specific model class we need
3
+ from transformers import AutoTokenizer, XLMRobertaForSequenceClassification
4
  import torch
5
  import os
6
 
 
11
  if HF_TOKEN is None:
12
  raise ValueError("Hugging Face token not found. Please set the HF_TOKEN secret in your Space settings.")
13
 
 
14
  MODEL_ID = "breadlicker45/bilingual-large-gender-v4-test"
 
 
15
  device = "cuda" if torch.cuda.is_available() else "cpu"
16
  print(f"Using device: {device}")
17
 
18
  print(f"Loading model: {MODEL_ID}...")
19
  try:
20
+ # Tokenizer can still be loaded automatically
21
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
22
+
23
+ # THE FIX: Use the explicit class instead of AutoModelForSequenceClassification.
24
+ # This ignores the problematic 'auto_map' in config.json and forces the
25
+ # use of the standard XLM-RoBERTa architecture for sequence classification.
26
+ model = XLMRobertaForSequenceClassification.from_pretrained(MODEL_ID, token=HF_TOKEN)
27
 
28
+ # Move the model to the selected device
29
  model.to(device)
30
  print("Model loaded successfully!")
31
 
 
34
  raise e
35
 
36
  # --- 2. Define the Manual Prediction Function ---
37
+ # (This function is already correct and does not need changes)
38
  def classify_gender(text: str) -> dict:
 
 
 
 
39
  if not text or not text.strip():
40
+ return None
41
 
 
 
42
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
 
 
43
  inputs = {k: v.to(device) for k, v in inputs.items()}
44
 
 
 
 
45
  with torch.no_grad():
46
  logits = model(**inputs).logits
47
 
 
 
 
48
  probabilities = torch.nn.functional.softmax(logits, dim=-1)
49
+ scores = probabilities.squeeze().tolist()
 
 
 
 
 
50
 
51
  results = {}
52
  for i, score in enumerate(scores):
 
56
  return results
57
 
58
  # --- 3. Create the Gradio Interface ---
59
+ # (This part remains the same)
60
 
61
  DESCRIPTION = """
62
  ## Bilingual Gender Classifier