Spaces:
Build error
Build error
Commit ·
7384288
1
Parent(s): 680c2d5
v.1.21
Browse files
app.py
CHANGED
|
@@ -80,6 +80,29 @@ class EventDetector:
|
|
| 80 |
logger.error(f"Error in EventDetector initialization: {e}")
|
| 81 |
raise
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
@spaces.GPU(duration=30)
|
| 84 |
def initialize_models(self):
|
| 85 |
if self.initialized:
|
|
@@ -144,17 +167,56 @@ class EventDetector:
|
|
| 144 |
self.cleanup()
|
| 145 |
raise
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
try:
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
except Exception as e:
|
| 157 |
-
logger.error(f"
|
|
|
|
| 158 |
|
| 159 |
@spaces.GPU(duration=20)
|
| 160 |
def detect_events(self, text, entity):
|
|
@@ -211,83 +273,17 @@ class EventDetector:
|
|
| 211 |
logger.error(f"Event detection error: {str(e)}")
|
| 212 |
return "Нет", f"Error: {str(e)}"
|
| 213 |
|
| 214 |
-
|
| 215 |
-
|
| 216 |
try:
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
time.sleep(2)
|
| 224 |
-
|
| 225 |
-
truncated_text = text[:500]
|
| 226 |
-
results = []
|
| 227 |
-
|
| 228 |
-
try:
|
| 229 |
-
inputs = [truncated_text]
|
| 230 |
-
sentiment_results = []
|
| 231 |
-
|
| 232 |
-
# Process each model separately with delay
|
| 233 |
-
if self.finbert:
|
| 234 |
-
finbert_result = self.finbert(inputs, truncation=True, max_length=512)[0]
|
| 235 |
-
results.append(self.get_sentiment_label(finbert_result))
|
| 236 |
-
time.sleep(0.5)
|
| 237 |
-
|
| 238 |
-
if self.roberta:
|
| 239 |
-
roberta_result = self.roberta(inputs, truncation=True, max_length=512)[0]
|
| 240 |
-
results.append(self.get_sentiment_label(roberta_result))
|
| 241 |
-
time.sleep(0.5)
|
| 242 |
-
|
| 243 |
-
if self.finbert_tone:
|
| 244 |
-
finbert_tone_result = self.finbert_tone(inputs, truncation=True, max_length=512)[0]
|
| 245 |
-
results.append(self.get_sentiment_label(finbert_tone_result))
|
| 246 |
-
|
| 247 |
-
# Get majority vote
|
| 248 |
-
if results:
|
| 249 |
-
sentiment_counts = pd.Series(results).value_counts()
|
| 250 |
-
final_sentiment = sentiment_counts.index[0] if sentiment_counts.iloc[0] >= 2 else "Neutral"
|
| 251 |
-
else:
|
| 252 |
-
final_sentiment = "Neutral"
|
| 253 |
-
|
| 254 |
-
self.last_gpu_use = time.time()
|
| 255 |
-
return final_sentiment
|
| 256 |
-
|
| 257 |
-
except Exception as e:
|
| 258 |
-
logger.error(f"Model inference error: {e}")
|
| 259 |
-
return "Neutral"
|
| 260 |
-
|
| 261 |
except Exception as e:
|
| 262 |
-
logger.error(f"
|
| 263 |
-
return "Neutral"
|
| 264 |
-
|
| 265 |
-
def create_visualizations(df):
|
| 266 |
-
if df is None or df.empty:
|
| 267 |
-
return None, None
|
| 268 |
-
|
| 269 |
-
try:
|
| 270 |
-
sentiments = df['Sentiment'].value_counts()
|
| 271 |
-
fig_sentiment = go.Figure(data=[go.Pie(
|
| 272 |
-
labels=sentiments.index,
|
| 273 |
-
values=sentiments.values,
|
| 274 |
-
marker_colors=['#FF6B6B', '#4ECDC4', '#95A5A6']
|
| 275 |
-
)])
|
| 276 |
-
fig_sentiment.update_layout(title="Распределение тональности")
|
| 277 |
-
|
| 278 |
-
events = df['Event_Type'].value_counts()
|
| 279 |
-
fig_events = go.Figure(data=[go.Bar(
|
| 280 |
-
x=events.index,
|
| 281 |
-
y=events.values,
|
| 282 |
-
marker_color='#2196F3'
|
| 283 |
-
)])
|
| 284 |
-
fig_events.update_layout(title="Распределение событий")
|
| 285 |
-
|
| 286 |
-
return fig_sentiment, fig_events
|
| 287 |
-
|
| 288 |
-
except Exception as e:
|
| 289 |
-
logger.error(f"Visualization error: {e}")
|
| 290 |
-
return None, None
|
| 291 |
|
| 292 |
@spaces.GPU
|
| 293 |
def process_file(file_obj):
|
|
@@ -399,7 +395,7 @@ def create_interface():
|
|
| 399 |
control = ProcessControl()
|
| 400 |
|
| 401 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 402 |
-
gr.Markdown("# AI-анализ мониторинга новостей v.1.
|
| 403 |
|
| 404 |
with gr.Row():
|
| 405 |
file_input = gr.File(
|
|
|
|
| 80 |
logger.error(f"Error in EventDetector initialization: {e}")
|
| 81 |
raise
|
| 82 |
|
| 83 |
+
def get_sentiment_label(self, result):
|
| 84 |
+
"""
|
| 85 |
+
Convert model output to standardized sentiment label
|
| 86 |
+
"""
|
| 87 |
+
try:
|
| 88 |
+
# Handle different model output formats
|
| 89 |
+
if isinstance(result, dict):
|
| 90 |
+
label = result.get('label', '').lower()
|
| 91 |
+
else:
|
| 92 |
+
return "Neutral"
|
| 93 |
+
|
| 94 |
+
# Map different model outputs to standard labels
|
| 95 |
+
if label in ['positive', 'pos', 'positive tone']:
|
| 96 |
+
return "Positive"
|
| 97 |
+
elif label in ['negative', 'neg', 'negative tone']:
|
| 98 |
+
return "Negative"
|
| 99 |
+
else:
|
| 100 |
+
return "Neutral"
|
| 101 |
+
|
| 102 |
+
except Exception as e:
|
| 103 |
+
logger.error(f"Error in get_sentiment_label: {e}")
|
| 104 |
+
return "Neutral"
|
| 105 |
+
|
| 106 |
@spaces.GPU(duration=30)
|
| 107 |
def initialize_models(self):
|
| 108 |
if self.initialized:
|
|
|
|
| 167 |
self.cleanup()
|
| 168 |
raise
|
| 169 |
|
| 170 |
+
@spaces.GPU(duration=20)
|
| 171 |
+
def analyze_sentiment(self, text):
|
| 172 |
try:
|
| 173 |
+
if not self.initialized:
|
| 174 |
+
if not self.initialize_models():
|
| 175 |
+
return "Neutral"
|
| 176 |
+
|
| 177 |
+
current_time = time.time()
|
| 178 |
+
if current_time - self.last_gpu_use < 2:
|
| 179 |
+
time.sleep(2)
|
| 180 |
+
|
| 181 |
+
truncated_text = text[:500]
|
| 182 |
+
results = []
|
| 183 |
+
|
| 184 |
+
try:
|
| 185 |
+
inputs = [truncated_text]
|
| 186 |
+
sentiment_results = []
|
| 187 |
+
|
| 188 |
+
# Process each model separately with delay
|
| 189 |
+
if self.finbert:
|
| 190 |
+
finbert_result = self.finbert(inputs, truncation=True, max_length=512)[0]
|
| 191 |
+
results.append(self.get_sentiment_label(finbert_result))
|
| 192 |
+
time.sleep(0.5)
|
| 193 |
+
|
| 194 |
+
if self.roberta:
|
| 195 |
+
roberta_result = self.roberta(inputs, truncation=True, max_length=512)[0]
|
| 196 |
+
results.append(self.get_sentiment_label(roberta_result))
|
| 197 |
+
time.sleep(0.5)
|
| 198 |
+
|
| 199 |
+
if self.finbert_tone:
|
| 200 |
+
finbert_tone_result = self.finbert_tone(inputs, truncation=True, max_length=512)[0]
|
| 201 |
+
results.append(self.get_sentiment_label(finbert_tone_result))
|
| 202 |
+
|
| 203 |
+
# Get majority vote
|
| 204 |
+
if results:
|
| 205 |
+
sentiment_counts = pd.Series(results).value_counts()
|
| 206 |
+
final_sentiment = sentiment_counts.index[0] if sentiment_counts.iloc[0] >= 2 else "Neutral"
|
| 207 |
+
else:
|
| 208 |
+
final_sentiment = "Neutral"
|
| 209 |
+
|
| 210 |
+
self.last_gpu_use = time.time()
|
| 211 |
+
return final_sentiment
|
| 212 |
+
|
| 213 |
+
except Exception as e:
|
| 214 |
+
logger.error(f"Model inference error: {e}")
|
| 215 |
+
return "Neutral"
|
| 216 |
+
|
| 217 |
except Exception as e:
|
| 218 |
+
logger.error(f"Sentiment analysis error: {e}")
|
| 219 |
+
return "Neutral"
|
| 220 |
|
| 221 |
@spaces.GPU(duration=20)
|
| 222 |
def detect_events(self, text, entity):
|
|
|
|
| 273 |
logger.error(f"Event detection error: {str(e)}")
|
| 274 |
return "Нет", f"Error: {str(e)}"
|
| 275 |
|
| 276 |
+
def cleanup(self):
|
| 277 |
+
"""Clean up GPU resources"""
|
| 278 |
try:
|
| 279 |
+
self.model = None
|
| 280 |
+
self.finbert = None
|
| 281 |
+
self.roberta = None
|
| 282 |
+
self.finbert_tone = None
|
| 283 |
+
torch.cuda.empty_cache()
|
| 284 |
+
self.initialized = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
except Exception as e:
|
| 286 |
+
logger.error(f"Error in cleanup: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
|
| 288 |
@spaces.GPU
|
| 289 |
def process_file(file_obj):
|
|
|
|
| 395 |
control = ProcessControl()
|
| 396 |
|
| 397 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 398 |
+
gr.Markdown("# AI-анализ мониторинга новостей v.1.21")
|
| 399 |
|
| 400 |
with gr.Row():
|
| 401 |
file_input = gr.File(
|