Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -57,8 +57,8 @@ 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 your text by
|
| 61 |
-
|
| 62 |
You can either paste your query text directly into the text area and optionally upload a CSV file containing candidate documents.
|
| 63 |
If no CSV is provided, the query text will be split into sentences to serve as candidate documents.
|
| 64 |
""")
|
|
@@ -66,7 +66,7 @@ If no CSV is provided, the query text will be split into sentences to serve as c
|
|
| 66 |
# Load models with caching to avoid reloading on every run
|
| 67 |
@st.cache_resource
|
| 68 |
def load_models():
|
| 69 |
-
# Load the
|
| 70 |
sentiment_pipe = pipeline("text-classification", model="mixedbread-ai/mxbai-rerank-base-v1")
|
| 71 |
# Load the Gemma text generation pipeline.
|
| 72 |
gemma_pipe = pipeline("text-generation", model="google/gemma-3-1b-it", use_auth_token=hf_token)
|
|
@@ -107,24 +107,28 @@ if st.button("Generate Report"):
|
|
| 107 |
status_text = st.empty()
|
| 108 |
progress_bar = st.progress(0)
|
| 109 |
try:
|
| 110 |
-
# Stage 1:
|
| 111 |
-
status_text.markdown("**🔍
|
| 112 |
progress_bar.progress(0)
|
| 113 |
-
|
| 114 |
-
#
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
progress_bar.progress(50)
|
| 117 |
|
| 118 |
-
# Stage 2: Generate Report using Gemma, using the
|
| 119 |
status_text.markdown("**📝 Generating report with Gemma...**")
|
| 120 |
prompt = f"""
|
| 121 |
Generate a detailed report based on the following analysis.
|
| 122 |
Query:
|
| 123 |
"{query_input}"
|
| 124 |
-
Candidate Documents:
|
| 125 |
-
{
|
| 126 |
-
Reranking Analysis Result (Top 3):
|
| 127 |
-
{results}
|
| 128 |
Please provide a concise summary report explaining the insights derived from this analysis.
|
| 129 |
"""
|
| 130 |
report = gemma_pipe(prompt, max_length=200)
|
|
@@ -132,9 +136,9 @@ Please provide a concise summary report explaining the insights derived from thi
|
|
| 132 |
status_text.success("**✅ Generation complete!**")
|
| 133 |
html("<script>localStorage.setItem('freezeTimer', 'true');</script>", height=0)
|
| 134 |
st.session_state.timer_frozen = True
|
| 135 |
-
st.write("**
|
| 136 |
st.write("**Generated Report:**", report[0]['generated_text'])
|
| 137 |
except Exception as e:
|
| 138 |
html("<script>document.getElementById('timer').remove();</script>")
|
| 139 |
status_text.error(f"**❌ Error:** {str(e)}")
|
| 140 |
-
progress_bar.empty()
|
|
|
|
| 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 your text by scoring candidate documents based on a query.
|
| 61 |
+
The input along with their scores is then used to generate a detailed report explaining key insights.
|
| 62 |
You can either paste your query text directly into the text area and optionally upload a CSV file containing candidate documents.
|
| 63 |
If no CSV is provided, the query text will be split into sentences to serve as candidate documents.
|
| 64 |
""")
|
|
|
|
| 66 |
# Load models with caching to avoid reloading on every run
|
| 67 |
@st.cache_resource
|
| 68 |
def load_models():
|
| 69 |
+
# Load the text-classification pipeline (acting as our scoring model).
|
| 70 |
sentiment_pipe = pipeline("text-classification", model="mixedbread-ai/mxbai-rerank-base-v1")
|
| 71 |
# Load the Gemma text generation pipeline.
|
| 72 |
gemma_pipe = pipeline("text-generation", model="google/gemma-3-1b-it", use_auth_token=hf_token)
|
|
|
|
| 107 |
status_text = st.empty()
|
| 108 |
progress_bar = st.progress(0)
|
| 109 |
try:
|
| 110 |
+
# Stage 1: Score candidate documents without reranking.
|
| 111 |
+
status_text.markdown("**🔍 Scoring candidate documents...**")
|
| 112 |
progress_bar.progress(0)
|
| 113 |
+
|
| 114 |
+
# Create query-document pairs and score each pair.
|
| 115 |
+
scored_docs = []
|
| 116 |
+
for doc in candidate_docs:
|
| 117 |
+
combined_text = f"Query: {query_input} Document: {doc}"
|
| 118 |
+
result = sentiment_pipe(combined_text)[0]
|
| 119 |
+
# Append the document along with its score.
|
| 120 |
+
scored_docs.append((doc, result["score"]))
|
| 121 |
+
|
| 122 |
progress_bar.progress(50)
|
| 123 |
|
| 124 |
+
# Stage 2: Generate Report using Gemma, using the scored candidate documents.
|
| 125 |
status_text.markdown("**📝 Generating report with Gemma...**")
|
| 126 |
prompt = f"""
|
| 127 |
Generate a detailed report based on the following analysis.
|
| 128 |
Query:
|
| 129 |
"{query_input}"
|
| 130 |
+
Candidate Documents with their scores:
|
| 131 |
+
{scored_docs}
|
|
|
|
|
|
|
| 132 |
Please provide a concise summary report explaining the insights derived from this analysis.
|
| 133 |
"""
|
| 134 |
report = gemma_pipe(prompt, max_length=200)
|
|
|
|
| 136 |
status_text.success("**✅ Generation complete!**")
|
| 137 |
html("<script>localStorage.setItem('freezeTimer', 'true');</script>", height=0)
|
| 138 |
st.session_state.timer_frozen = True
|
| 139 |
+
st.write("**Scored Candidate Documents:**", scored_docs)
|
| 140 |
st.write("**Generated Report:**", report[0]['generated_text'])
|
| 141 |
except Exception as e:
|
| 142 |
html("<script>document.getElementById('timer').remove();</script>")
|
| 143 |
status_text.error(f"**❌ Error:** {str(e)}")
|
| 144 |
+
progress_bar.empty()
|