Manish014 commited on
Commit
ba64767
Β·
verified Β·
1 Parent(s): 8c19911

updated app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -21
app.py CHANGED
@@ -1,28 +1,34 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load model with custom generation settings
5
- pipe = pipeline(
6
  "summarization",
7
  model="Manish014/review-summariser-gpt-config1",
8
  tokenizer="Manish014/review-summariser-gpt-config1",
9
- device=0 # Use GPU if available, else set to -1
10
  )
11
 
12
- # Summarization function with settings
13
- def summarize(text):
 
 
14
  if not text.strip():
15
- return "Please enter a product review."
16
-
17
- result = pipe(
18
  text,
19
  max_length=80,
20
  min_length=10,
21
  num_beams=4,
22
  early_stopping=True,
23
  length_penalty=1.2
24
- )
25
- return result[0]["summary_text"]
 
 
 
 
26
 
27
  # Example reviews
28
  example_reviews = [
@@ -31,17 +37,30 @@ example_reviews = [
31
  ["Worst purchase I've made. Do not recommend at all."]
32
  ]
33
 
34
- # Gradio Interface
35
- demo = gr.Interface(
36
- fn=summarize,
37
- inputs=gr.Textbox(label="Product Review", lines=5, placeholder="Enter product review..."),
38
- outputs=gr.Textbox(label="Summary"),
39
- title="πŸ“ Review Summariser GPT - Config 1",
40
- description="Enter a long product review and get a helpful short summary.",
41
- examples=example_reviews,
42
- theme="default"
43
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- # Launch app
46
  if __name__ == "__main__":
47
  demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Load both summarization and sentiment models
5
+ summarizer = pipeline(
6
  "summarization",
7
  model="Manish014/review-summariser-gpt-config1",
8
  tokenizer="Manish014/review-summariser-gpt-config1",
9
+ device=0 # Use GPU if available
10
  )
11
 
12
+ sentiment_analyzer = pipeline("sentiment-analysis")
13
+
14
+ # Inference function
15
+ def analyze_review(text):
16
  if not text.strip():
17
+ return "Please enter a review.", "Sentiment unavailable."
18
+
19
+ summary = summarizer(
20
  text,
21
  max_length=80,
22
  min_length=10,
23
  num_beams=4,
24
  early_stopping=True,
25
  length_penalty=1.2
26
+ )[0]["summary_text"]
27
+
28
+ sentiment = sentiment_analyzer(text)[0]
29
+ sentiment_output = f"{sentiment['label']} ({round(sentiment['score'] * 100, 2)}%)"
30
+
31
+ return summary, sentiment_output
32
 
33
  # Example reviews
34
  example_reviews = [
 
37
  ["Worst purchase I've made. Do not recommend at all."]
38
  ]
39
 
40
+ # Build Gradio UI
41
+ with gr.Blocks(theme=gr.themes.Base()) as demo:
42
+ gr.Markdown("## πŸ“ Review Summariser GPT - Config 1")
43
+ gr.Markdown("Enter a long product review below to get a helpful short summary and sentiment prediction.")
44
+
45
+ with gr.Row():
46
+ input_box = gr.Textbox(label="πŸ—£οΈ Product Review", lines=5, placeholder="Write your detailed review here...")
47
+
48
+ with gr.Row():
49
+ summary_output = gr.Textbox(label="βœ‚οΈ Summary")
50
+ sentiment_output = gr.Textbox(label="πŸ“Š Sentiment")
51
+
52
+ submit_btn = gr.Button("πŸ” Analyze Review")
53
+ clear_btn = gr.Button("🧹 Clear")
54
+
55
+ submit_btn.click(analyze_review, inputs=input_box, outputs=[summary_output, sentiment_output])
56
+ clear_btn.click(lambda: ("", "", ""), outputs=[input_box, summary_output, sentiment_output])
57
+
58
+ gr.Examples(
59
+ examples=example_reviews,
60
+ inputs=input_box,
61
+ label="πŸ” Try an Example"
62
+ )
63
 
64
+ # Run the app
65
  if __name__ == "__main__":
66
  demo.launch()