Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +71 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,73 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
"""
|
| 7 |
-
# Welcome to Streamlit!
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
# ====================== 页面配置 ======================
|
| 7 |
+
st.set_page_config(
|
| 8 |
+
page_title="Manipulative Language Detector",
|
| 9 |
+
page_icon="🧠",
|
| 10 |
+
layout="wide"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# ====================== 模型加载 ======================
|
| 14 |
+
@st.cache_resource
|
| 15 |
+
def load_model():
|
| 16 |
+
model_name = "LilithHu/mbert-manipulative-detector"
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 18 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 19 |
+
model.eval()
|
| 20 |
+
return tokenizer, model
|
| 21 |
+
|
| 22 |
+
tokenizer, model = load_model()
|
| 23 |
+
|
| 24 |
+
# ====================== 多语言支持 ======================
|
| 25 |
+
lang = st.sidebar.selectbox("Language / 语言", ["English", "中文"])
|
| 26 |
+
st.sidebar.markdown("---")
|
| 27 |
+
|
| 28 |
+
# ====================== 标题 ======================
|
| 29 |
+
if lang == "English":
|
| 30 |
+
st.title("🧠 Manipulative Language Detector")
|
| 31 |
+
st.markdown("This tool uses an AI model to detect manipulative language in messages.")
|
| 32 |
+
else:
|
| 33 |
+
st.title("🧠 情感操控语言识别器")
|
| 34 |
+
st.markdown("本工具使用 AI 模型检测文本中的情感操控语言。")
|
| 35 |
+
|
| 36 |
+
st.markdown("---")
|
| 37 |
+
|
| 38 |
+
# ====================== 用户输入 ======================
|
| 39 |
+
user_text = st.text_area("Enter your message / 输入文本", height=150)
|
| 40 |
+
|
| 41 |
+
# ====================== 推理函数 ======================
|
| 42 |
+
def predict(text):
|
| 43 |
+
inputs = tokenizer(text, truncation=True, padding='max_length', max_length=128, return_tensors='pt')
|
| 44 |
+
with torch.no_grad():
|
| 45 |
+
outputs = model(**inputs)
|
| 46 |
+
logits = outputs.logits
|
| 47 |
+
predicted_class = torch.argmax(logits, dim=1).item()
|
| 48 |
+
return predicted_class
|
| 49 |
+
|
| 50 |
+
# ====================== 按钮 & 结果显示 ======================
|
| 51 |
+
if st.button("🔍 Analyze / 分析"):
|
| 52 |
+
if not user_text.strip():
|
| 53 |
+
st.warning("⚠️ Please enter a message!" if lang == "English" else "⚠️ 请输入内容!")
|
| 54 |
+
else:
|
| 55 |
+
with st.spinner("Analyzing..." if lang == "English" else "分析中..."):
|
| 56 |
+
try:
|
| 57 |
+
prediction = predict(user_text)
|
| 58 |
+
label = "manipulative" if prediction == 1 else "non-manipulative"
|
| 59 |
+
|
| 60 |
+
if label == "manipulative":
|
| 61 |
+
st.error("⚠️ Manipulative Language Detected!" if lang == "English" else "⚠️ 检测到操纵性语言!")
|
| 62 |
+
else:
|
| 63 |
+
st.success("✅ No manipulative language detected." if lang == "English" else "✅ 未检测到操纵性语言。")
|
| 64 |
+
except Exception as e:
|
| 65 |
+
st.error(f"❌ Error: {e}")
|
| 66 |
+
|
| 67 |
+
# ====================== 页脚 ======================
|
| 68 |
+
st.markdown("---")
|
| 69 |
+
st.markdown(
|
| 70 |
+
"<p style='text-align: center; color: #888;'>© 2025 Manipulative Language Detector</p>",
|
| 71 |
+
unsafe_allow_html=True
|
| 72 |
+
)
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|