mayf commited on
Commit
dd57377
·
verified ·
1 Parent(s): 443a3e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -38
app.py CHANGED
@@ -2,25 +2,23 @@ import os
2
  import numpy as np
3
  import pandas as pd
4
  import streamlit as st
5
- from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification, AutoModelForCausalLM
 
6
  from keybert import KeyBERT
 
7
 
8
- # ─── DeepSeek Model Client ────────────────────────────────────────────────────
9
- # High-level helper pipeline for text-generation
10
- pipe = pipeline(
11
- "text-generation",
12
- model="deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B",
13
- trust_remote_code=True
14
  )
15
 
16
  @st.cache_resource
17
  def load_sentiment_pipeline():
18
  model_name = "mayf/amazon_reviews_bert_ft"
19
  tok = AutoTokenizer.from_pretrained(model_name, use_auth_token=True)
20
- mdl = AutoModelForSequenceClassification.from_pretrained(
21
- model_name,
22
- use_auth_token=True
23
- )
24
  return pipeline(
25
  "sentiment-analysis",
26
  model=mdl,
@@ -51,6 +49,7 @@ def main():
51
  st.warning("Please enter a review to analyze.")
52
  return
53
 
 
54
  progress = st.progress(0)
55
 
56
  # Load models
@@ -59,15 +58,14 @@ def main():
59
  kw_model = load_keybert_model()
60
  progress.progress(20)
61
 
62
- # Sentiment analysis
63
  progress.text("Analyzing sentiment...")
64
  raw_scores = sentiment_pipeline(review)[0]
65
- sentiment_results = {
66
- LABEL_MAP[item['label']]: float(item['score']) for item in raw_scores
67
- }
68
  progress.progress(40)
69
 
70
- # Keyword extraction
71
  progress.text("Extracting keywords...")
72
  keywords = kw_model.extract_keywords(
73
  review,
@@ -77,34 +75,29 @@ def main():
77
  )
78
  progress.progress(60)
79
 
80
- # Display results
81
  col1, col2 = st.columns(2)
82
  with col1:
83
  st.subheader("Sentiment Scores")
84
  st.json({k: round(v, 4) for k, v in sentiment_results.items()})
85
  with col2:
86
- st.subheader("Top Keywords")
87
  for kw, score in keywords:
88
  st.write(f"• {kw} ({score:.4f})")
89
 
90
  # Bar chart
91
  progress.text("Rendering chart...")
92
- df_scores = pd.DataFrame.from_dict(
93
- sentiment_results, orient='index', columns=['score']
94
- )
95
  df_scores.index.name = 'Sentiment'
96
  st.bar_chart(df_scores)
97
  progress.progress(80)
98
 
99
  # Highlight highest sentiment
100
- max_label, max_score = max(
101
- sentiment_results.items(), key=lambda x: x[1]
102
- )
103
  st.markdown(f"**Highest Sentiment:** **{max_label}** ({max_score:.4f})")
104
 
105
  # GPT-Driven Analysis & Suggestions
106
  progress.text("Generating insights...")
107
- # Build the prompt
108
  prompt = f"""
109
  You are an analytical amazon feedback expert.
110
  Review: \"{review}\"
@@ -114,24 +107,23 @@ Tasks:
114
  1. Analysis: Write a concise paragraph (3 sentences) interpreting customer sentiment by combining the scores and keywords.
115
  2. Recommendations: Three separate paragraphs with actionable suggestions (max 30 words each).
116
  """
117
- # Prepare chat messages
118
- chat_input = [
119
- {"role": "system", "content": "You are a product-feedback analyst."},
120
- {"role": "user", "content": prompt}
121
- ]
122
- # Flatten into a single text prompt
123
- flat_prompt = "\n".join(
124
- f"{msg['role'].upper()}: {msg['content']}" for msg in chat_input
125
- )
126
- # Generate
127
- gen_output = pipe(flat_prompt, max_new_tokens=200)
128
- gpt_reply = gen_output[0]['generated_text']
129
 
 
 
 
 
 
 
 
 
 
 
130
  st.markdown(gpt_reply)
 
 
131
  progress.progress(100)
132
  progress.text("Done!")
133
 
134
  if __name__ == "__main__":
135
  main()
136
 
137
-
 
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"
20
  tok = AutoTokenizer.from_pretrained(model_name, use_auth_token=True)
21
+ mdl = AutoModelForSequenceClassification.from_pretrained(model_name, use_auth_token=True)
 
 
 
22
  return pipeline(
23
  "sentiment-analysis",
24
  model=mdl,
 
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
 
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
  )
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")
82
  st.json({k: round(v, 4) for k, v in sentiment_results.items()})
83
  with col2:
84
+ st.subheader("Top 3 Keywords")
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'
92
  st.bar_chart(df_scores)
93
  progress.progress(80)
94
 
95
  # Highlight highest sentiment
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}\"
 
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()
129