Update app.py
Browse files
app.py
CHANGED
@@ -26,19 +26,6 @@ model_options = {
|
|
26 |
# ้กต้ข้
็ฝฎ
|
27 |
st.set_page_config(page_title="Emoji Offensive Text Detector", page_icon="๐จ", layout="wide")
|
28 |
|
29 |
-
# ้กต้ขๅธๅฑ
|
30 |
-
with st.sidebar:
|
31 |
-
st.header("๐ง Navigation")
|
32 |
-
section = st.radio("Select Mode:", ["๐ Text Moderation", "๐ Text Analysis"])
|
33 |
-
|
34 |
-
if section == "๐ Text Moderation":
|
35 |
-
selected_model = st.selectbox("Choose classification model", list(model_options.keys()))
|
36 |
-
selected_model_id = model_options[selected_model]
|
37 |
-
classifier = pipeline("text-classification", model=selected_model_id, device=0 if torch.cuda.is_available() else -1)
|
38 |
-
|
39 |
-
elif section == "๐ Text Analysis":
|
40 |
-
st.markdown("You can view editing suggestions based on past analyses.")
|
41 |
-
|
42 |
# ๅๅงๅๅๅฒ่ฎฐๅฝ
|
43 |
if "history" not in st.session_state:
|
44 |
st.session_state.history = []
|
@@ -60,14 +47,21 @@ def classify_emoji_text(text: str):
|
|
60 |
st.session_state.history.append({"text": text, "translated": translated_text, "label": label, "score": score, "reason": reasoning})
|
61 |
return translated_text, label, score, reasoning
|
62 |
|
63 |
-
#
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
with st.spinner("๐ Processing..."):
|
72 |
try:
|
73 |
translated, label, score, reason = classify_emoji_text(text)
|
@@ -78,48 +72,44 @@ if section == "๐ Text Moderation":
|
|
78 |
st.markdown(f"### ๐ Confidence Score: `{score:.2%}`")
|
79 |
st.markdown("### ๐ง Model Explanation:")
|
80 |
st.info(reason)
|
81 |
-
|
82 |
except Exception as e:
|
83 |
-
st.error(f"โ
|
84 |
|
85 |
-
|
86 |
-
st.markdown("### ๐ผ๏ธ Or upload a screenshot
|
87 |
-
|
88 |
-
uploaded_file
|
89 |
-
|
90 |
-
if uploaded_file is not None:
|
91 |
image = Image.open(uploaded_file)
|
92 |
-
st.image(image, caption="Uploaded
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
st.
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
st.markdown("
|
101 |
-
st.
|
102 |
-
|
103 |
-
st.markdown(f"### ๐ฏ Prediction: `{label}`")
|
104 |
-
st.markdown(f"### ๐ Confidence Score: `{score:.2%}`")
|
105 |
-
st.markdown("### ๐ง Model Explanation:")
|
106 |
st.info(reason)
|
107 |
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
|
|
|
26 |
# ้กต้ข้
็ฝฎ
|
27 |
st.set_page_config(page_title="Emoji Offensive Text Detector", page_icon="๐จ", layout="wide")
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
# ๅๅงๅๅๅฒ่ฎฐๅฝ
|
30 |
if "history" not in st.session_state:
|
31 |
st.session_state.history = []
|
|
|
47 |
st.session_state.history.append({"text": text, "translated": translated_text, "label": label, "score": score, "reason": reasoning})
|
48 |
return translated_text, label, score, reasoning
|
49 |
|
50 |
+
# ้กต้ขๅธๅฑ
|
51 |
+
st.sidebar.header("๐ง Settings")
|
52 |
+
selected_model = st.sidebar.selectbox("Choose classification model", list(model_options.keys()))
|
53 |
+
selected_model_id = model_options[selected_model]
|
54 |
+
classifier = pipeline("text-classification", model=selected_model_id, device=0 if torch.cuda.is_available() else -1)
|
55 |
+
|
56 |
+
# ไธป้กต้ข๏ผ้ๆ Text Moderation ๅ Text Analysis
|
57 |
+
st.title("๐จ Emoji Offensive Text Detector & Violation Analysis")
|
58 |
+
|
59 |
+
# ่พๅ
ฅไธๅ็ฑป
|
60 |
+
st.markdown("## โ๏ธ ่พๅ
ฅๆไธไผ ๆๆฌ่ฟ่กๅ็ฑป")
|
61 |
+
col1, col2 = st.columns([2,1])
|
62 |
+
with col1:
|
63 |
+
text = st.text_area("Enter sentence with emojis:", value="ไฝ ๆฏ๐ท", height=150)
|
64 |
+
if st.button("๐ฆ Analyze Text"):
|
65 |
with st.spinner("๐ Processing..."):
|
66 |
try:
|
67 |
translated, label, score, reason = classify_emoji_text(text)
|
|
|
72 |
st.markdown(f"### ๐ Confidence Score: `{score:.2%}`")
|
73 |
st.markdown("### ๐ง Model Explanation:")
|
74 |
st.info(reason)
|
|
|
75 |
except Exception as e:
|
76 |
+
st.error(f"โ Error during processing: {e}")
|
77 |
|
78 |
+
with col2:
|
79 |
+
st.markdown("### ๐ผ๏ธ Or upload a screenshot:")
|
80 |
+
uploaded_file = st.file_uploader("Image (JPG/PNG)", type=["jpg","png","jpeg"])
|
81 |
+
if uploaded_file:
|
|
|
|
|
82 |
image = Image.open(uploaded_file)
|
83 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
84 |
+
with st.spinner("๐ง Running OCR..."):
|
85 |
+
ocr_text = pytesseract.image_to_string(image, lang="chi_sim+eng").strip()
|
86 |
+
st.markdown("#### ๐ OCR Extracted Text:")
|
87 |
+
st.code(ocr_text)
|
88 |
+
translated, label, score, reason = classify_emoji_text(ocr_text)
|
89 |
+
st.markdown("#### ๐ Translated:")
|
90 |
+
st.code(translated)
|
91 |
+
st.markdown(f"#### ๐ฏ Prediction: `{label}`")
|
92 |
+
st.markdown(f"#### ๐ Confidence: `{score:.2%}`")
|
93 |
+
st.markdown("#### ๐ง Explanation:")
|
|
|
|
|
|
|
94 |
st.info(reason)
|
95 |
|
96 |
+
st.markdown("---")
|
97 |
+
|
98 |
+
# ่ฟ่งๅๆไปช่กจ็
|
99 |
+
st.markdown("## ๐ Violation Analysis Dashboard")
|
100 |
+
if st.session_state.history:
|
101 |
+
df = pd.DataFrame(st.session_state.history)
|
102 |
+
st.markdown("### ๐งพ ๅๅฒ่ฎฐๅฝ่ฏฆๆ
")
|
103 |
+
for item in st.session_state.history:
|
104 |
+
st.markdown(f"- ๐น **่พๅ
ฅ:** `{item['text']}` | **Label:** `{item['label']}` | **Confidence:** `{item['score']:.2%}`")
|
105 |
+
st.markdown(f" - **Translated:** `{item['translated']}`")
|
106 |
+
st.markdown(f" - **Suggestion:** {item['reason']}")
|
107 |
+
|
108 |
+
radar_df = pd.DataFrame({
|
109 |
+
"Category": ["Insult","Abuse","Discrimination","Hate Speech","Vulgarity"],
|
110 |
+
"Score": [0.7,0.4,0.3,0.5,0.6]
|
111 |
+
})
|
112 |
+
radar_fig = px.line_polar(radar_df, r='Score', theta='Category', line_close=True, title="โ ๏ธ Risk Radar by Category")
|
113 |
+
st.plotly_chart(radar_fig)
|
114 |
+
else:
|
115 |
+
st.info("โ ๏ธ No data available. Please analyze some text first.")
|