Update app.py
Browse files
app.py
CHANGED
|
@@ -4,25 +4,28 @@ from huggingface_hub import hf_hub_download
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
|
|
|
|
| 7 |
def load_model():
|
| 8 |
"""Download and load model from HF Hub"""
|
| 9 |
try:
|
| 10 |
print("Loading model from Hugging Face Hub...")
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
HF_MODEL = "cyberbullying-model"
|
| 16 |
repo_id = f"{HF_USERNAME}/{HF_MODEL}"
|
| 17 |
|
| 18 |
print(f"Downloading from: {repo_id}")
|
| 19 |
|
| 20 |
-
|
|
|
|
| 21 |
try:
|
| 22 |
model_file = hf_hub_download(
|
| 23 |
repo_id=repo_id,
|
| 24 |
filename="tfidf_logreg_best.jobilib",
|
| 25 |
-
cache_dir=".cache"
|
|
|
|
|
|
|
| 26 |
)
|
| 27 |
model = joblib.load(model_file)
|
| 28 |
print(f"✅ Model loaded successfully")
|
|
@@ -30,23 +33,31 @@ def load_model():
|
|
| 30 |
print(f"❌ Error loading model: {e}")
|
| 31 |
return None
|
| 32 |
|
|
|
|
|
|
|
| 33 |
try:
|
| 34 |
vectorizer_file = hf_hub_download(
|
| 35 |
repo_id=repo_id,
|
| 36 |
filename="vocab.txt",
|
| 37 |
-
cache_dir=".cache"
|
|
|
|
|
|
|
| 38 |
)
|
| 39 |
vectorizer = joblib.load(vectorizer_file)
|
| 40 |
print(f"✅ Vectorizer loaded successfully")
|
| 41 |
except Exception as e:
|
| 42 |
-
print(f"
|
| 43 |
vectorizer = None
|
| 44 |
|
|
|
|
|
|
|
| 45 |
try:
|
| 46 |
label_encoder_file = hf_hub_download(
|
| 47 |
repo_id=repo_id,
|
| 48 |
filename="label_encoder.jobilib",
|
| 49 |
-
cache_dir=".cache"
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
label_encoder = joblib.load(label_encoder_file)
|
| 52 |
print(f"✅ Label encoder loaded successfully")
|
|
@@ -61,7 +72,7 @@ def load_model():
|
|
| 61 |
}
|
| 62 |
|
| 63 |
except Exception as e:
|
| 64 |
-
print(f"❌ Critical error
|
| 65 |
import traceback
|
| 66 |
print(traceback.format_exc())
|
| 67 |
return None
|
|
@@ -119,7 +130,7 @@ def predict(text):
|
|
| 119 |
|
| 120 |
print(f"Prediction: {label}, Score: {score:.4f}")
|
| 121 |
|
| 122 |
-
# Category definitions
|
| 123 |
cyberbullying_types = {
|
| 124 |
"age": {"emoji": "👶", "color": "#ff6b6b", "text": "Age-Based Cyberbullying"},
|
| 125 |
"gender": {"emoji": "⚥️", "color": "#ff8c42", "text": "Gender-Based Cyberbullying"},
|
|
@@ -129,7 +140,7 @@ def predict(text):
|
|
| 129 |
"not_cyberbullying": {"emoji": "✅", "color": "#00ff64", "text": "Safe Message"}
|
| 130 |
}
|
| 131 |
|
| 132 |
-
# Get category info
|
| 133 |
label_lower = str(label).lower().strip()
|
| 134 |
category = cyberbullying_types.get(
|
| 135 |
label_lower,
|
|
@@ -171,7 +182,7 @@ def predict(text):
|
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
-
with gr.Blocks(
|
| 175 |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"/>
|
| 176 |
|
| 177 |
* {
|
|
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
|
| 7 |
+
|
| 8 |
def load_model():
|
| 9 |
"""Download and load model from HF Hub"""
|
| 10 |
try:
|
| 11 |
print("Loading model from Hugging Face Hub...")
|
| 12 |
|
| 13 |
+
# Your HF settings
|
| 14 |
+
HF_USERNAME = "Daizzyy"
|
| 15 |
+
HF_MODEL = "cyberbullying-model"
|
|
|
|
| 16 |
repo_id = f"{HF_USERNAME}/{HF_MODEL}"
|
| 17 |
|
| 18 |
print(f"Downloading from: {repo_id}")
|
| 19 |
|
| 20 |
+
# Download model file
|
| 21 |
+
print("Downloading: tfidf_logreg_best.jobilib")
|
| 22 |
try:
|
| 23 |
model_file = hf_hub_download(
|
| 24 |
repo_id=repo_id,
|
| 25 |
filename="tfidf_logreg_best.jobilib",
|
| 26 |
+
cache_dir=".cache",
|
| 27 |
+
force_download=True,
|
| 28 |
+
resume_download=True
|
| 29 |
)
|
| 30 |
model = joblib.load(model_file)
|
| 31 |
print(f"✅ Model loaded successfully")
|
|
|
|
| 33 |
print(f"❌ Error loading model: {e}")
|
| 34 |
return None
|
| 35 |
|
| 36 |
+
# Download vectorizer (vocab.txt)
|
| 37 |
+
print("Downloading: vocab.txt")
|
| 38 |
try:
|
| 39 |
vectorizer_file = hf_hub_download(
|
| 40 |
repo_id=repo_id,
|
| 41 |
filename="vocab.txt",
|
| 42 |
+
cache_dir=".cache",
|
| 43 |
+
force_download=True,
|
| 44 |
+
resume_download=True
|
| 45 |
)
|
| 46 |
vectorizer = joblib.load(vectorizer_file)
|
| 47 |
print(f"✅ Vectorizer loaded successfully")
|
| 48 |
except Exception as e:
|
| 49 |
+
print(f"⚠️ Error loading vectorizer: {e}")
|
| 50 |
vectorizer = None
|
| 51 |
|
| 52 |
+
# Download label encoder (optional)
|
| 53 |
+
print("Downloading: label_encoder.jobilib")
|
| 54 |
try:
|
| 55 |
label_encoder_file = hf_hub_download(
|
| 56 |
repo_id=repo_id,
|
| 57 |
filename="label_encoder.jobilib",
|
| 58 |
+
cache_dir=".cache",
|
| 59 |
+
force_download=True,
|
| 60 |
+
resume_download=True
|
| 61 |
)
|
| 62 |
label_encoder = joblib.load(label_encoder_file)
|
| 63 |
print(f"✅ Label encoder loaded successfully")
|
|
|
|
| 72 |
}
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
+
print(f"❌ Critical error: {str(e)}")
|
| 76 |
import traceback
|
| 77 |
print(traceback.format_exc())
|
| 78 |
return None
|
|
|
|
| 130 |
|
| 131 |
print(f"Prediction: {label}, Score: {score:.4f}")
|
| 132 |
|
| 133 |
+
# Category definitions
|
| 134 |
cyberbullying_types = {
|
| 135 |
"age": {"emoji": "👶", "color": "#ff6b6b", "text": "Age-Based Cyberbullying"},
|
| 136 |
"gender": {"emoji": "⚥️", "color": "#ff8c42", "text": "Gender-Based Cyberbullying"},
|
|
|
|
| 140 |
"not_cyberbullying": {"emoji": "✅", "color": "#00ff64", "text": "Safe Message"}
|
| 141 |
}
|
| 142 |
|
| 143 |
+
# Get category info
|
| 144 |
label_lower = str(label).lower().strip()
|
| 145 |
category = cyberbullying_types.get(
|
| 146 |
label_lower,
|
|
|
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
+
with gr.Blocks(css="""
|
| 186 |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"/>
|
| 187 |
|
| 188 |
* {
|