Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
|
@@ -139,15 +142,37 @@ class UltimateCommunicationAnalyzer:
|
|
| 139 |
# Load custom intent model
|
| 140 |
self.intent_model = MultiLabelIntentClassifier("distilbert-base-uncased", 6)
|
| 141 |
|
| 142 |
-
# Try to load
|
| 143 |
try:
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
except Exception as e:
|
| 153 |
logger.error(f"β Error loading intent model: {e}")
|
|
@@ -553,7 +578,8 @@ if __name__ == "__main__":
|
|
| 553 |
except Exception as e:
|
| 554 |
logger.error(f"β Failed to launch app: {e}")
|
| 555 |
print(f"Error: {e}")
|
| 556 |
-
print("\nMake sure both
|
| 557 |
-
print("1. Fallacy model:
|
| 558 |
-
print("2. Intent model:
|
|
|
|
| 559 |
raise
|
|
|
|
| 1 |
+
# Install required packages
|
| 2 |
+
!pip install huggingface_hub
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
import torch
|
| 6 |
import torch.nn as nn
|
|
|
|
| 142 |
# Load custom intent model
|
| 143 |
self.intent_model = MultiLabelIntentClassifier("distilbert-base-uncased", 6)
|
| 144 |
|
| 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 |
+
# Load the state dict
|
| 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 |
+
checkpoint = torch.load('intent_detection_model.pth', map_location='cpu')
|
| 169 |
+
self.intent_model.load_state_dict(checkpoint['model_state_dict'])
|
| 170 |
+
logger.info("β
Intent detection model loaded from local file!")
|
| 171 |
+
except FileNotFoundError:
|
| 172 |
+
logger.error("β Neither HuggingFace nor local model found!")
|
| 173 |
+
raise Exception("Intent model not found. Please either:")
|
| 174 |
+
print("1. Ensure 'intent_detection_model.pth' exists locally, OR")
|
| 175 |
+
print("2. Make sure SamanthaStorm/intentanalyzer is properly uploaded to HuggingFace")
|
| 176 |
|
| 177 |
except Exception as e:
|
| 178 |
logger.error(f"β Error loading intent model: {e}")
|
|
|
|
| 578 |
except Exception as e:
|
| 579 |
logger.error(f"β Failed to launch app: {e}")
|
| 580 |
print(f"Error: {e}")
|
| 581 |
+
print("\nMake sure both models are available:")
|
| 582 |
+
print("1. Fallacy model: SamanthaStorm/fallacyfinder (auto-downloaded)")
|
| 583 |
+
print("2. Intent model: SamanthaStorm/intentanalyzer (auto-downloaded)")
|
| 584 |
+
print("3. Or ensure 'intent_detection_model.pth' exists locally")
|
| 585 |
raise
|