mayf commited on
Commit
40d3817
·
verified ·
1 Parent(s): 8aeff3e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -23
app.py CHANGED
@@ -2,23 +2,35 @@ 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"
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,
@@ -61,7 +73,6 @@ def main():
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
 
@@ -108,21 +119,22 @@ Tasks:
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, AutoModelForSequenceClassification, AutoModelForCausalLM
 
6
  from keybert import KeyBERT
 
7
 
8
+ # ─── DeepSeek Model Client ────────────────────────────────────────────────────
9
+ # Option 1: High-level helper pipeline for chat-like generation
10
+ pipe = pipeline(
11
+ "text-generation",
12
+ model="deepseek-ai/DeepSeek-R1",
13
+ trust_remote_code=True
14
  )
15
 
16
+ # Option 2: Direct model & tokenizer instantiation (alternative)
17
+ # tokenizer_ds = AutoTokenizer.from_pretrained(
18
+ # "deepseek-ai/DeepSeek-R1",
19
+ # trust_remote_code=True
20
+ # )
21
+ # model_ds = AutoModelForCausalLM.from_pretrained(
22
+ # "deepseek-ai/DeepSeek-R1",
23
+ # trust_remote_code=True
24
+ # )
25
+
26
  @st.cache_resource
27
  def load_sentiment_pipeline():
28
  model_name = "mayf/amazon_reviews_bert_ft"
29
  tok = AutoTokenizer.from_pretrained(model_name, use_auth_token=True)
30
+ mdl = AutoModelForSequenceClassification.from_pretrained(
31
+ model_name,
32
+ use_auth_token=True
33
+ )
34
  return pipeline(
35
  "sentiment-analysis",
36
  model=mdl,
 
73
  # Run sentiment analysis
74
  progress.text("Analyzing sentiment...")
75
  raw_scores = sentiment_pipeline(review)[0]
 
76
  sentiment_results = {LABEL_MAP[item['label']]: float(item['score']) for item in raw_scores}
77
  progress.progress(40)
78
 
 
119
  2. Recommendations: Three separate paragraphs with actionable suggestions (max 30 words each).
120
  """
121
 
122
+ # Use the high-level pipeline for generation
123
+ chat_input = [
124
+ {"role": "system", "content": "You are a product-feedback analyst."},
125
+ {"role": "user", "content": prompt}
126
+ ]
127
+ gen_output = pipe(chat_input)
128
+ gpt_reply = gen_output[0]['generated_text']
129
+
130
+ # Alternative: direct model invocation
131
+ # inputs = tokenizer_ds(prompt, return_tensors="pt")
132
+ # outputs = model_ds.generate(**inputs, max_new_tokens=200)
133
+ # gpt_reply = tokenizer_ds.decode(outputs[0], skip_special_tokens=True)
134
 
135
+ st.markdown(gpt_reply)
136
  progress.progress(100)
137
  progress.text("Done!")
138
 
139
  if __name__ == "__main__":
140
+ main()