Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from functools import lru_cache
|
| 4 |
-
import math
|
| 5 |
|
| 6 |
DEFAULT_LABELS = [
|
| 7 |
"finance", "sports", "tech", "politics", "health", "entertainment",
|
|
@@ -18,9 +17,11 @@ def get_pipes():
|
|
| 18 |
"zero-shot-classification",
|
| 19 |
model="valhalla/distilbart-mnli-12-1"
|
| 20 |
)
|
|
|
|
| 21 |
sentiment = pipeline(
|
| 22 |
"sentiment-analysis",
|
| 23 |
-
model="
|
|
|
|
| 24 |
)
|
| 25 |
return summarizer, zshot, sentiment
|
| 26 |
|
|
@@ -80,19 +81,34 @@ def classify_topics(text: str, labels: list[str]):
|
|
| 80 |
|
| 81 |
|
| 82 |
def analyze_sentiment(text: str):
|
|
|
|
| 83 |
_, _, sentiment = get_pipes()
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
|
| 88 |
def analyze(text, labels_csv, summary_words):
|
| 89 |
text = (text or "").strip()
|
| 90 |
if not text:
|
| 91 |
return (
|
| 92 |
-
"",
|
| 93 |
[], # table rows
|
| 94 |
-
"",
|
| 95 |
-
"",
|
| 96 |
0.0, # sentiment score
|
| 97 |
)
|
| 98 |
|
|
@@ -113,14 +129,14 @@ def analyze(text, labels_csv, summary_words):
|
|
| 113 |
return summary, table_rows, top_str, sent_label, sent_score
|
| 114 |
|
| 115 |
|
| 116 |
-
with gr.Blocks(title="Text Insight Stack", css="""
|
| 117 |
:root{--radius:16px}
|
| 118 |
.header {font-size: 28px; font-weight: 800;}
|
| 119 |
.subtle {opacity:.8}
|
| 120 |
.card {border:1px solid #e5e7eb; border-radius: var(--radius); padding:16px}
|
| 121 |
""") as demo:
|
| 122 |
gr.Markdown("""
|
| 123 |
-
<div class="header">🧠 Text Insight Stack</div>
|
| 124 |
<div class="subtle">Summarize • Topic Classify • Sentiment — powered by three open models on Hugging Face</div>
|
| 125 |
""")
|
| 126 |
|
|
@@ -133,7 +149,7 @@ with gr.Blocks(title="Text Insight Stack", css="""
|
|
| 133 |
elem_classes=["card"],
|
| 134 |
)
|
| 135 |
labels = gr.Textbox(
|
| 136 |
-
label="Candidate topic labels (comma
|
| 137 |
value=", ".join(DEFAULT_LABELS),
|
| 138 |
elem_classes=["card"],
|
| 139 |
)
|
|
@@ -151,13 +167,15 @@ with gr.Blocks(title="Text Insight Stack", css="""
|
|
| 151 |
out_table = gr.Dataframe(headers=["label", "score"], datatype=["str", "number"], interactive=False)
|
| 152 |
out_top = gr.Markdown()
|
| 153 |
with gr.Tab("Sentiment"):
|
| 154 |
-
|
|
|
|
| 155 |
out_sent_score = gr.Number(label="Confidence score")
|
| 156 |
|
| 157 |
-
|
| 158 |
label="Try an example",
|
| 159 |
examples=[[
|
| 160 |
-
"Open-source models are transforming AI by enabling broad access to powerful capabilities. However, organizations must balance innovation with governance, ensuring that safety and compliance keep pace with deployment. This article explores how companies can adopt a pragmatic approach to evaluation, monitoring, and human oversight while still benefiting from the speed of open development."
|
|
|
|
| 161 |
inputs=[txt]
|
| 162 |
)
|
| 163 |
|
|
@@ -168,4 +186,5 @@ with gr.Blocks(title="Text Insight Stack", css="""
|
|
| 168 |
)
|
| 169 |
|
| 170 |
if __name__ == "__main__":
|
| 171 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from functools import lru_cache
|
|
|
|
| 4 |
|
| 5 |
DEFAULT_LABELS = [
|
| 6 |
"finance", "sports", "tech", "politics", "health", "entertainment",
|
|
|
|
| 17 |
"zero-shot-classification",
|
| 18 |
model="valhalla/distilbart-mnli-12-1"
|
| 19 |
)
|
| 20 |
+
# 3-class sentiment: NEGATIVE / NEUTRAL / POSITIVE
|
| 21 |
sentiment = pipeline(
|
| 22 |
"sentiment-analysis",
|
| 23 |
+
model="cardiffnlp/twitter-roberta-base-sentiment-latest",
|
| 24 |
+
tokenizer="cardiffnlp/twitter-roberta-base-sentiment-latest"
|
| 25 |
)
|
| 26 |
return summarizer, zshot, sentiment
|
| 27 |
|
|
|
|
| 81 |
|
| 82 |
|
| 83 |
def analyze_sentiment(text: str):
|
| 84 |
+
"""3-class sentiment with chunk-aware averaging for long inputs."""
|
| 85 |
_, _, sentiment = get_pipes()
|
| 86 |
+
# Smaller chunk for sentiment; keep first few for speed
|
| 87 |
+
s_chunks = chunk_text(text, max_chars=300) or [text[:300]]
|
| 88 |
+
s_chunks = s_chunks[:8]
|
| 89 |
+
|
| 90 |
+
agg = {"NEGATIVE": 0.0, "NEUTRAL": 0.0, "POSITIVE": 0.0}
|
| 91 |
+
for ch in s_chunks:
|
| 92 |
+
scores = sentiment(ch, return_all_scores=True)[0]
|
| 93 |
+
for s in scores:
|
| 94 |
+
agg[s["label"].upper()] += float(s["score"])
|
| 95 |
+
n = float(len(s_chunks))
|
| 96 |
+
for k in agg:
|
| 97 |
+
agg[k] /= n
|
| 98 |
+
|
| 99 |
+
label = max(agg, key=agg.get)
|
| 100 |
+
score = agg[label]
|
| 101 |
+
return label, score
|
| 102 |
|
| 103 |
|
| 104 |
def analyze(text, labels_csv, summary_words):
|
| 105 |
text = (text or "").strip()
|
| 106 |
if not text:
|
| 107 |
return (
|
| 108 |
+
"", # summary
|
| 109 |
[], # table rows
|
| 110 |
+
"", # top topics string
|
| 111 |
+
"", # sentiment label
|
| 112 |
0.0, # sentiment score
|
| 113 |
)
|
| 114 |
|
|
|
|
| 129 |
return summary, table_rows, top_str, sent_label, sent_score
|
| 130 |
|
| 131 |
|
| 132 |
+
with gr.Blocks(title="TriScope — Text Insight Stack", css="""
|
| 133 |
:root{--radius:16px}
|
| 134 |
.header {font-size: 28px; font-weight: 800;}
|
| 135 |
.subtle {opacity:.8}
|
| 136 |
.card {border:1px solid #e5e7eb; border-radius: var(--radius); padding:16px}
|
| 137 |
""") as demo:
|
| 138 |
gr.Markdown("""
|
| 139 |
+
<div class="header">🧠 TriScope — Text Insight Stack</div>
|
| 140 |
<div class="subtle">Summarize • Topic Classify • Sentiment — powered by three open models on Hugging Face</div>
|
| 141 |
""")
|
| 142 |
|
|
|
|
| 149 |
elem_classes=["card"],
|
| 150 |
)
|
| 151 |
labels = gr.Textbox(
|
| 152 |
+
label="Candidate topic labels (comma-separated)",
|
| 153 |
value=", ".join(DEFAULT_LABELS),
|
| 154 |
elem_classes=["card"],
|
| 155 |
)
|
|
|
|
| 167 |
out_table = gr.Dataframe(headers=["label", "score"], datatype=["str", "number"], interactive=False)
|
| 168 |
out_top = gr.Markdown()
|
| 169 |
with gr.Tab("Sentiment"):
|
| 170 |
+
# Show 3 classes
|
| 171 |
+
out_sent_label = gr.Label(num_top_classes=3)
|
| 172 |
out_sent_score = gr.Number(label="Confidence score")
|
| 173 |
|
| 174 |
+
gr.Examples(
|
| 175 |
label="Try an example",
|
| 176 |
examples=[[
|
| 177 |
+
"Open-source models are transforming AI by enabling broad access to powerful capabilities. However, organizations must balance innovation with governance, ensuring that safety and compliance keep pace with deployment. This article explores how companies can adopt a pragmatic approach to evaluation, monitoring, and human oversight while still benefiting from the speed of open development."
|
| 178 |
+
]],
|
| 179 |
inputs=[txt]
|
| 180 |
)
|
| 181 |
|
|
|
|
| 186 |
)
|
| 187 |
|
| 188 |
if __name__ == "__main__":
|
| 189 |
+
# Helpful for Spaces; enables logs and proper binding
|
| 190 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, debug=True)
|