Tabs / app.py
Ray1st's picture
Create app.py
1409d86 verified
import gradio as gr #importing the necessary libraries
from transformers import pipeline
sentiment_pipeline = pipeline("sentiment-analysis") #importing the model that we will use to work at, as explained in the output terminal
chatbot_pipeline = pipeline("text-generation")
summarization_pipeline = pipeline("summarization", model="t5-small") # T5 for summarization
# Sentiment Analysis function
def analyze_sentiment(text):
result = sentiment_pipeline(text)[0]
return result["label"], round(result["score"], 2)
def generate_response(user_input):
response = chatbot_pipeline(user_input, max_length=100, num_return_sequences=1)
return response[0]['generated_text']
# Summarization function
def summarize_text(long_text):
summary = summarization_pipeline(long_text, max_length=50, min_length=25, do_sample=False)
return summary[0]["summary_text"]
# Speech-to-Text function
def text_to_speech(text):
from gtts import gTTS
import os
tts = gTTS(text)
tts.save("output.mp3")
return "output.mp3"
with gr.Blocks() as demo:
gr.Markdown("# Multi-Task AI App with Alternative Models")
with gr.Tabs():
# tab for Sentiment Analysis
with gr.Tab("Sentiment Analysis"):
gr.Markdown("### Sentiment Analysis")
sentiment_input = gr.Textbox(label="Enter Text for Sentiment Analysis")
sentiment_output_label = gr.Textbox(label="Sentiment")
sentiment_output_score = gr.Textbox(label="Confidence Score")
gr.Button("Analyze").click(analyze_sentiment, inputs=sentiment_input, outputs=[sentiment_output_label, sentiment_output_score])
# tab for chatbot
with gr.Tab("Chatbot"):
gr.Markdown("### Chatbot")
chatbot_input = gr.Textbox(label="Enter Your Message")
chatbot_output = gr.Textbox(label="Chatbot Response", lines=5)
gr.Button("Send").click(chatbot_response, inputs=chatbot_input, outputs=chatbot_output)
# tab for summarization
with gr.Tab("Summarization"):
gr.Markdown("### Summarization")
summarization_input = gr.Textbox(label="Enter Long Text for Summarization", lines=5)
summarization_output = gr.Textbox(label="Summary", lines=5)
gr.Button("Summarize").click(summarize_text, inputs=summarization_input, outputs=summarization_output)
# tab for STT
with gr.Tab("Speech-to-Text"):
gr.Markdown("### Text-to-Speech")
tts_input = gr.Textbox(label="Enter Text for Text-to-Speech")
tts_output = gr.Audio(label="Generated Speech")
gr.Button("Convert to Speech").click(text_to_speech, inputs=tts_input, outputs=tts_output)
demo.launch()