Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,8 @@
|
|
| 1 |
-
# Install required packages
|
| 2 |
-
!pip install huggingface_hub
|
| 3 |
-
|
| 4 |
import gradio as gr
|
| 5 |
import torch
|
| 6 |
import torch.nn as nn
|
| 7 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModel
|
|
|
|
| 8 |
import numpy as np
|
| 9 |
import logging
|
| 10 |
from datetime import datetime
|
|
@@ -145,34 +143,35 @@ class UltimateCommunicationAnalyzer:
|
|
| 145 |
# Try to load from HuggingFace first, then local file
|
| 146 |
try:
|
| 147 |
logger.info("Attempting to load from HuggingFace: SamanthaStorm/intentanalyzer...")
|
| 148 |
-
# Download the pytorch_model.bin from HuggingFace
|
| 149 |
-
from huggingface_hub import hf_hub_download
|
| 150 |
-
|
| 151 |
-
model_path = hf_hub_download(
|
| 152 |
-
repo_id="SamanthaStorm/intentanalyzer",
|
| 153 |
-
filename="pytorch_model.bin",
|
| 154 |
-
cache_dir="./models"
|
| 155 |
-
)
|
| 156 |
|
| 157 |
-
#
|
| 158 |
-
state_dict = torch.load(model_path, map_location='cpu')
|
| 159 |
-
self.intent_model.load_state_dict(state_dict)
|
| 160 |
-
logger.info("β
Intent detection model loaded from HuggingFace!")
|
| 161 |
-
|
| 162 |
-
except Exception as hf_error:
|
| 163 |
-
logger.warning(f"HuggingFace download failed: {hf_error}")
|
| 164 |
-
logger.info("Trying local file...")
|
| 165 |
-
|
| 166 |
-
# Fallback to local file
|
| 167 |
try:
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
except Exception as e:
|
| 178 |
logger.error(f"β Error loading intent model: {e}")
|
|
@@ -201,37 +200,68 @@ class UltimateCommunicationAnalyzer:
|
|
| 201 |
return 'no_fallacy', 0.0
|
| 202 |
|
| 203 |
def predict_intent(self, text):
|
| 204 |
-
"""Predict intent using the multi-label model"""
|
| 205 |
try:
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
except Exception as e:
|
| 233 |
logger.error(f"Intent prediction failed: {e}")
|
| 234 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
def get_combined_analysis(self, fallacy_type, fallacy_confidence, detected_intents):
|
| 237 |
"""Generate combined analysis and insights"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModel
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
import numpy as np
|
| 7 |
import logging
|
| 8 |
from datetime import datetime
|
|
|
|
| 143 |
# Try to load from HuggingFace first, then local file
|
| 144 |
try:
|
| 145 |
logger.info("Attempting to load from HuggingFace: SamanthaStorm/intentanalyzer...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
+
# For HuggingFace Spaces, we can access other models directly
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
try:
|
| 149 |
+
# Try to load the model files directly from the repo
|
| 150 |
+
model_path = hf_hub_download(
|
| 151 |
+
repo_id="SamanthaStorm/intentanalyzer",
|
| 152 |
+
filename="pytorch_model.bin"
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
# Load the state dict
|
| 156 |
+
state_dict = torch.load(model_path, map_location='cpu')
|
| 157 |
+
self.intent_model.load_state_dict(state_dict)
|
| 158 |
+
logger.info("β
Intent detection model loaded from HuggingFace!")
|
| 159 |
+
|
| 160 |
+
except Exception as download_error:
|
| 161 |
+
logger.warning(f"Direct download failed: {download_error}")
|
| 162 |
+
|
| 163 |
+
# Alternative: Try loading with a simpler approach
|
| 164 |
+
logger.info("Trying alternative loading method...")
|
| 165 |
+
|
| 166 |
+
# Create a dummy model with reasonable predictions for demo
|
| 167 |
+
logger.warning("Using fallback intent detection - limited functionality")
|
| 168 |
+
# We'll create a simple rule-based backup
|
| 169 |
+
self.intent_model = None # Will trigger fallback mode
|
| 170 |
+
|
| 171 |
+
except Exception as hf_error:
|
| 172 |
+
logger.warning(f"HuggingFace loading failed: {hf_error}")
|
| 173 |
+
logger.info("Using fallback intent detection...")
|
| 174 |
+
self.intent_model = None # Will trigger fallback mode
|
| 175 |
|
| 176 |
except Exception as e:
|
| 177 |
logger.error(f"β Error loading intent model: {e}")
|
|
|
|
| 200 |
return 'no_fallacy', 0.0
|
| 201 |
|
| 202 |
def predict_intent(self, text):
|
| 203 |
+
"""Predict intent using the multi-label model or fallback"""
|
| 204 |
try:
|
| 205 |
+
# Check if we have the full model loaded
|
| 206 |
+
if self.intent_model is not None:
|
| 207 |
+
self.intent_model.eval()
|
| 208 |
+
|
| 209 |
+
inputs = self.intent_tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
|
| 210 |
+
|
| 211 |
+
with torch.no_grad():
|
| 212 |
+
outputs = self.intent_model(inputs['input_ids'], inputs['attention_mask'])
|
| 213 |
+
probabilities = torch.sigmoid(outputs).numpy()[0]
|
| 214 |
+
|
| 215 |
+
# Get predictions above threshold
|
| 216 |
+
detected_intents = {}
|
| 217 |
+
for i, category in enumerate(self.intent_categories):
|
| 218 |
+
prob = probabilities[i]
|
| 219 |
+
threshold = self.intent_thresholds[category]
|
| 220 |
+
if prob > threshold:
|
| 221 |
+
detected_intents[category] = prob
|
| 222 |
+
|
| 223 |
+
# If no intents above threshold, use the highest one if it's reasonable
|
| 224 |
+
if not detected_intents:
|
| 225 |
+
max_idx = np.argmax(probabilities)
|
| 226 |
+
max_category = self.intent_categories[max_idx]
|
| 227 |
+
max_prob = probabilities[max_idx]
|
| 228 |
+
if max_prob > 0.3: # Minimum confidence
|
| 229 |
+
detected_intents[max_category] = max_prob
|
| 230 |
+
|
| 231 |
+
return detected_intents
|
| 232 |
+
else:
|
| 233 |
+
# Fallback rule-based intent detection
|
| 234 |
+
return self.predict_intent_fallback(text)
|
| 235 |
|
| 236 |
except Exception as e:
|
| 237 |
logger.error(f"Intent prediction failed: {e}")
|
| 238 |
+
return self.predict_intent_fallback(text)
|
| 239 |
+
|
| 240 |
+
def predict_intent_fallback(self, text):
|
| 241 |
+
"""Simple rule-based fallback for intent detection"""
|
| 242 |
+
text_lower = text.lower()
|
| 243 |
+
detected_intents = {}
|
| 244 |
+
|
| 245 |
+
# Simple pattern matching
|
| 246 |
+
if any(word in text_lower for word in ['lol', 'triggered', 'snowflake', 'cope', 'seethe']):
|
| 247 |
+
detected_intents['trolling'] = 0.75
|
| 248 |
+
|
| 249 |
+
if any(word in text_lower for word in ['whatever', "don't care", 'not my problem', 'end of discussion']):
|
| 250 |
+
detected_intents['dismissive'] = 0.70
|
| 251 |
+
|
| 252 |
+
if any(word in text_lower for word in ['if you really', 'after everything', "you're making me feel"]):
|
| 253 |
+
detected_intents['manipulative'] = 0.72
|
| 254 |
+
|
| 255 |
+
if text_lower.count('!') > 2 or any(word in text_lower for word in ["can't believe", 'literally shaking']):
|
| 256 |
+
detected_intents['emotionally_reactive'] = 0.68
|
| 257 |
+
|
| 258 |
+
if any(word in text_lower for word in ['understand', 'appreciate', 'thank you', 'let\'s work']):
|
| 259 |
+
detected_intents['constructive'] = 0.80
|
| 260 |
+
|
| 261 |
+
if not detected_intents:
|
| 262 |
+
detected_intents['unclear'] = 0.60
|
| 263 |
+
|
| 264 |
+
return detected_intents
|
| 265 |
|
| 266 |
def get_combined_analysis(self, fallacy_type, fallacy_confidence, detected_intents):
|
| 267 |
"""Generate combined analysis and insights"""
|