Update app.py
Browse files
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
|
6 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
7 |
from keybert import KeyBERT
|
8 |
-
from openai import AzureOpenAI # new
|
9 |
|
10 |
-
# ───
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
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(
|
|
|
|
|
|
|
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 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
|
|
122 |
|
123 |
-
|
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()
|