Spaces:
Paused
Paused
Update func_ai.py
Browse files- func_ai.py +9 -11
func_ai.py
CHANGED
@@ -9,7 +9,6 @@ VECTOR_API_URL = os.getenv('API_URL')
|
|
9 |
|
10 |
# translator = Translator()
|
11 |
|
12 |
-
|
13 |
sentiment_model = pipeline(
|
14 |
'sentiment-analysis',
|
15 |
model='cardiffnlp/twitter-xlm-roberta-base-sentiment',
|
@@ -17,15 +16,12 @@ sentiment_model = pipeline(
|
|
17 |
device=0 if torch.cuda.is_available() else -1
|
18 |
)
|
19 |
|
20 |
-
|
21 |
-
|
22 |
classifier = pipeline(
|
23 |
"zero-shot-classification",
|
24 |
model="valhalla/distilbart-mnli-12-6",
|
25 |
device=0 if torch.cuda.is_available() else -1
|
26 |
)
|
27 |
|
28 |
-
|
29 |
def classify_comment(text):
|
30 |
if not text:
|
31 |
print("Received empty text for classification.")
|
@@ -33,43 +29,45 @@ def classify_comment(text):
|
|
33 |
print(f"Classifying comment: {text}")
|
34 |
try:
|
35 |
translated_text = GoogleTranslator(source='auto', target="en").translate(text)
|
|
|
36 |
except Exception as e:
|
37 |
print(f"Translation failed: {e}")
|
38 |
return "non-interrogative"
|
39 |
if not translated_text:
|
40 |
print("Translation returned empty text.")
|
41 |
return "non-interrogative"
|
42 |
-
|
43 |
try:
|
44 |
result = classifier(translated_text, ["interrogative", "non-interrogative"], clean_up_tokenization_spaces=True)
|
|
|
45 |
except Exception as e:
|
46 |
print(f"Classification failed: {e}")
|
47 |
return "non-interrogative"
|
|
|
48 |
top_class = result['labels'][0]
|
|
|
49 |
return top_class
|
50 |
|
51 |
-
|
52 |
def retrieve_from_vdb(query):
|
53 |
print(f"Отправка запроса к FastAPI сервису: {query}")
|
54 |
response = requests.post(f"{VECTOR_API_URL}/search/", json={"query": query})
|
55 |
if response.status_code == 200:
|
56 |
results = response.json().get("results", [])
|
57 |
-
print(f"Получено {len(results)}
|
58 |
return results
|
59 |
else:
|
60 |
print(f"Ошибка при поиске: {response.text}")
|
61 |
return []
|
62 |
|
63 |
-
|
64 |
def analyze_sentiment(comments):
|
65 |
print("Начинаем анализ настроений.")
|
66 |
results = []
|
67 |
for i in range(0, len(comments), 50):
|
68 |
batch = comments[i:i + 50]
|
69 |
-
print(f"Анализируем батч с {i} по {i + len(batch)}
|
70 |
batch_results = sentiment_model(batch)
|
|
|
71 |
results.extend(batch_results)
|
72 |
time.sleep(1) # Задержка для предотвращения перегрузки
|
73 |
-
print("Анализ настроений завершен.")
|
74 |
return results
|
75 |
-
|
|
|
9 |
|
10 |
# translator = Translator()
|
11 |
|
|
|
12 |
sentiment_model = pipeline(
|
13 |
'sentiment-analysis',
|
14 |
model='cardiffnlp/twitter-xlm-roberta-base-sentiment',
|
|
|
16 |
device=0 if torch.cuda.is_available() else -1
|
17 |
)
|
18 |
|
|
|
|
|
19 |
classifier = pipeline(
|
20 |
"zero-shot-classification",
|
21 |
model="valhalla/distilbart-mnli-12-6",
|
22 |
device=0 if torch.cuda.is_available() else -1
|
23 |
)
|
24 |
|
|
|
25 |
def classify_comment(text):
|
26 |
if not text:
|
27 |
print("Received empty text for classification.")
|
|
|
29 |
print(f"Classifying comment: {text}")
|
30 |
try:
|
31 |
translated_text = GoogleTranslator(source='auto', target="en").translate(text)
|
32 |
+
print(f"Translated text: {translated_text}")
|
33 |
except Exception as e:
|
34 |
print(f"Translation failed: {e}")
|
35 |
return "non-interrogative"
|
36 |
if not translated_text:
|
37 |
print("Translation returned empty text.")
|
38 |
return "non-interrogative"
|
39 |
+
|
40 |
try:
|
41 |
result = classifier(translated_text, ["interrogative", "non-interrogative"], clean_up_tokenization_spaces=True)
|
42 |
+
print(f"Classification result: {result}")
|
43 |
except Exception as e:
|
44 |
print(f"Classification failed: {e}")
|
45 |
return "non-interrogative"
|
46 |
+
|
47 |
top_class = result['labels'][0]
|
48 |
+
print(f"Top class: {top_class}")
|
49 |
return top_class
|
50 |
|
|
|
51 |
def retrieve_from_vdb(query):
|
52 |
print(f"Отправка запроса к FastAPI сервису: {query}")
|
53 |
response = requests.post(f"{VECTOR_API_URL}/search/", json={"query": query})
|
54 |
if response.status_code == 200:
|
55 |
results = response.json().get("results", [])
|
56 |
+
print(f"Получено {len(results)} результатов: {results}")
|
57 |
return results
|
58 |
else:
|
59 |
print(f"Ошибка при поиске: {response.text}")
|
60 |
return []
|
61 |
|
|
|
62 |
def analyze_sentiment(comments):
|
63 |
print("Начинаем анализ настроений.")
|
64 |
results = []
|
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
|
|