mayf commited on
Commit
2ecaff0
·
verified ·
1 Parent(s): 6bc8fd1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -30
app.py CHANGED
@@ -2,18 +2,10 @@ import os
2
  import numpy as np
3
  import pandas as pd
4
  import streamlit as st
5
- from huggingface_hub import login
6
- from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
7
  from keybert import KeyBERT
8
- from openai import AzureOpenAI # new
9
-
10
- # ─── Azure OpenAI Client ─────────────────────────────────────────────────────
11
- openai_client = AzureOpenAI(
12
- api_key = "fbca46bfd8814334be46a2e5c323904c", # use your key here
13
- api_version = "2023-05-15", # apparently HKUST uses a deprecated version
14
- azure_endpoint = "https://hkust.azure-api.net" # per HKUST instructions
15
- )
16
 
 
17
  @st.cache_resource
18
  def load_sentiment_pipeline():
19
  model_name = "mayf/amazon_reviews_bert_ft"
@@ -30,6 +22,16 @@ def load_sentiment_pipeline():
30
  def load_keybert_model():
31
  return KeyBERT(model="all-MiniLM-L6-v2")
32
 
 
 
 
 
 
 
 
 
 
 
33
  LABEL_MAP = {
34
  "LABEL_0": "Very Negative",
35
  "LABEL_1": "Negative",
@@ -49,23 +51,22 @@ def main():
49
  st.warning("Please enter a review to analyze.")
50
  return
51
 
52
- # Initialize progress bar
53
  progress = st.progress(0)
54
 
55
  # Load models
56
  progress.text("Loading models...")
57
  sentiment_pipeline = load_sentiment_pipeline()
58
  kw_model = load_keybert_model()
 
59
  progress.progress(20)
60
 
61
- # Run sentiment analysis
62
  progress.text("Analyzing sentiment...")
63
  raw_scores = sentiment_pipeline(review)[0]
64
- # Map labels
65
  sentiment_results = {LABEL_MAP[item['label']]: float(item['score']) for item in raw_scores}
66
  progress.progress(40)
67
 
68
- # Extract keywords
69
  progress.text("Extracting keywords...")
70
  keywords = kw_model.extract_keywords(
71
  review,
@@ -75,7 +76,7 @@ def main():
75
  )
76
  progress.progress(60)
77
 
78
- # Display scores and keywords side by side
79
  col1, col2 = st.columns(2)
80
  with col1:
81
  st.subheader("Sentiment Scores")
@@ -85,7 +86,7 @@ def main():
85
  for kw, score in keywords:
86
  st.write(f"• {kw} ({score:.4f})")
87
 
88
- # Bar chart
89
  progress.text("Rendering chart...")
90
  df_scores = pd.DataFrame.from_dict(sentiment_results, orient='index', columns=['score'])
91
  df_scores.index.name = 'Sentiment'
@@ -96,33 +97,25 @@ def main():
96
  max_label, max_score = max(sentiment_results.items(), key=lambda x: x[1])
97
  st.markdown(f"**Highest Sentiment:** **{max_label}** ({max_score:.4f})")
98
 
99
- # GPT-Driven Analysis & Suggestions
100
  progress.text("Generating insights...")
101
  prompt = f"""
102
  You are an analytical amazon feedback expert.
103
  Review: \"{review}\"
104
  Sentiment Scores: {sentiment_results}
105
  Top Keywords: {[kw for kw, _ in keywords]}
 
106
  Tasks:
107
  1. Analysis: Write a concise paragraph (3 sentences) interpreting customer sentiment by combining the scores and keywords.
108
  2. Recommendations: Three separate paragraphs with actionable suggestions (max 30 words each).
109
  """
 
 
110
 
111
- response = openai_client.chat.completions.create(
112
- model="gpt-35-turbo",
113
- messages=[
114
- {"role": "system", "content": "You are a product-feedback analyst."},
115
- {"role": "user", "content": prompt}
116
- ],
117
- temperature=0.7,
118
- max_tokens=200
119
- )
120
- gpt_reply = response.choices[0].message.content.strip()
121
- st.markdown(gpt_reply)
122
-
123
- # Complete
124
  progress.progress(100)
125
  progress.text("Done!")
126
 
 
127
  if __name__ == "__main__":
128
  main()
 
 
2
  import numpy as np
3
  import pandas as pd
4
  import streamlit as st
5
+ from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
 
6
  from keybert import KeyBERT
 
 
 
 
 
 
 
 
7
 
8
+ # ─── Sentiment & Keyword Models ─────────────────────────────────────────────
9
  @st.cache_resource
10
  def load_sentiment_pipeline():
11
  model_name = "mayf/amazon_reviews_bert_ft"
 
22
  def load_keybert_model():
23
  return KeyBERT(model="all-MiniLM-L6-v2")
24
 
25
+ # ─── FLAN-T5 Generation Pipeline ────────────────────────────────────────────
26
+ @st.cache_resource
27
+ def load_flant5_pipeline():
28
+ # High-level helper for text2text generation
29
+ return pipeline(
30
+ "text2text-generation",
31
+ model="google/flan-t5-base",
32
+ tokenizer="google/flan-t5-base"
33
+ )
34
+
35
  LABEL_MAP = {
36
  "LABEL_0": "Very Negative",
37
  "LABEL_1": "Negative",
 
51
  st.warning("Please enter a review to analyze.")
52
  return
53
 
 
54
  progress = st.progress(0)
55
 
56
  # Load models
57
  progress.text("Loading models...")
58
  sentiment_pipeline = load_sentiment_pipeline()
59
  kw_model = load_keybert_model()
60
+ generation_pipeline = load_flant5_pipeline()
61
  progress.progress(20)
62
 
63
+ # Sentiment
64
  progress.text("Analyzing sentiment...")
65
  raw_scores = sentiment_pipeline(review)[0]
 
66
  sentiment_results = {LABEL_MAP[item['label']]: float(item['score']) for item in raw_scores}
67
  progress.progress(40)
68
 
69
+ # Keywords
70
  progress.text("Extracting keywords...")
71
  keywords = kw_model.extract_keywords(
72
  review,
 
76
  )
77
  progress.progress(60)
78
 
79
+ # Display
80
  col1, col2 = st.columns(2)
81
  with col1:
82
  st.subheader("Sentiment Scores")
 
86
  for kw, score in keywords:
87
  st.write(f"• {kw} ({score:.4f})")
88
 
89
+ # Chart
90
  progress.text("Rendering chart...")
91
  df_scores = pd.DataFrame.from_dict(sentiment_results, orient='index', columns=['score'])
92
  df_scores.index.name = 'Sentiment'
 
97
  max_label, max_score = max(sentiment_results.items(), key=lambda x: x[1])
98
  st.markdown(f"**Highest Sentiment:** **{max_label}** ({max_score:.4f})")
99
 
100
+ # FLAN-T5 Analysis & Suggestions
101
  progress.text("Generating insights...")
102
  prompt = f"""
103
  You are an analytical amazon feedback expert.
104
  Review: \"{review}\"
105
  Sentiment Scores: {sentiment_results}
106
  Top Keywords: {[kw for kw, _ in keywords]}
107
+
108
  Tasks:
109
  1. Analysis: Write a concise paragraph (3 sentences) interpreting customer sentiment by combining the scores and keywords.
110
  2. Recommendations: Three separate paragraphs with actionable suggestions (max 30 words each).
111
  """
112
+ output = generation_pipeline(prompt, max_length=200, do_sample=False)[0]['generated_text']
113
+ st.markdown(output)
114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  progress.progress(100)
116
  progress.text("Done!")
117
 
118
+
119
  if __name__ == "__main__":
120
  main()
121
+