Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import pipeline | |
# Load models | |
summarizer = pipeline( | |
"summarization", | |
model="Manish014/review-summariser-gpt-config1", | |
tokenizer="Manish014/review-summariser-gpt-config1", | |
device=0 # Use GPU if available | |
) | |
sentiment_analyzer = pipeline("sentiment-analysis") | |
# Inference function | |
def analyze_review(text): | |
if not text.strip(): | |
return "β Please enter a product review.", "β Sentiment unavailable." | |
summary = summarizer( | |
text, | |
max_length=80, | |
min_length=10, | |
num_beams=4, | |
early_stopping=True, | |
length_penalty=1.2 | |
)[0]["summary_text"] | |
sentiment = sentiment_analyzer(text)[0] | |
sentiment_label = f"{sentiment['label']} ({round(sentiment['score'] * 100, 2)}%)" | |
return summary, sentiment_label | |
# Example inputs | |
examples = [ | |
["This product leaks water and smells like burnt plastic."], | |
["Absolutely loved the screen resolution and battery life."], | |
["Worst purchase I've made. Do not recommend at all."], | |
["The headphones are okay. Battery is good but fit is not comfortable."], | |
["The fan is extremely loud and doesn't cool much."] | |
] | |
# Build UI | |
with gr.Blocks(theme=gr.themes.Base()) as demo: | |
gr.Markdown("## π Review Summariser GPT - Config 1") | |
gr.Markdown("Enter a detailed product review below to receive a helpful summary βοΈ and predicted sentiment π.") | |
with gr.Row(): | |
review_input = gr.Textbox(label="π£οΈ Product Review", lines=5, placeholder="Write your review here...") | |
with gr.Row(): | |
summary_output = gr.Textbox(label="βοΈ Summary", lines=2) | |
sentiment_output = gr.Textbox(label="π Sentiment", lines=1) | |
with gr.Row(): | |
analyze_btn = gr.Button("π Analyze") | |
clear_btn = gr.Button("π§Ή Clear") | |
analyze_btn.click(analyze_review, inputs=review_input, outputs=[summary_output, sentiment_output]) | |
clear_btn.click(lambda: ("", "", ""), outputs=[review_input, summary_output, sentiment_output]) | |
gr.Examples(examples=examples, inputs=review_input, label="π Try Example Reviews") | |
with gr.Accordion("βΉοΈ About this App", open=False): | |
gr.Markdown( | |
""" | |
This application uses a fine-tuned T5 model to summarize lengthy product reviews into short summaries and also classifies the sentiment as Positive or Negative. | |
- Model: `Manish014/review-summariser-gpt-config1` | |
- Summarization by π€ Transformers | |
- Sentiment by `distilbert-base-uncased-finetuned-sst-2-english` | |
""" | |
) | |
# Run app | |
if __name__ == "__main__": | |
demo.launch() | |