pentarosarium commited on
Commit
3dc9f8a
·
1 Parent(s): a237f7d
Files changed (2) hide show
  1. app.py +54 -136
  2. requirements.txt +4 -2
app.py CHANGED
@@ -286,158 +286,80 @@ class EventDetector:
286
  def initialize_models(self, device):
287
  """Initialize all models with GPU support"""
288
  try:
289
- # Force device to CUDA if available
290
- if torch.cuda.is_available():
291
- device = "cuda"
292
- logger.info(f"Using CUDA: {torch.cuda.get_device_name(0)}")
293
-
294
- # === REPLACEMENT FOR HELSINKI-NLP USING M2M100 (SMALLER MODEL) ===
295
- logger.info("replacing Helsinki-NLP with M2M100 (smaller model)")
296
- from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
297
-
298
- # Use a much smaller model with 418M parameters (vs 2.46G)
299
- model_name = "facebook/m2m100_418M"
300
-
301
- # Load tokenizer and model with explicit steps
302
- self.translator_tokenizer = M2M100Tokenizer.from_pretrained(model_name)
303
-
304
- # Most careful loading to avoid meta tensor errors
305
- self.translator_model = M2M100ForConditionalGeneration.from_pretrained(
306
- model_name,
307
- torch_dtype=torch.float16, # Use half precision
308
- low_cpu_mem_usage=True # More memory efficient loading
309
  )
310
 
311
- # Explicitly move to CUDA after loading
312
- self.translator_model = self.translator_model.to(device)
313
-
314
- # Custom translation functions
315
- def translate_ru_en(text_list):
316
- """Function that mimics the Helsinki-NLP translator pipeline API"""
317
- if not isinstance(text_list, list):
318
- text_list = [text_list]
319
 
320
- results = []
321
- for text in text_list:
322
- if not text or not isinstance(text, str):
323
- results.append({"translation_text": ""})
324
- continue
325
-
326
- try:
327
- # Explicitly set source and target languages
328
- self.translator_tokenizer.src_lang = "ru"
329
- self.translator_tokenizer.tgt_lang = "en"
330
-
331
- # Tokenize
332
- encoded = self.translator_tokenizer(text.strip(), return_tensors="pt")
333
-
334
- # Manually move to device
335
- encoded = {k: v.to(device) for k, v in encoded.items()}
336
-
337
- # Generate with careful error handling
338
- with torch.no_grad():
339
- output = self.translator_model.generate(**encoded, max_length=512, num_beams=2)
340
-
341
- # Decode
342
- decoded = self.translator_tokenizer.batch_decode(output, skip_special_tokens=True)
343
- translation = decoded[0] if decoded else ""
344
-
345
- results.append({"translation_text": translation})
346
- except Exception as e:
347
- logger.error(f"Translation error: {str(e)}")
348
- results.append({"translation_text": f"Translation error: {str(e)}"})
349
-
350
- return results
351
 
352
- def translate_en_ru(text_list):
353
- """Function that mimics the Helsinki-NLP translator pipeline API for EN-RU"""
354
- if not isinstance(text_list, list):
355
- text_list = [text_list]
356
-
357
- results = []
358
- for text in text_list:
359
- if not text or not isinstance(text, str):
360
- results.append({"translation_text": ""})
361
- continue
362
-
363
- try:
364
- # Explicitly set source and target languages
365
- self.translator_tokenizer.src_lang = "en"
366
- self.translator_tokenizer.tgt_lang = "ru"
367
-
368
- # Tokenize
369
- encoded = self.translator_tokenizer(text.strip(), return_tensors="pt")
370
-
371
- # Manually move to device
372
- encoded = {k: v.to(device) for k, v in encoded.items()}
373
-
374
- # Generate with careful error handling
375
- with torch.no_grad():
376
- output = self.translator_model.generate(**encoded, max_length=512, num_beams=2)
377
-
378
- # Decode
379
- decoded = self.translator_tokenizer.batch_decode(output, skip_special_tokens=True)
380
- translation = decoded[0] if decoded else ""
381
-
382
- results.append({"translation_text": translation})
383
- except Exception as e:
384
- logger.error(f"Translation error: {str(e)}")
385
- results.append({"translation_text": f"Translation error: {str(e)}"})
386
-
387
- return results
388
 
