Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -1,46 +1,51 @@
1
- # ПРОСТЕЙШИЙ ПРИМЕР АНАЛИЗА ТОНАЛЬНОСТИ ТЕКСТА
2
  import gradio as gr
3
  from transformers import pipeline
4
 
5
- # Загружаем готовый пайплайн из Hugging Face
6
- sentiment_pipe = pipeline("sentiment-analysis",
7
- model="blanchefort/rubert-base-cased-sentiment")
 
 
 
 
8
 
9
  def analyze_sentiment(text: str) -> str:
10
  text = text.strip()
11
  if not text:
12
  return "Введите текст для анализа."
13
 
14
- result = sentiment_pipe(text)[0]
 
 
15
  label = result["label"]
16
  score = result["score"]
17
 
18
- # Определяем тональность
19
  if label.upper().startswith("NEG"):
20
- label_ru = "НЕГАТИВНАЯ ТОНАЛЬНОСТЬ"
21
  elif label.upper().startswith("POS"):
22
- label_ru = "ПОЗИТИВНАЯ ТОНАЛЬНОСТЬ"
23
  else:
24
- label_ru = f"ТОНАЛЬНОСТЬ: {label}"
25
 
26
  return f"{label_ru} (уверенность модели: {score:.2f})"
27
 
28
-
29
  # Описание интерфейса
30
  demo = gr.Interface(
31
  fn=analyze_sentiment,
32
  inputs=gr.Textbox(
33
  lines=5,
34
- label="Введите текст (поддерживается русский и английский)",
35
- placeholder="Например: Мне нравится этот продукт!",
36
  ),
37
  outputs=gr.Textbox(label="Результат анализа"),
38
  title="Sentiment Demo",
39
  description=(
40
- "Пример простого приложения.\n"
41
  "Модель определяет тональность текста."
42
  ),
43
  )
44
 
45
- if __name__ == "__main__":
46
  demo.launch()
 
1
+ # Простейший пример анализа тональности текста
2
  import gradio as gr
3
  from transformers import pipeline
4
 
5
+ # Загружаем готовый pipeline с Hugging Face
6
+ translation_pipe = pipeline("translation_ru_to_uz", model="sarahai/nllb-ru-uz")
7
+
8
+ sentiment_pipe = pipeline(
9
+ "sentiment-analysis",
10
+ model="blanchefort/rubert-base-cased-sentiment"
11
+ )
12
 
13
  def analyze_sentiment(text: str) -> str:
14
  text = text.strip()
15
  if not text:
16
  return "Введите текст для анализа."
17
 
18
+ translation = translator_pipe(text)
19
+
20
+ result = sentiment_pipe(translation)[0]
21
  label = result["label"]
22
  score = result["score"]
23
 
24
+ # Перевод меток модели
25
  if label.upper().startswith("NEG"):
26
+ label_ru = "Негативная тональность"
27
  elif label.upper().startswith("POS"):
28
+ label_ru = "Позитивная тональность"
29
  else:
30
+ label_ru = f"Тональность: {label}"
31
 
32
  return f"{label_ru} (уверенность модели: {score:.2f})"
33
 
 
34
  # Описание интерфейса
35
  demo = gr.Interface(
36
  fn=analyze_sentiment,
37
  inputs=gr.Textbox(
38
  lines=5,
39
+ label="Введите текст (желательно на русском)",
40
+ placeholder="Например: Мне нравится этот продукт!"
41
  ),
42
  outputs=gr.Textbox(label="Результат анализа"),
43
  title="Sentiment Demo",
44
  description=(
45
+ "Пример простого приложения на Hugging Face.\n"
46
  "Модель определяет тональность текста."
47
  ),
48
  )
49
 
50
+ if name == "main":
51
  demo.launch()