File size: 2,294 Bytes
23bdf7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
"""Untitled9.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1lByo8bABlmF1g_sMH-Aps0X2jAab758m
"""

!pip install gradio --quiet
!pip install transformers --quiet

import gradio as gr
from transformers import pipeline

def sentiment_analysis(text):
    sentiment_pipeline = pipeline("sentiment-analysis")
    result = sentiment_pipeline(text)[0]
    return result["label"], result["score"]

def chatbot_response(user_input):
    chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
    response = chatbot(user_input, max_length=100)[0]["generated_text"]
    return response

def summarize_text(text):
    summarization_pipeline = pipeline("summarization")
    summary = summarization_pipeline(text, max_length=100, min_length=30, do_sample=False)[0]["summary_text"]
    return summary

def text_to_speech(text):
    tts_pipeline = pipeline("text-to-speech", model="facebook/fastspeech2-en-ljspeech")
    audio = tts_pipeline(text)
    return audio["audio"]

with gr.Blocks() as demo:
    with gr.Tab("Sentiment Analysis"):
        text_input = gr.Textbox(label="Enter text")
        sentiment_output = gr.Textbox(label="Sentiment")
        confidence_output = gr.Number(label="Confidence Score")
        sentiment_btn = gr.Button("Analyze")
        sentiment_btn.click(sentiment_analysis, inputs=text_input, outputs=[sentiment_output, confidence_output])

    with gr.Tab("Chatbot"):
        chat_input = gr.Textbox(label="Ask the chatbot")
        chat_output = gr.Textbox(label="Response")
        chat_btn = gr.Button("Send")
        chat_btn.click(chatbot_response, inputs=chat_input, outputs=chat_output)

    with gr.Tab("Summarization"):
        text_area = gr.Textbox(label="Enter long text", lines=5)
        summary_output = gr.Textbox(label="Summary")
        summary_btn = gr.Button("Summarize")
        summary_btn.click(summarize_text, inputs=text_area, outputs=summary_output)

    with gr.Tab("Text-to-Speech"):
        tts_input = gr.Textbox(label="Enter text to convert to speech")
        tts_output = gr.Audio(label="Generated Speech")
        tts_btn = gr.Button("Convert")
        tts_btn.click(text_to_speech, inputs=tts_input, outputs=tts_output)

demo.launch()