File size: 3,572 Bytes
eb36270
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline

sentiment_model = pipeline("sentiment-analysis")

def analyze_sentiment(text):
    result = sentiment_model(text)
    return result[0]['label'], result[0]['score']

sentiment_tab = gr.Interface(fn=analyze_sentiment, inputs=gr.Textbox(), outputs=["text", "number"])
sentiment_tab.launch()
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline

# Use "text-generation" pipeline instead of "conversational"
chatbot_model = pipeline("text-generation", model="microsoft/DialoGPT-medium")  

def chat_with_bot(message):
    response = chatbot_model(message)
    # Access the generated text correctly for the "text-generation" pipeline
    return response[0]['generated_text']  

chatbot_tab = gr.Interface(fn=chat_with_bot, inputs=gr.Textbox(), outputs="text", live=True)
chatbot_tab.launch()
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")

def summarize_text(text):
    summary = summarizer(text)
    return summary[0]['summary_text']

summarization_tab = gr.Interface(fn=summarize_text, inputs=gr.Textbox(), outputs="text")
summarization_tab.launch()
import pyttsx3

def text_to_speech(text):
    engine = pyttsx3.init()
    engine.say(text)
    engine.save_to_file(text, "output.mp3")
    engine.runAndWait()
    return "output.mp3"

speech_tab = gr.Interface(fn=text_to_speech, inputs=gr.Textbox(), outputs="audio")
speech_tab.launch()


import gradio as gr
from transformers import pipeline
import pyttsx3

# 1. Sentiment Analysis
sentiment_model = pipeline("sentiment-analysis")
def analyze_sentiment(text):
    result = sentiment_model(text)
    return result[0]['label'], result[0]['score']

# 2. Chatbot
chatbot_model = pipeline("text-generation", model="microsoft/DialoGPT-medium")  
def chat_with_bot(message):
    response = chatbot_model(message)
    return response[0]['generated_text']

# 3. Summarization
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
def summarize_text(text):
    summary = summarizer(text)
    return summary[0]['summary_text']

# 4. Text-to-Speech
def text_to_speech(text):
    engine = pyttsx3.init()
    engine.say(text)
    engine.save_to_file(text, "output.mp3")
    engine.runAndWait()
    return "output.mp3"

# Gradio interface with tabs
with gr.Blocks() as demo:
    with gr.Tab("Sentiment Analysis"):
        with gr.Row():
            text_input = gr.Textbox(label="Enter Text for Sentiment Analysis")
            sentiment_output = gr.Label(label="Sentiment")
            sentiment_score = gr.Number(label="Score")
        text_input.submit(analyze_sentiment, inputs=text_input, outputs=[sentiment_output, sentiment_score])
    
    with gr.Tab("Chatbot"):
        with gr.Row():
            chatbot_input = gr.Textbox(label="Talk to the Bot")
            chatbot_output = gr.Textbox(label="Bot Response")
        chatbot_input.submit(chat_with_bot, inputs=chatbot_input, outputs=chatbot_output)
    
    with gr.Tab("Summarization"):
        with gr.Row():
            summarization_input = gr.Textbox(label="Enter Text for Summarization")
            summarization_output = gr.Textbox(label="Summary")
        summarization_input.submit(summarize_text, inputs=summarization_input, outputs=summarization_output)
    
    with gr.Tab("Text-to-Speech"):
        with gr.Row():
            tts_input = gr.Textbox(label="Enter Text to Convert to Speech")
            audio_output = gr.Audio(label="Generated Speech")
        tts_input.submit(text_to_speech, inputs=tts_input, outputs=audio_output)

demo.launch()