demo / untitled9.py
rangeAtom's picture
Upload untitled9.py
23bdf7d verified
raw
history blame
2.29 kB
# -*- 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()