frankai98 commited on
Commit
91eb9f9
Β·
verified Β·
1 Parent(s): 5782099

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -88
app.py CHANGED
@@ -1,110 +1,129 @@
1
- import os
2
- import nest_asyncio
3
- nest_asyncio.apply()
4
- import streamlit as st
5
- from transformers import pipeline
6
- from huggingface_hub import login
7
- from streamlit.components.v1 import html
 
 
8
 
9
  # Retrieve the token from environment variables
10
- hf_token = os.environ.get("HF_TOKEN")
11
- if not hf_token:
12
- st.error("Hugging Face token not found. Please set the HF_TOKEN environment variable.")
13
- st.stop()
14
 
15
  # Login with the token
16
- login(token=hf_token)
17
 
18
  # Initialize session state for timer and results
19
- if 'result' not in st.session_state:
20
- st.session_state.result = {}
21
- if 'timer_started' not in st.session_state:
22
- st.session_state.timer_started = False
23
- if 'timer_frozen' not in st.session_state:
24
- st.session_state.timer_frozen = False
25
 
26
  # Timer component using HTML and JavaScript
27
- def timer():
28
- return """
29
- <div id="timer" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>
30
- <script>
31
- (function() {
32
- var start = Date.now();
33
- var timerElement = document.getElementById('timer');
34
- localStorage.removeItem("freezeTimer");
35
- var interval = setInterval(function() {
36
- if(localStorage.getItem("freezeTimer") === "true"){
37
- clearInterval(interval);
38
- timerElement.style.color = '#00cc00';
39
- return;
40
- }
41
- var elapsed = Date.now() - start;
42
- var minutes = Math.floor(elapsed / 60000);
43
- var seconds = Math.floor((elapsed % 60000) / 1000);
44
- timerElement.innerHTML = '⏱️ Elapsed: ' +
45
- (minutes < 10 ? '0' : '') + minutes + ':' +
46
- (seconds < 10 ? '0' : '') + seconds;
47
- }, 1000);
48
- })();
49
- </script>
50
- """
51
 
52
- st.set_page_config(page_title="Sentiment & Report Generator", page_icon="πŸ“")
53
- st.header("Sentiment Analysis & Report Generation with Gemma")
 
 
 
 
 
 
 
54
 
55
  # Load models with caching to avoid reloading on every run
56
- @st.cache_resource
57
- def load_models():
58
- sentiment_pipe = pipeline("text-classification", model="mixedbread-ai/mxbai-rerank-base-v1")
59
- gemma_pipe = pipeline("text-generation", model="google/gemma-3-1b-it", use_auth_token=hf_token)
60
- return sentiment_pipe, gemma_pipe
 
 
61
 
62
- sentiment_pipe, gemma_pipe = load_models()
63
 
64
- # Provide two options for input: text area or file upload
65
- uploaded_file = st.file_uploader("Upload Review File (txt format)", type=["txt"])
66
  user_input = st.text_area("Or, enter your text for sentiment analysis and report generation:")
67
 
68
- # If a file is uploaded, override user_input with its contents
69
  if uploaded_file is not None:
70
  try:
71
- user_input = uploaded_file.read().decode("utf-8")
 
 
 
 
 
 
72
  except Exception as e:
73
  st.error(f"Error reading file: {e}")
74
 
75
- if st.button("Generate Report"):
76
- if not user_input.strip():
77
  st.error("Please enter some text!")
78
- else:
79
- if not st.session_state.timer_started and not st.session_state.timer_frozen:
80
- st.session_state.timer_started = True
81
- html(timer(), height=50)
82
- status_text = st.empty()
83
- progress_bar = st.progress(0)
84
- try:
85
- # Stage 1: Sentiment Analysis
86
- status_text.markdown("**πŸ” Running sentiment analysis...**")
87
- progress_bar.progress(0)
88
- sentiment_result = sentiment_pipe(user_input)
89
- progress_bar.progress(50)
90
- # Stage 2: Generate Report using Gemma
91
- status_text.markdown("**πŸ“ Generating report with Gemma...**")
92
- prompt = f"""
93
- Generate a detailed report based on the following analysis.
94
- Original text:
95
- "{user_input}"
96
- Sentiment analysis result:
97
- {sentiment_result}
98
- Please provide a concise summary report explaining the sentiment and key insights.
99
- """
100
- report = gemma_pipe(prompt, max_length=200)
101
- progress_bar.progress(100)
102
- status_text.success("**βœ… Generation complete!**")
103
- html("<script>localStorage.setItem('freezeTimer', 'true');</script>", height=0)
104
- st.session_state.timer_frozen = True
105
- st.write("**Sentiment Analysis Result:**", sentiment_result)
106
- st.write("**Generated Report:**", report[0]['generated_text'])
107
- except Exception as e:
108
- html("<script>document.getElementById('timer').remove();</script>")
109
- status_text.error(f"**❌ Error:** {str(e)}")
 
 
 
110
  progress_bar.empty()
 
1
+ import os
2
+ import nest_asyncio
3
+ nest_asyncio.apply()
4
+ import streamlit as st
5
+ from sentence_transformers import CrossEncoder
6
+ from transformers import pipeline
7
+ from huggingface_hub import login
8
+ from streamlit.components.v1 import html
9
+ import pandas as pd
10
 
11
  # Retrieve the token from environment variables
12
+ hf_token = os.environ.get("HF_TOKEN")
13
+ if not hf_token:
14
+ st.error("Hugging Face token not found. Please set the HF_TOKEN environment variable.")
15
+ st.stop()
16
 
