Daizzyy commited on
Commit
7edacbb
·
verified ·
1 Parent(s): 1b3d7c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -55
app.py CHANGED
@@ -1,69 +1,62 @@
1
  import gradio as gr
2
  import joblib
3
- from huggingface_hub import hf_hub_download
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")
32
- except Exception as e:
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")
64
- except Exception as e:
65
- print(f"⚠️ Label encoder not found (optional): {e}")
66
- label_encoder = None
 
 
 
 
 
 
 
 
 
67
 
68
  return {
69
  "model": model,
@@ -72,7 +65,7 @@ def load_model():
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
@@ -96,7 +89,7 @@ def predict(text):
96
  try:
97
  # Check if models are loaded
98
  if model_components is None:
99
- return "<div class='warn'>❌ Model not loaded. Please check server logs.</div>"
100
 
101
  model = model_components["model"]
102
  vectorizer = model_components["vectorizer"]
 
1
  import gradio as gr
2
  import joblib
 
3
  import os
4
 
5
 
 
6
  def load_model():
7
+ """Load model from local files in the Space"""
8
  try:
9
+ print("Loading model from local files...")
10
 
11
+ print("\nFiles in current directory:")
12
+ for f in os.listdir("."):
13
+ if f.endswith((".jobilib", ".pkl", ".txt")):
14
+ print(f" - {f}")
15
 
16
+
17
+ model_files = [
18
+ "tfidf_logreg_best.jobilib",
19
+ "model.jobilib",
20
+ "model.pkl"
21
+ ]
22
 
23
+ model = None
24
+ for mf in model_files:
25
+ if os.path.exists(mf):
26
+ print(f"\nLoading model: {mf}")
27
+ model = joblib.load(mf)
28
+ print(f"✅ Model loaded from {mf}")
29
+ break
30
+
31
+ if model is None:
32
+ print("❌ No model file found!")
 
 
 
 
33
  return None
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ vectorizer_files = [
37
+ "vocab.txt",
38
+ "vocab",
39
+ "vectorizer.jobilib",
40
+ "tfidf.jobilib"
41
+ ]
42
+
43
+ vectorizer = None
44
+ for vf in vectorizer_files:
45
+ if os.path.exists(vf):
46
+ print(f"Loading vectorizer: {vf}")
47
+ vectorizer = joblib.load(vf)
48
+ print(f"✅ Vectorizer loaded from {vf}")
49
+ break
50
+
51
+ if vectorizer is None:
52
+ print("⚠️ Vectorizer not found")
53
+
54
+ # Try to load label encoder
55
+ label_encoder = None
56
+ if os.path.exists("label_encoder.jobilib"):
57
+ print("Loading label encoder...")
58
+ label_encoder = joblib.load("label_encoder.jobilib")
59
+ print("✅ Label encoder loaded")
60
 
61
  return {
62
  "model": model,
 
65
  }
66
 
67
  except Exception as e:
68
+ print(f"❌ Error loading model: {str(e)}")
69
  import traceback
70
  print(traceback.format_exc())
71
  return None
 
89
  try:
90
  # Check if models are loaded
91
  if model_components is None:
92
+ return "<div class='warn'>❌ Model not loaded. Check logs.</div>"
93
 
94
  model = model_components["model"]
95
  vectorizer = model_components["vectorizer"]