Uniaff commited on
Commit
cf91668
·
verified ·
1 Parent(s): 91c5333

Update func_ai.py

Browse files
Files changed (1) hide show
  1. func_ai.py +22 -9
func_ai.py CHANGED
@@ -1,18 +1,17 @@
1
  import requests
2
  import torch
3
- # from googletrans import Translator
4
  from transformers import pipeline
5
  from deep_translator import GoogleTranslator
6
  import time
7
  import os
8
- VECTOR_API_URL = os.getenv('API_URL')
9
 
10
- # translator = Translator()
11
 
 
12
  sentiment_model = pipeline(
13
  'sentiment-analysis',
14
- model='cardiffnlp/twitter-xlm-roberta-base-sentiment',
15
- tokenizer='cardiffnlp/twitter-xlm-roberta-base-sentiment',
16
  device=0 if torch.cuda.is_available() else -1
17
  )
18
 
@@ -65,9 +64,23 @@ def analyze_sentiment(comments):
65
  for i in range(0, len(comments), 50):
66
  batch = comments[i:i + 50]
67
  print(f"Анализируем батч с {i} по {i + len(batch)} комментарий: {batch}")
68
- batch_results = sentiment_model(batch)
69
- print(f"Результаты батча: {batch_results}")
70
- results.extend(batch_results)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  time.sleep(1) # Задержка для предотвращения перегрузки
72
- print("Анализ настроений завершен. Общие результаты: {results}")
73
  return results
 
1
  import requests
2
  import torch
 
3
  from transformers import pipeline
4
  from deep_translator import GoogleTranslator
5
  import time
6
  import os
 
7
 
8
+ VECTOR_API_URL = os.getenv('API_URL')
9
 
10
+ # Initialize the sentiment analysis model with distilbert
11
  sentiment_model = pipeline(
12
  'sentiment-analysis',
13
+ model='distilbert/distilbert-base-uncased-finetuned-sst-2-english',
14
+ tokenizer='distilbert/distilbert-base-uncased-finetuned-sst-2-english',
15
  device=0 if torch.cuda.is_available() else -1
16
  )
17
 
 
64
  for i in range(0, len(comments), 50):
65
  batch = comments[i:i + 50]
66
  print(f"Анализируем батч с {i} по {i + len(batch)} комментарий: {batch}")
67
+
68
+ # Translate each comment in the batch
69
+ try:
70
+ translated_batch = [GoogleTranslator(source='auto', target='en').translate(comment) for comment in batch]
71
+ print(f"Translated batch: {translated_batch}")
72
+ except Exception as e:
73
+ print(f"Translation failed: {e}")
74
+ continue
75
+
76
+ # Analyze sentiment on the translated batch
77
+ try:
78
+ batch_results = sentiment_model(translated_batch)
79
+ print(f"Результаты батча: {batch_results}")
80
+ results.extend(batch_results)
81
+ except Exception as e:
82
+ print(f"Sentiment analysis failed: {e}")
83
+
84
  time.sleep(1) # Задержка для предотвращения перегрузки
85
+ print(f"Анализ настроений завершен. Общие результаты: {results}")
86
  return results