17
  # Login with the token
18
+ login(token=hf_token)
19
 
20
  # Initialize session state for timer and results
21
+ if 'result' not in st.session_state:
22
+ st.session_state.result = {}
23
+ if 'timer_started' not in st.session_state:
24
+ st.session_state.timer_started = False
25
+ if 'timer_frozen' not in st.session_state:
26
+ st.session_state.timer_frozen = False
27
 
28
  # Timer component using HTML and JavaScript
29
+ def timer():
30
+ return """
31
+ <div id="timer" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>
32
+ <script>
33
+ (function() {
34
+ var start = Date.now();
35
+ var timerElement = document.getElementById('timer');
36
+ localStorage.removeItem("freezeTimer");
37
+ var interval = setInterval(function() {
38
+ if(localStorage.getItem("freezeTimer") === "true"){
39
+ clearInterval(interval);
40
+ timerElement.style.color = '#00cc00';
41
+ return;
42
+ }
43
+ var elapsed = Date.now() - start;
44
+ var minutes = Math.floor(elapsed / 60000);
45
+ var seconds = Math.floor((elapsed % 60000) / 1000);
46
+ timerElement.innerHTML = '⏱️ Elapsed: ' +
47
+ (minutes < 10 ? '0' : '') + minutes + ':' +
48
+ (seconds < 10 ? '0' : '') + seconds;
49
+ }, 1000);
50
+ })();
51
+ </script>
52
+ """
53
 
54
+ st.set_page_config(page_title="Sentiment & Report Generator", page_icon="πŸ“")
55
+ st.header("Sentiment Analysis & Report Generation with Gemma")
56
+
57
+ # Introduction for the Hugging Face interface
58
+ st.write("""
59
+ Welcome to the Sentiment Analysis & Report Generator app!
60
+ This tool leverages Hugging Face's models to analyze the sentiment of your text and generate a detailed report explaining the key insights.
61
+ You can either paste your review text directly into the text area or upload a CSV file containing your reviews.
62
+ """)
63
 
64
  # Load models with caching to avoid reloading on every run
65
+ @st.cache_resource
66
+ def load_models():
67
+ # Load the sentiment model (CrossEncoder) for ranking sentiment labels.
68
+ sentiment_model = CrossEncoder("mixedbread-ai/mxbai-rerank-base-v1")
69
+ # Load the Gemma text generation pipeline.
70
+ gemma_pipe = pipeline("text-generation", model="google/gemma-3-1b-it", use_auth_token=hf_token)
71
+ return sentiment_model, gemma_pipe
72
 
73
+ sentiment_model, gemma_pipe = load_models()
74
 
75
+ # Provide two options for input: file upload (CSV) or text area
76
+ uploaded_file = st.file_uploader("Upload Review File (CSV format)", type=["csv"])
77
  user_input = st.text_area("Or, enter your text for sentiment analysis and report generation:")
78
 
 
79
  if uploaded_file is not None:
80
  try:
81
+ # Read the CSV file; if a column named 'review' exists, use it.
82
+ df = pd.read_csv(uploaded_file)
83
+ if 'review' in df.columns:
84
+ user_input = " ".join(df['review'].astype(str).tolist())
85
+ else:
86
+ # Otherwise, join all text from the first column.
87
+ user_input = " ".join(df.iloc[:, 0].astype(str).tolist())
88
  except Exception as e:
89
  st.error(f"Error reading file: {e}")
90
 
91
+ if st.button("Generate Report"):
92
+ if not user_input.strip():
93
  st.error("Please enter some text!")
94
+ else:
95
+ if not st.session_state.timer_started and not st.session_state.timer_frozen:
96
+ st.session_state.timer_started = True
97
+ html(timer(), height=50)
98
+ status_text = st.empty()
99
+ progress_bar = st.progress(0)
100
+ try:
101
+ # Stage 1: Sentiment Analysis using CrossEncoder ranking
102
+ status_text.markdown("**πŸ” Running sentiment analysis...**")
103
+ progress_bar.progress(0)
104
+ # Use sentiment analysis as ranking over sentiment labels.
105
+ labels = ["positive", "neutral", "negative"]
106
+ sentiment_result = sentiment_model.rank(user_input, labels, return_documents=True, top_k=1)
107
+ progress_bar.progress(50)
108
+
109
+ # Stage 2: Generate Report using Gemma
110
+ status_text.markdown("**πŸ“ Generating report with Gemma...**")
111
+ prompt = f"""
112
+ Generate a detailed report based on the following analysis.
113
+ Original text:
114
+ "{user_input}"
115
+ Sentiment analysis result:
116
+ {sentiment_result}
117
+ Please provide a concise summary report explaining the sentiment and key insights.
118
+ """
119
+ report = gemma_pipe(prompt, max_length=200)
120
+ progress_bar.progress(100)
121
+ status_text.success("**βœ… Generation complete!**")
122
+ html("<script>localStorage.setItem('freezeTimer', 'true');</script>", height=0)
123
+ st.session_state.timer_frozen = True
124
+ st.write("**Sentiment Analysis Result:**", sentiment_result)
125
+ st.write("**Generated Report:**", report[0]['generated_text'])
126
+ except Exception as e:
127
+ html("<script>document.getElementById('timer').remove();</script>")
128
+ status_text.error(f"**❌ Error:** {str(e)}")
129
  progress_bar.empty()