Felguk commited on
Commit
9c0c131
·
verified ·
1 Parent(s): 2772773

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -4
app.py CHANGED
@@ -1,10 +1,11 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Загружаем модели для анализа тональности, суммаризации текста и генерации подписей к изображениям
5
  sentiment_pipeline = pipeline("sentiment-analysis")
6
  summarization_pipeline = pipeline("summarization")
7
  image_captioning_pipeline = pipeline("image-to-text")
 
8
 
9
  # Функция для анализа тональности текста
10
  def analyze_sentiment(text):
@@ -21,6 +22,11 @@ def generate_caption(image):
21
  result = image_captioning_pipeline(image)
22
  return result[0]['generated_text']
23
 
 
 
 
 
 
24
  # Примеры текстов для анализа тональности
25
  sentiment_examples = [
26
  "I love programming, it's so much fun!",
@@ -44,6 +50,13 @@ image_examples = [
44
  "https://get.wallhere.com/photo/sea-bay-water-beach-coast-swimming-pool-resort-island-lagoon-Caribbean-vacation-estate-leisure-ocean-tropics-2560x1440-px-geographical-feature-atoll-554636.jpg" # Пример 3
45
  ]
46
 
 
 
 
 
 
 
 
47
  # Создаем интерфейс Gradio с вкладками
48
  with gr.Blocks() as demo:
49
  with gr.Tab("Sentiment Analysis"):
@@ -54,7 +67,7 @@ with gr.Blocks() as demo:
54
  title="Анализ тональности текста",
55
  description="Введите текст, чтобы определить его тональность.",
56
  examples=sentiment_examples,
57
- examples_per_page=5
58
  )
59
  with gr.Tab("Text Summarization"):
60
  gr.Interface(
@@ -64,7 +77,7 @@ with gr.Blocks() as demo:
64
  title="Суммаризация текста",
65
  description="Введите текст, чтобы получить его краткое содержание.",
66
  examples=summarization_examples,
67
- examples_per_page=3
68
  )
69
  with gr.Tab("Image Captioning"):
70
  gr.Interface(
@@ -73,7 +86,21 @@ with gr.Blocks() as demo:
73
  outputs="text",
74
  title="Генерация подписи к изображению",
75
  description="Загрузите изображение, чтобы сгенерировать его описание.",
76
- examples=image_examples # Добавляем примеры изображений
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  )
78
 
79
  # Запускаем интерфейс
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Загружаем модели для анализа тональности, суммаризации текста, генерации подписей к изображениям и ответов на вопросы
5
  sentiment_pipeline = pipeline("sentiment-analysis")
6
  summarization_pipeline = pipeline("summarization")
7
  image_captioning_pipeline = pipeline("image-to-text")
8
+ qa_pipeline = pipeline("question-answering")
9
 
10
  # Функция для анализа тональности текста
11
  def analyze_sentiment(text):
 
22
  result = image_captioning_pipeline(image)
23
  return result[0]['generated_text']
24
 
25
+ # Функция для ответов на вопросы
26
+ def answer_question(context, question):
27
+ result = qa_pipeline(question=question, context=context)
28
+ return f"Answer: {result['answer']}, Confidence: {result['score']:.4f}"
29
+
30
  # Примеры текстов для анализа тональности
31
  sentiment_examples = [
32
  "I love programming, it's so much fun!",
 
50
  "https://get.wallhere.com/photo/sea-bay-water-beach-coast-swimming-pool-resort-island-lagoon-Caribbean-vacation-estate-leisure-ocean-tropics-2560x1440-px-geographical-feature-atoll-554636.jpg" # Пример 3
51
  ]
52
 
53
+ # Примеры для ответов на вопросы
54
+ qa_examples = [
55
+ ["Gradio is a Python library for building machine learning demos. It allows developers to quickly create interactive interfaces for their models.", "What is Gradio?"],
56
+ ["The weather today is sunny with a slight breeze. It's a perfect day to go outside and enjoy nature.", "What is the weather like today?"],
57
+ ["Artificial intelligence is transforming industries by automating tasks and providing insights from large datasets.", "How is AI transforming industries?"]
58
+ ]
59
+
60
  # Создаем интерфейс Gradio с вкладками
61
  with gr.Blocks() as demo:
62
  with gr.Tab("Sentiment Analysis"):
 
67
  title="Анализ тональности текста",
68
  description="Введите текст, чтобы определить его тональность.",
69
  examples=sentiment_examples,
70
+ examples_per_page=5 # Отображаем 3 примера на странице
71
  )
72
  with gr.Tab("Text Summarization"):
73
  gr.Interface(
 
77
  title="Суммаризация текста",
78
  description="Введите текст, чтобы получить его краткое содержание.",
79
  examples=summarization_examples,
80
+ examples_per_page=3 # Отображаем 2 примера на странице
81
  )
82
  with gr.Tab("Image Captioning"):
83
  gr.Interface(
 
86
  outputs="text",
87
  title="Генерация подписи к изображению",
88
  description="Загрузите изображение, чтобы сгенерировать его описание.",
89
+ examples=image_examples,
90
+ examples_per_page=3 # Отображаем 2 примера на странице
91
+ )
92
+ with gr.Tab("Question Answering"):
93
+ gr.Interface(
94
+ fn=answer_question,
95
+ inputs=[
96
+ gr.Textbox(lines=5, placeholder="Введите контекст..."),
97
+ gr.Textbox(lines=2, placeholder="Введите вопрос...")
98
+ ],
99
+ outputs="text",
100
+ title="Ответы на вопросы",
101
+ description="Введите контекст и вопрос, чтобы получить ответ.",
102
+ examples=qa_examples,
103
+ examples_per_page=3 # Отображаем 2 примера на странице
104
  )
105
 
106
  # Запускаем интерфейс