Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import torch
|
|
| 3 |
import numpy as np
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
|
| 6 |
-
#
|
| 7 |
# Your Model's Hugging Face Repository ID
|
| 8 |
# NOTE: This assumes you successfully pushed the model during the CV loop!
|
| 9 |
HF_REPO_ID = "sumittguptaa148/DL-Gen-AI-Project"
|
|
@@ -20,19 +20,12 @@ BEST_THRESHOLDS = {
|
|
| 20 |
'surprise': 0.48
|
| 21 |
}
|
| 22 |
|
| 23 |
-
#
|
| 24 |
try:
|
| 25 |
-
# We load AutoModel for a custom architecture, but since we pushed
|
| 26 |
-
# using Trainer, the hub might save it with an AutoModelForSequenceClassification structure
|
| 27 |
-
# Let's try to load the base AutoModel and then load the state dict manually
|
| 28 |
-
|
| 29 |
# Load the tokenizer
|
| 30 |
tokenizer = AutoTokenizer.from_pretrained(HF_REPO_ID)
|
| 31 |
|
| 32 |
# Load the model weights and structure.
|
| 33 |
-
# We use AutoModelForSequenceClassification here as Hugging Face Trainer usually
|
| 34 |
-
# saves a classification head that is compatible with this class structure
|
| 35 |
-
# when pushing to the hub.
|
| 36 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 37 |
model = AutoModelForSequenceClassification.from_pretrained(HF_REPO_ID, num_labels=len(LABELS))
|
| 38 |
model.to(device)
|
|
@@ -47,7 +40,7 @@ except Exception as e:
|
|
| 47 |
device = "cpu"
|
| 48 |
|
| 49 |
|
| 50 |
-
#
|
| 51 |
def predict_emotion(text):
|
| 52 |
if model is None:
|
| 53 |
return "Model failed to load. Please check logs."
|
|
@@ -78,32 +71,25 @@ def predict_emotion(text):
|
|
| 78 |
threshold = BEST_THRESHOLDS[label]
|
| 79 |
|
| 80 |
# Classification
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
# Format for display
|
| 84 |
results[f"{label.capitalize()} ({is_present})"] = f"{prob:.4f} (Threshold: {threshold:.2f})"
|
| 85 |
|
| 86 |
return results
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
# Output components for Gradio
|
| 90 |
-
output_components = [
|
| 91 |
-
gr.Textbox(label=f"{label.capitalize()} (Classification & Prob)", lines=1) for label in LABELS
|
| 92 |
-
]
|
| 93 |
-
|
| 94 |
-
# Map output components to keys in the dictionary returned by predict_emotion
|
| 95 |
-
# Gradio expects the output component labels to match the dictionary keys
|
| 96 |
-
output_keys = [f"{label.capitalize()} ({{}})" for label in LABELS] # Placeholder for "Present/Absent" part
|
| 97 |
|
| 98 |
# Custom function to create the correct output components for Gradio
|
| 99 |
def get_output_components():
|
| 100 |
# Use Textbox for results
|
| 101 |
outputs = []
|
| 102 |
for label in LABELS:
|
|
|
|
| 103 |
outputs.append(gr.Textbox(label=f"{label.capitalize()} Emotion Result", lines=1))
|
| 104 |
return outputs
|
| 105 |
|
| 106 |
-
|
| 107 |
# Custom wrapper to ensure output matches the order of Gradio components
|
| 108 |
def predict_emotion_gradio(text):
|
| 109 |
raw_results = predict_emotion(text)
|
|
@@ -117,7 +103,6 @@ def predict_emotion_gradio(text):
|
|
| 117 |
|
| 118 |
return tuple(ordered_results)
|
| 119 |
|
| 120 |
-
|
| 121 |
title = "Multi-Label Emotion Classification with RoBERTa-Large"
|
| 122 |
description = "A DL/GenAI project classifying text into Anger, Fear, Joy, Sadness, and Surprise. The model uses a fine-tuned RoBERTa-Large with 5-Fold CV and dynamic threshold optimization."
|
| 123 |
|
|
@@ -127,6 +112,5 @@ gr.Interface(
|
|
| 127 |
outputs=get_output_components(), # Use the custom function to get ordered components
|
| 128 |
title=title,
|
| 129 |
description=description,
|
| 130 |
-
# allow_flagging
|
| 131 |
-
theme="huggingface"
|
| 132 |
).launch()
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
|
| 6 |
+
# Configuration
|
| 7 |
# Your Model's Hugging Face Repository ID
|
| 8 |
# NOTE: This assumes you successfully pushed the model during the CV loop!
|
| 9 |
HF_REPO_ID = "sumittguptaa148/DL-Gen-AI-Project"
|
|
|
|
| 20 |
'surprise': 0.48
|
| 21 |
}
|
| 22 |
|
| 23 |
+
# Load Model and Tokenizer
|
| 24 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# Load the tokenizer
|
| 26 |
tokenizer = AutoTokenizer.from_pretrained(HF_REPO_ID)
|
| 27 |
|
| 28 |
# Load the model weights and structure.
|
|
|
|
|
|
|
|
|
|
| 29 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 30 |
model = AutoModelForSequenceClassification.from_pretrained(HF_REPO_ID, num_labels=len(LABELS))
|
| 31 |
model.to(device)
|
|
|
|
| 40 |
device = "cpu"
|
| 41 |
|
| 42 |
|
| 43 |
+
# Prediction Function
|
| 44 |
def predict_emotion(text):
|
| 45 |
if model is None:
|
| 46 |
return "Model failed to load. Please check logs."
|
|
|
|
| 71 |
threshold = BEST_THRESHOLDS[label]
|
| 72 |
|
| 73 |
# Classification
|
| 74 |
+
# Using unicode checkmark/cross for better visual appeal in the Gradio app
|
| 75 |
+
is_present = "✅ Present" if prob >= threshold else "❌ Absent"
|
| 76 |
|
| 77 |
# Format for display
|
| 78 |
results[f"{label.capitalize()} ({is_present})"] = f"{prob:.4f} (Threshold: {threshold:.2f})"
|
| 79 |
|
| 80 |
return results
|
| 81 |
|
| 82 |
+
# Gradio Interface Setup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
# Custom function to create the correct output components for Gradio
|
| 85 |
def get_output_components():
|
| 86 |
# Use Textbox for results
|
| 87 |
outputs = []
|
| 88 |
for label in LABELS:
|
| 89 |
+
# NOTE: Changing label slightly to avoid conflict with the key returned by predict_emotion()
|
| 90 |
outputs.append(gr.Textbox(label=f"{label.capitalize()} Emotion Result", lines=1))
|
| 91 |
return outputs
|
| 92 |
|
|
|
|
| 93 |
# Custom wrapper to ensure output matches the order of Gradio components
|
| 94 |
def predict_emotion_gradio(text):
|
| 95 |
raw_results = predict_emotion(text)
|
|
|
|
| 103 |
|
| 104 |
return tuple(ordered_results)
|
| 105 |
|
|
|
|
| 106 |
title = "Multi-Label Emotion Classification with RoBERTa-Large"
|
| 107 |
description = "A DL/GenAI project classifying text into Anger, Fear, Joy, Sadness, and Surprise. The model uses a fine-tuned RoBERTa-Large with 5-Fold CV and dynamic threshold optimization."
|
| 108 |
|
|
|
|
| 112 |
outputs=get_output_components(), # Use the custom function to get ordered components
|
| 113 |
title=title,
|
| 114 |
description=description,
|
| 115 |
+
# The 'allow_flagging' and 'theme' arguments have been removed to fix the TypeErrors.
|
|
|
|
| 116 |
).launch()
|