P2 / app.py
ZiyadAlharthi's picture
Create app.py
eb36270 verified
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()