389
- # Set up the replacement pipelines
390
- self.translator = translate_ru_en
391
- self.rutranslator = translate_en_ru
 
 
 
392
 
393
- # === CONTINUE WITH ORIGINAL CODE FOR OTHER MODELS ===
394
- # But add safetensors parameter to all model loading
395
- from transformers import AutoModelForSequenceClassification
396
 
397
- # For sentiment models, use direct model loading instead of pipeline
398
- self.finbert_tokenizer = AutoTokenizer.from_pretrained("ProsusAI/finbert")
399
- self.finbert_model = AutoModelForSequenceClassification.from_pretrained(
400
- "ProsusAI/finbert",
401
  use_safetensors=True,
402
- torch_dtype=torch.float16,
403
- low_cpu_mem_usage=True
404
- ).to(device)
405
 
406
- # Create custom sentiment function
407
- def analyze_finbert(text):
408
- inputs = self.finbert_tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
409
- inputs = {k: v.to(device) for k, v in inputs.items()}
410
-
411
- with torch.no_grad():
412
- outputs = self.finbert_model(**inputs)
413
-
414
- probs = torch.nn.functional.softmax(outputs.logits, dim=1)
415
- pred_class = torch.argmax(probs, dim=1).item()
416
-
417
- # Map to expected format
418
  labels = ["negative", "neutral", "positive"]
419
- return [{"label": labels[pred_class], "score": probs[0][pred_class].item()}]
420
 
421
- # Replace pipelines with custom functions
422
- self.finbert = analyze_finbert
423
 
424
- # Do the same for the other sentiment models...
425
- # (Add similar custom implementations)
426
 
427
- # Initialize MT5 model with careful loading
428
  self.model_name = "google/mt5-small"
429
  self.tokenizer = AutoTokenizer.from_pretrained(
430
  self.model_name,
431
  legacy=True
432
  )
433
  self.model = AutoModelForSeq2SeqLM.from_pretrained(
434
- self.model_name,
435
  use_safetensors=True,
436
- torch_dtype=torch.float16,
437
- low_cpu_mem_usage=True
438
- ).to(device)
439
 
440
- # Initialize Groq
441
  if 'groq_key':
