frankai98 commited on
Commit
5ab1c08
Β·
verified Β·
1 Parent(s): 0a0aca8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -20
app.py CHANGED
@@ -1,18 +1,20 @@
 
1
  import nest_asyncio
2
  nest_asyncio.apply()
3
 
4
  import streamlit as st
5
- import asyncio
6
- import subprocess
7
  from transformers import pipeline
 
8
  from streamlit.components.v1 import html
9
 
10
- # -----------------------------------------------------------
11
- # Hugging Face CLI Login
12
- # This will prompt you in the terminal for your Hugging Face token.
13
- # Ensure you have your token ready.
14
- subprocess.run(["huggingface-cli", "login"], shell=True)
15
- # -----------------------------------------------------------
 
 
16
 
17
  # Initialize session state for timer and results
18
  if 'result' not in st.session_state:
@@ -30,10 +32,8 @@ def timer():
30
  (function() {
31
  var start = Date.now();
32
  var timerElement = document.getElementById('timer');
33
- // Remove any existing freeze flag
34
  localStorage.removeItem("freezeTimer");
35
  var interval = setInterval(function() {
36
- // If freeze flag is set, stop the timer
37
  if(localStorage.getItem("freezeTimer") === "true"){
38
  clearInterval(interval);
39
  timerElement.style.color = '#00cc00';
@@ -57,7 +57,8 @@ st.header("Sentiment Analysis & Report Generation with Gemma")
57
  @st.cache_resource
58
  def load_models():
59
  sentiment_pipe = pipeline("text-classification", model="mixedbread-ai/mxbai-rerank-base-v1")
60
- gemma_pipe = pipeline("text-generation", model="google/gemma-3-1b-it")
 
61
  return sentiment_pipe, gemma_pipe
62
 
63
  sentiment_pipe, gemma_pipe = load_models()
@@ -69,27 +70,22 @@ if st.button("Generate Report"):
69
  if not user_input.strip():
70
  st.error("Please enter some text!")
71
  else:
72
- # Start the timer if not already started
73
  if not st.session_state.timer_started and not st.session_state.timer_frozen:
74
  st.session_state.timer_started = True
75
  html(timer(), height=50)
76
 
77
- # Initialize status message and progress bar
78
  status_text = st.empty()
79
  progress_bar = st.progress(0)
80
 
81
  try:
82
- # ---------------------------
83
  # Stage 1: Sentiment Analysis
84
  status_text.markdown("**πŸ” Running sentiment analysis...**")
85
  progress_bar.progress(0)
86
  sentiment_result = sentiment_pipe(user_input)
87
  progress_bar.progress(50)
88
 
89
- # ---------------------------
90
  # Stage 2: Generate Report using Gemma
91
  status_text.markdown("**πŸ“ Generating report with Gemma...**")
92
- # Build a prompt that includes the original text and the sentiment analysis result.
93
  prompt = f"""
94
  Generate a detailed report based on the following analysis.
95
 
@@ -104,17 +100,13 @@ Please provide a concise summary report explaining the sentiment and key insight
104
  report = gemma_pipe(prompt, max_length=200)
105
  progress_bar.progress(100)
106
  status_text.success("**βœ… Generation complete!**")
107
-
108
- # Freeze the timer immediately upon completion.
109
  html("<script>localStorage.setItem('freezeTimer', 'true');</script>", height=0)
110
  st.session_state.timer_frozen = True
111
 
112
- # Display the results
113
  st.write("**Sentiment Analysis Result:**", sentiment_result)
114
  st.write("**Generated Report:**", report[0]['generated_text'])
115
 
116
  except Exception as e:
117
- # Remove timer element if an error occurs
118
  html("<script>document.getElementById('timer').remove();</script>")
119
  status_text.error(f"**❌ Error:** {str(e)}")
120
  progress_bar.empty()
 
1
+ import os
2
  import nest_asyncio
3
  nest_asyncio.apply()
4
 
5
  import streamlit as st
 
 
6
  from transformers import pipeline
7
+ from huggingface_hub import login
8
  from streamlit.components.v1 import html
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:
 
32
  (function() {
33
  var start = Date.now();
34
  var timerElement = document.getElementById('timer');
 
35
  localStorage.removeItem("freezeTimer");
36
  var interval = setInterval(function() {
 
37
  if(localStorage.getItem("freezeTimer") === "true"){
38
  clearInterval(interval);
39
  timerElement.style.color = '#00cc00';
 
57
  @st.cache_resource
58
  def load_models():
59
  sentiment_pipe = pipeline("text-classification", model="mixedbread-ai/mxbai-rerank-base-v1")
60
+ # Pass the token to the Gemma pipeline
61
+ gemma_pipe = pipeline("text-generation", model="google/gemma-3-1b-it", use_auth_token=hf_token)
62
  return sentiment_pipe, gemma_pipe
63
 
64
  sentiment_pipe, gemma_pipe = load_models()
 
70
  if not user_input.strip():
71
  st.error("Please enter some text!")
72
  else:
 
73
  if not st.session_state.timer_started and not st.session_state.timer_frozen:
74
  st.session_state.timer_started = True
75
  html(timer(), height=50)
76
 
 
77
  status_text = st.empty()
78
  progress_bar = st.progress(0)
79
 
80
  try:
 
81
  # Stage 1: Sentiment Analysis
82
  status_text.markdown("**πŸ” Running sentiment analysis...**")
83
  progress_bar.progress(0)
84
  sentiment_result = sentiment_pipe(user_input)
85
  progress_bar.progress(50)
86
 
 
87
  # Stage 2: Generate Report using Gemma
88
  status_text.markdown("**πŸ“ Generating report with Gemma...**")
 
89
  prompt = f"""
90
  Generate a detailed report based on the following analysis.
91
 
 
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
 
 
106
  st.write("**Sentiment Analysis Result:**", sentiment_result)
107
  st.write("**Generated Report:**", report[0]['generated_text'])
108
 
109
  except Exception as e:
 
110
  html("<script>document.getElementById('timer').remove();</script>")
111
  status_text.error(f"**❌ Error:** {str(e)}")
112
  progress_bar.empty()