Spaces:
Sleeping
Sleeping
# Importing dependencies (Transformers for summarization and sentiment pipeline. Gradio for building UI) | |
#!pip install transformers gradio (needed for Google Colab, not for HF) | |
from transformers import pipeline | |
import gradio as gr | |
#intialiazing summarizer pipeline | |
summarizer_pipeline = pipeline("summarization") | |
#intialiazing sentiment pipline. It in not essential to specify the model but I have chosen it as this model is designed for sentiment analysis. | |
sentiment_pipeline = pipeline("sentiment-analysis", model="siebert/sentiment-roberta-large-english") | |
"""" | |
Function to input text and process the input text (summarization and sentiment analysis), | |
Choosing a text limit for input length since the summarizer works best if the text to be summarized is greater in length than its out max_length(token output). | |
""" | |
#Function | |
def summarize_and_sentiment(text): | |
text = text.strip() # Remove leading/trailing whitespace | |
# Checking if text length is at least 150 characters as I have set max_length for output token = 150 | |
if len(text) < 150: | |
return "Error: Text should be greater than 150 characters in length. Please increase the length of the text.", "" #Using "" as second argument so that when an error occurs the function returns two values instead of one and this is what expected in Gradio UI.The outputs would no longer match the expected structure if we return just one outpit from function. | |
# Generate summary using summarizer pipeline | |
summary = summarizer_pipeline(text, min_length=4, max_length=150) | |
summary_text = summary[0]["summary_text"] #Only using the text generated for summarization. | |
# Perform sentiment analysis on the summary using sentiment pipeline | |
sentiment = sentiment_pipeline(summary_text) | |
sentiment_label = sentiment[0]['label'] #Only using the label(positive or negative) and not the score for sentiment. | |
return summary_text, sentiment_label #Returing two outputs (only summarized text and sentiment label), hence in case of error, if loop also needs to output two values so that UI input-output structure can be easily handled. | |
# Setting up Gradio UI | |
with gr.Blocks() as demo: | |
gr.Markdown("# Text Summarizer and Sentiment Analysis Tool") | |
gr.Markdown("Input a text with at least 150 characters to get its summary and sentiment analysis.") | |
# Text input box | |
input_text = gr.Textbox( | |
label="Enter your text : ", | |
placeholder="Paste your text here...", | |
lines=5 | |
) | |
# Output boxes | |
output_summary = gr.Textbox(label="Summary : ", interactive=False) | |
output_sentiment = gr.Textbox(label="Sentiment : ", interactive=False) | |
# Button to process text | |
analyze_button = gr.Button("Start the process!") | |
# Connect button with function. Analyze button when clicked, itself maps input and output to and from the function(def summarize_and_sentiment). | |
analyze_button.click( | |
fn=summarize_and_sentiment, | |
inputs=input_text, | |
outputs=[output_summary, output_sentiment] #Order of output must be the same as the order of output of function(def summarize_and_sentiment). | |
) | |
# Launch the Gradio app | |
demo.launch() |