Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
@@ -14,7 +14,6 @@ from transformers import BertTokenizer, BertForSequenceClassification
|
|
14 |
import os
|
15 |
|
16 |
def load_model():
|
17 |
-
# Use BertForSequenceClassification instead of AutoModelForSequenceClassification
|
18 |
model_name = "indobenchmark/indobert-lite-large-p2"
|
19 |
model = BertForSequenceClassification.from_pretrained(
|
20 |
model_name,
|
@@ -23,18 +22,16 @@ def load_model():
|
|
23 |
ignore_mismatched_sizes=True
|
24 |
)
|
25 |
|
26 |
-
# Try to load local weights if available
|
27 |
try:
|
28 |
-
|
29 |
-
local_model_path = "model.safetensors"
|
30 |
if os.path.exists(local_model_path):
|
31 |
weights = torch.load(local_model_path)
|
32 |
model.load_state_dict(weights)
|
|
|
33 |
else:
|
34 |
-
print("
|
35 |
except Exception as e:
|
36 |
-
print(f"Error loading local weights: {e}")
|
37 |
-
print("Falling back to pre-trained model.")
|
38 |
|
39 |
model.eval()
|
40 |
return model
|
@@ -49,13 +46,25 @@ def predict_stress_with_accuracy(text_input):
|
|
49 |
if not text_input.strip():
|
50 |
return None, None, None, None
|
51 |
|
|
|
|
|
|
|
52 |
# Tokenize the input text
|
53 |
inputs = tokenizer(text_input, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
# Get the model's output
|
56 |
with torch.no_grad():
|
57 |
output = model(**inputs)
|
58 |
|
|
|
|
|
|
|
|
|
59 |
# Apply softmax to get probabilities
|
60 |
probabilities = F.softmax(output.logits, dim=1)
|
61 |
|
@@ -63,6 +72,12 @@ def predict_stress_with_accuracy(text_input):
|
|
63 |
probs = probabilities[0].tolist()
|
64 |
confidence_scores = [round(p * 100, 1) for p in probs]
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
# Get main prediction
|
67 |
predicted_class = torch.argmax(probabilities, dim=1).item()
|
68 |
main_confidence = confidence_scores[predicted_class]
|
@@ -71,7 +86,9 @@ def predict_stress_with_accuracy(text_input):
|
|
71 |
stress_levels = {0: "Neutral", 1: "Mild Stress", 2: "Very Stress"}
|
72 |
prediction = stress_levels[predicted_class]
|
73 |
|
74 |
-
|
|
|
|
|
75 |
result_html = f"""
|
76 |
<div class="result-card">
|
77 |
<div class="prediction-text">{prediction}</div>
|
@@ -82,7 +99,6 @@ def predict_stress_with_accuracy(text_input):
|
|
82 |
</div>
|
83 |
"""
|
84 |
|
85 |
-
# Generate HTML for detailed analysis
|
86 |
detailed_html = f"""
|
87 |
<div class="detailed-analysis">
|
88 |
<div class="analysis-title">Detailed Analysis</div>
|
|
|
14 |
import os
|
15 |
|
16 |
def load_model():
|
|
|
17 |
model_name = "indobenchmark/indobert-lite-large-p2"
|
18 |
model = BertForSequenceClassification.from_pretrained(
|
19 |
model_name,
|
|
|
22 |
ignore_mismatched_sizes=True
|
23 |
)
|
24 |
|
|
|
25 |
try:
|
26 |
+
local_model_path = "model.pt" # Changed from .safetensors
|
|
|
27 |
if os.path.exists(local_model_path):
|
28 |
weights = torch.load(local_model_path)
|
29 |
model.load_state_dict(weights)
|
30 |
+
print("β
Local model weights loaded successfully!")
|
31 |
else:
|
32 |
+
print("β No local model weights found. Using pre-trained weights.")
|
33 |
except Exception as e:
|
34 |
+
print(f"β Error loading local weights: {e}")
|
|
|
35 |
|
36 |
model.eval()
|
37 |
return model
|
|
|
46 |
if not text_input.strip():
|
47 |
return None, None, None, None
|
48 |
|
49 |
+
# Print input for debugging
|
50 |
+
print(f"π Input Text: '{text_input}'")
|
51 |
+
|
52 |
# Tokenize the input text
|
53 |
inputs = tokenizer(text_input, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
54 |
+
|
55 |
+
# Print tokenization details
|
56 |
+
print("π Tokenization Details:")
|
57 |
+
print(f" Input IDs: {inputs['input_ids']}")
|
58 |
+
print(f" Attention Mask: {inputs['attention_mask']}")
|
59 |
|
60 |
# Get the model's output
|
61 |
with torch.no_grad():
|
62 |
output = model(**inputs)
|
63 |
|
64 |
+
# Print raw logits
|
65 |
+
print("π Raw Logits:")
|
66 |
+
print(output.logits)
|
67 |
+
|
68 |
# Apply softmax to get probabilities
|
69 |
probabilities = F.softmax(output.logits, dim=1)
|
70 |
|
|
|
72 |
probs = probabilities[0].tolist()
|
73 |
confidence_scores = [round(p * 100, 1) for p in probs]
|
74 |
|
75 |
+
# Print probabilities
|
76 |
+
print("π Class Probabilities:")
|
77 |
+
print(f" Neutral: {confidence_scores[0]}%")
|
78 |
+
print(f" Mild Stress: {confidence_scores[1]}%")
|
79 |
+
print(f" Very Stress: {confidence_scores[2]}%")
|
80 |
+
|
81 |
# Get main prediction
|
82 |
predicted_class = torch.argmax(probabilities, dim=1).item()
|
83 |
main_confidence = confidence_scores[predicted_class]
|
|
|
86 |
stress_levels = {0: "Neutral", 1: "Mild Stress", 2: "Very Stress"}
|
87 |
prediction = stress_levels[predicted_class]
|
88 |
|
89 |
+
print(f"π Predicted Class: {prediction} ({main_confidence}% confident)")
|
90 |
+
|
91 |
+
# Rest of the HTML generation code remains the same...
|
92 |
result_html = f"""
|
93 |
<div class="result-card">
|
94 |
<div class="prediction-text">{prediction}</div>
|
|
|
99 |
</div>
|
100 |
"""
|
101 |
|
|
|
102 |
detailed_html = f"""
|
103 |
<div class="detailed-analysis">
|
104 |
<div class="analysis-title">Detailed Analysis</div>
|