frankai98 commited on
Commit
6c1e788
·
verified ·
1 Parent(s): 91eb9f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -15
app.py CHANGED
@@ -2,22 +2,21 @@ 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:
@@ -25,7 +24,7 @@ if 'timer_started' not in st.session_state:
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>
@@ -57,20 +56,20 @@ st.header("Sentiment Analysis & Report Generation with Gemma")
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"])
@@ -98,12 +97,10 @@ if st.button("Generate Report"):
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
 
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
  import pandas as pd
9
 
10
+ # Retrieve the token from environment variables
11
  hf_token = os.environ.get("HF_TOKEN")
12
  if not hf_token:
13
  st.error("Hugging Face token not found. Please set the HF_TOKEN environment variable.")
14
  st.stop()
15
 
16
+ # Login with the token
17
  login(token=hf_token)
18
 
19
+ # Initialize session state for timer and results
20
  if 'result' not in st.session_state:
21
  st.session_state.result = {}
22
  if 'timer_started' not in st.session_state:
 
24
  if 'timer_frozen' not in st.session_state:
25
  st.session_state.timer_frozen = False
26
 
27
+ # Timer component using HTML and JavaScript
28
  def timer():
29
  return """
30
  <div id="timer" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>
 
56
  # Introduction for the Hugging Face interface
57
  st.write("""
58
  Welcome to the Sentiment Analysis & Report Generator app!
59
+ This tool leverages Hugging Faces models to analyze the sentiment of your text and generate a detailed report explaining the key insights.
60
  You can either paste your review text directly into the text area or upload a CSV file containing your reviews.
61
  """)
62
 
63
+ # Load models with caching to avoid reloading on every run
64
  @st.cache_resource
65
  def load_models():
66
+ # Load the sentiment analysis model via pipeline.
67
+ sentiment_pipe = pipeline("text-classification", model="mixedbread-ai/mxbai-rerank-base-v1")
68
  # Load the Gemma text generation pipeline.
69
  gemma_pipe = pipeline("text-generation", model="google/gemma-3-1b-it", use_auth_token=hf_token)
70
+ return sentiment_pipe, gemma_pipe
71
 
72
+ sentiment_pipe, gemma_pipe = load_models()
73
 
74
  # Provide two options for input: file upload (CSV) or text area
75
  uploaded_file = st.file_uploader("Upload Review File (CSV format)", type=["csv"])
 
97
  status_text = st.empty()
98
  progress_bar = st.progress(0)
99
  try:
100
+ # Stage 1: Sentiment Analysis using pipeline
101
  status_text.markdown("**🔍 Running sentiment analysis...**")
102
  progress_bar.progress(0)
103
+ sentiment_result = sentiment_pipe(user_input)
 
 
104
  progress_bar.progress(50)
105
 
106
  # Stage 2: Generate Report using Gemma