Felguk commited on
Commit
d96f5d2
·
verified ·
1 Parent(s): 95ad2c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -14
app.py CHANGED
@@ -1,16 +1,22 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Загружаем модель для анализа тональности
5
  sentiment_pipeline = pipeline("sentiment-analysis")
 
6
 
7
  # Функция для анализа тональности текста
8
  def analyze_sentiment(text):
9
  result = sentiment_pipeline(text)[0]
10
  return f"Label: {result['label']}, Confidence: {result['score']:.4f}"
11
 
12
- # Примеры текстов для анализа
13
- examples = [
 
 
 
 
 
14
  "I love programming, it's so much fun!",
15
  "This movie was terrible, I hated it.",
16
  "The weather is nice today.",
@@ -18,16 +24,35 @@ examples = [
18
  "Gradio is an amazing tool for building ML demos!"
19
  ]
20
 
21
- # Создаем интерфейс Gradio
22
- iface = gr.Interface(
23
- fn=analyze_sentiment,
24
- inputs=gr.Textbox(lines=2, placeholder="Введите текст здесь..."),
25
- outputs="text",
26
- title="Анализ тональности текста",
27
- description="Введите текст, чтобы определить его тональность.",
28
- examples=examples, # Добавляем примеры
29
- examples_per_page=5 # Количество примеров на странице
30
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  # Запускаем интерфейс
33
- iface.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Загружаем модели для анализа тональности и суммаризации текста
5
  sentiment_pipeline = pipeline("sentiment-analysis")
6
+ summarization_pipeline = pipeline("summarization")
7
 
8
  # Функция для анализа тональности текста
9
  def analyze_sentiment(text):
10
  result = sentiment_pipeline(text)[0]
11
  return f"Label: {result['label']}, Confidence: {result['score']:.4f}"
12
 
13
+ # Функция для суммаризации текста
14
+ def summarize_text(text):
15
+ result = summarization_pipeline(text, max_length=50, min_length=25, do_sample=False)
16
+ return result[0]['summary_text']
17
+
18
+ # Примеры текстов для анализа тональности
19
+ sentiment_examples = [
20
  "I love programming, it's so much fun!",
21
  "This movie was terrible, I hated it.",
22
  "The weather is nice today.",
 
24
  "Gradio is an amazing tool for building ML demos!"
25
  ]
26
 
27
+ # Примеры текстов для суммаризации
28
+ summarization_examples = [
29
+ "Gradio is a powerful tool for building machine learning demos. It allows developers to quickly create interactive interfaces for their models.",
30
+ "The weather today is sunny with a slight breeze. It's a perfect day to go outside and enjoy nature.",
31
+ "Artificial intelligence is transforming industries by automating tasks and providing insights from large datasets."
32
+ ]
33
+
34
+ # Создаем интерфейс Gradio с вкладками
35
+ with gr.Blocks() as demo:
36
+ with gr.Tab("Sentiment Analysis"):
37
+ gr.Interface(
38
+ fn=analyze_sentiment,
39
+ inputs=gr.Textbox(lines=2, placeholder="Введите текст для анализа тональности..."),
40
+ outputs="text",
41
+ title="Анализ тональности текста",
42
+ description="Введите текст, чтобы определить его тональность.",
43
+ examples=sentiment_examples,
44
+ examples_per_page=5
45
+ )
46
+ with gr.Tab("Text Summarization"):
47
+ gr.Interface(
48
+ fn=summarize_text,
49
+ inputs=gr.Textbox(lines=5, placeholder="Введите текст для суммаризации..."),
50
+ outputs="text",
51
+ title="Суммаризация текста",
52
+ description="Введите текст, чтобы получить его краткое содержание.",
53
+ examples=summarization_examples,
54
+ examples_per_page=3
55
+ )
56
 
57
  # Запускаем интерфейс
58
+ demo.launch()