M2S3 / tabsApp.py
Mohammed J
Create tabsApp.py
31b4996 verified
# Sentiment Analysis Model
sentiment_analyzer = pipeline("sentiment-analysis")
# Chatbot Model
chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
# Summarization Model
summarizer = pipeline("summarization")
# Sentiment Analysis Function
def analyze_sentiment(text):
result = sentiment_analyzer(text)[0]
sentiment = result['label']
confidence = round(result['score'], 4)
return sentiment, confidence
# Chatbot Function
def generate_response(user_input, history=[]):
history.append(user_input)
input_text = " ".join(history)
response = chatbot(input_text, max_length=1000, pad_token_id=50256)
bot_reply = response[0]['generated_text'][len(input_text):]
history.append(bot_reply)
return history, history
# Summarization Function
def summarize_text(text):
summary = summarizer(text, max_length=150, min_length=40, do_sample=False)
return summary[0]['summary_text']
with gr.Blocks() as demo:
gr.Markdown("# Multi-Function Language Application")
with gr.Tabs():
with gr.TabItem("Sentiment Analysis"):
gr.Markdown("## Enter text for sentiment analysis:")
sentiment_input = gr.Textbox(placeholder="Type your text here...")
sentiment_button = gr.Button("Analyze")
sentiment_output = gr.Textbox(label="Sentiment")
confidence_output = gr.Textbox(label="Confidence Score")
sentiment_button.click(analyze_sentiment, inputs=sentiment_input, outputs=[sentiment_output, confidence_output])
with gr.TabItem("Chatbot"):
gr.Markdown("## Chat with the bot:")
chatbot_input = gr.Textbox(placeholder="Type your message here...")
chatbot_button = gr.Button("Send")
chatbot_output = gr.Chatbot()
chatbot_state = gr.State([])
chatbot_button.click(generate_response, inputs=[chatbot_input, chatbot_state], outputs=[chatbot_output, chatbot_state])
with gr.TabItem("Summarization"):
gr.Markdown("## Enter text to summarize:")
summary_input = gr.Textbox(placeholder="Paste your text here...")
summary_button = gr.Button("Summarize")
summary_output = gr.Textbox(label="Summary")
summary_button.click(summarize_text, inputs=summary_input, outputs=summary_output)
demo.launch()