File size: 2,678 Bytes
d1f6f7b
2bfcd91
d1f6f7b
b723159
ba64767
8c19911
 
 
ba64767
8c19911
d1f6f7b
ba64767
 
 
 
2bfcd91
b723159
 
ba64767
8c19911
 
 
 
 
 
ba64767
 
 
b723159
ba64767
b723159
d1f6f7b
b723159
 
2bfcd91
 
b723159
 
 
d1f6f7b
 
b723159
ba64767
 
b723159
 
 
 
ba64767
 
b723159
 
ba64767
 
b723159
 
ba64767
b723159
 
ba64767
b723159
ba64767
b723159
 
 
 
 
 
 
 
 
d1f6f7b
b723159
8c19911
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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()