442
  self.groq = ChatOpenAI(
443
  base_url="https://api.groq.com/openai/v1",
@@ -449,10 +371,6 @@ class EventDetector:
449
  logger.warning("Groq API key not found, impact estimation will be limited")
450
  self.groq = None
451
 
452
- self.device = device
453
- self.initialized = True
454
- logger.info("All models initialized successfully!")
455
-
456
  except Exception as e:
457
  logger.error(f"Error in model initialization: {str(e)}")
458
  raise
@@ -1141,7 +1059,7 @@ def create_interface():
1141
  control = ProcessControl()
1142
 
1143
  with gr.Blocks(analytics_enabled=False) as app:
1144
- gr.Markdown("# AI-анализ мониторинга новостей v.3.1 + forced cuda")
1145
 
1146
  with gr.Row():
1147
  file_input = gr.File(
 
286
  def initialize_models(self, device):
287
  """Initialize all models with GPU support"""
288
  try:
289
+ # Initialize translation model with safetensors format
290
+ from transformers import AutoModelForSeq2SeqLM, MarianTokenizer
291
+
292
+ # Direct model loading with safetensors
293
+ self.translator_tokenizer = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-ru-en")
294
+ self.translator_model = AutoModelForSeq2SeqLM.from_pretrained(
295
+ "Helsinki-NLP/opus-mt-ru-en",
296
+ use_safetensors=True, # Force safetensors
297
+ from_tf=False, # Not from TensorFlow
298
+ device_map=device
 
 
 
 
 
 
 
 
 
 
299
  )
300
 
301
+ # Create your own translation function
302
+ def translate_fn(text):
303
+ inputs = self.translator_tokenizer(text, return_tensors="pt").to(device)
304
+ outputs = self.translator_model.generate(**inputs)
305
+ return self.translator_tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
306
 
307
+ # Replace pipeline with custom function
308
+ self.translator = translate_fn
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
309
 
310
+ # Same for Russian translator
311
+ self.rutranslator_tokenizer = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-ru")
312
+ self.rutranslator_model = AutoModelForSeq2SeqLM.from_pretrained(
313
+ "Helsinki-NLP/opus-mt-en-ru",
314
+ use_safetensors=True,
315
+ device_map=device
316
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
 
318
+ def ru_translate_fn(text):
319
+ inputs = self.rutranslator_tokenizer(text, return_tensors="pt").to(device)
320
+ outputs = self.rutranslator_model.generate(**inputs)
321
+ return self.rutranslator_tokenizer.decode(outputs[0], skip_special_tokens=True)
322
+
323
+ self.rutranslator = ru_translate_fn
324
 
325
+ # Continue with other models but use safetensors
326
+ # For sentiment models
327
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
328
 
329
+ # Initialize FinBERT
330
+ finbert_tokenizer = AutoTokenizer.from_pretrained("ProsusAI/finbert")
331
+ finbert_model = AutoModelForSequenceClassification.from_pretrained(
332
+ "ProsusAI/finbert",
333
  use_safetensors=True,
334
+ device_map=device
335
+ )
 
336
 
337
+ # Define custom pipeline instead of using transformers pipeline
338
+ def finbert_sentiment(text):
339
+ inputs = finbert_tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device)
340
+ outputs = finbert_model(**inputs)
341
+ probs = outputs.logits.softmax(dim=1)[0]
342
+ pred_idx = probs.argmax().item()
 
 
 
 
 
 
343
  labels = ["negative", "neutral", "positive"]
344
+ return {"label": labels[pred_idx], "score": probs[pred_idx].item()}
345
 
346
+ self.finbert = lambda text: [finbert_sentiment(text)]
 
347
 
348
+ # Similar custom implementations for other sentiment models...
 
349
 
350
+ # Initialize MT5 model with safetensors
351
  self.model_name = "google/mt5-small"
352
  self.tokenizer = AutoTokenizer.from_pretrained(
353
  self.model_name,
354
  legacy=True
355
  )
356
  self.model = AutoModelForSeq2SeqLM.from_pretrained(
357
+ self.model_name,
358
  use_safetensors=True,
359
+ device_map=device
360
+ )
 
361
 
362
+ # Initialize Groq LLM if key is available
363
  if 'groq_key':
364
  self.groq = ChatOpenAI(
365
  base_url="https://api.groq.com/openai/v1",
 
371
  logger.warning("Groq API key not found, impact estimation will be limited")
372
  self.groq = None
373
 
 
 
 
 
374
  except Exception as e:
375
  logger.error(f"Error in model initialization: {str(e)}")
376
  raise
 
1059
  control = ProcessControl()
1060
 
1061
  with gr.Blocks(analytics_enabled=False) as app:
1062
+ gr.Markdown("# AI-анализ мониторинга новостей v.2.26 + forced cuda")
1063
 
1064
  with gr.Row():
1065
  file_input = gr.File(
requirements.txt CHANGED
@@ -1,8 +1,10 @@
1
  gradio==4.44.0
2
  spaces==0.19.4
3
  pandas
4
- transformers>=4.30.0
5
- torch==2.1.2
 
 
6
  tqdm
7
  sentencepiece
8
  pymystem3
 
1
  gradio==4.44.0
2
  spaces==0.19.4
3
  pandas
4
+ transformers==4.38.0
5
+ safetensors==0.4.2
6
+ sentencepiece==0.1.99
7
+ torch==2.6.0
8
  tqdm
9
  sentencepiece
10
  pymystem3