File size: 5,692 Bytes
9c2c656
 
 
9c0c131
9c2c656
d96f5d2
2772773
9c0c131
9c2c656
95ad2c5
9c2c656
 
 
 
d96f5d2
 
 
 
 
2772773
 
 
 
 
9c0c131
 
 
 
 
d96f5d2
 
95ad2c5
 
 
 
 
 
 
d96f5d2
 
 
 
 
 
 
2772773
 
 
 
 
 
 
9c0c131
 
 
 
 
 
 
d96f5d2
 
 
 
 
 
 
 
 
 
9c0c131
d96f5d2
 
 
 
 
 
 
 
 
9c0c131
d96f5d2
2772773
 
 
 
 
 
 
9c0c131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2772773
9c2c656
95ad2c5
d96f5d2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import gradio as gr
from transformers import pipeline

# Загружаем модели для анализа тональности, суммаризации текста, генерации подписей к изображениям и ответов на вопросы
sentiment_pipeline = pipeline("sentiment-analysis")
summarization_pipeline = pipeline("summarization")
image_captioning_pipeline = pipeline("image-to-text")
qa_pipeline = pipeline("question-answering")

# Функция для анализа тональности текста
def analyze_sentiment(text):
    result = sentiment_pipeline(text)[0]
    return f"Label: {result['label']}, Confidence: {result['score']:.4f}"

# Функция для суммаризации текста
def summarize_text(text):
    result = summarization_pipeline(text, max_length=50, min_length=25, do_sample=False)
    return result[0]['summary_text']

# Функция для генерации подписи к изображению
def generate_caption(image):
    result = image_captioning_pipeline(image)
    return result[0]['generated_text']

# Функция для ответов на вопросы
def answer_question(context, question):
    result = qa_pipeline(question=question, context=context)
    return f"Answer: {result['answer']}, Confidence: {result['score']:.4f}"

# Примеры текстов для анализа тональности
sentiment_examples = [
    "I love programming, it's so much fun!",
    "This movie was terrible, I hated it.",
    "The weather is nice today.",
    "I feel so frustrated with this project.",
    "Gradio is an amazing tool for building ML demos!"
]

# Примеры текстов для суммаризации
summarization_examples = [
    "Gradio is a powerful tool for building machine learning demos. It allows developers to quickly create interactive interfaces for their models.",
    "The weather today is sunny with a slight breeze. It's a perfect day to go outside and enjoy nature.",
    "Artificial intelligence is transforming industries by automating tasks and providing insights from large datasets."
]

# Примеры изображений для генерации подписей
image_examples = [
    "https://a.d-cd.net/b977306s-1920.jpg",  # Пример 1
    "https://i.pinimg.com/originals/ba/bd/6d/babd6d37eb2dd965c7f1dfb516d54094.jpg",  # Пример 2
    "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
]

# Примеры для ответов на вопросы
qa_examples = [
    ["Gradio is a Python library for building machine learning demos. It allows developers to quickly create interactive interfaces for their models.", "What is Gradio?"],
    ["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?"],
    ["Artificial intelligence is transforming industries by automating tasks and providing insights from large datasets.", "How is AI transforming industries?"]
]

# Создаем интерфейс Gradio с вкладками
with gr.Blocks() as demo:
    with gr.Tab("Sentiment Analysis"):
        gr.Interface(
            fn=analyze_sentiment,
            inputs=gr.Textbox(lines=2, placeholder="Введите текст для анализа тональности..."),
            outputs="text",
            title="Анализ тональности текста",
            description="Введите текст, чтобы определить его тональность.",
            examples=sentiment_examples,
            examples_per_page=5  # Отображаем 3 примера на странице
        )
    with gr.Tab("Text Summarization"):
        gr.Interface(
            fn=summarize_text,
            inputs=gr.Textbox(lines=5, placeholder="Введите текст для суммаризации..."),
            outputs="text",
            title="Суммаризация текста",
            description="Введите текст, чтобы получить его краткое содержание.",
            examples=summarization_examples,
            examples_per_page=3 # Отображаем 2 примера на странице
        )
    with gr.Tab("Image Captioning"):
        gr.Interface(
            fn=generate_caption,
            inputs=gr.Image(type="pil", label="Загрузите изображение"),
            outputs="text",
            title="Генерация подписи к изображению",
            description="Загрузите изображение, чтобы сгенерировать его описание.",
            examples=image_examples,
            examples_per_page=3  # Отображаем 2 примера на странице
        )
    with gr.Tab("Question Answering"):
        gr.Interface(
            fn=answer_question,
            inputs=[
                gr.Textbox(lines=5, placeholder="Введите контекст..."),
                gr.Textbox(lines=2, placeholder="Введите вопрос...")
            ],
            outputs="text",
            title="Ответы на вопросы",
            description="Введите контекст и вопрос, чтобы получить ответ.",
            examples=qa_examples,
            examples_per_page=3  # Отображаем 2 примера на странице
        )

# Запускаем интерфейс
demo.launch()