aeresd commited on
Commit
aa53269
ยท
verified ยท
1 Parent(s): 68c3cca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -61
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
- if section == "๐Ÿ“ Text Moderation":
65
- st.title("๐Ÿ“ Offensive Text Classification")
66
- st.markdown("### โœ๏ธ Input your sentence:")
67
- default_text = "ไฝ ๆ˜ฏ๐Ÿท"
68
- text = st.text_area("Enter sentence with emojis:", value=default_text, height=150)
69
-
70
- if st.button("๐Ÿšฆ Analyze"):
 
 
 
 
 
 
 
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"โŒ An error occurred during processing:\n\n{e}")
84
 
85
- st.markdown("---")
86
- st.markdown("### ๐Ÿ–ผ๏ธ Or upload a screenshot of bullet comments:")
87
-
88
- uploaded_file = st.file_uploader("Upload an image (JPG/PNG)", type=["jpg", "jpeg", "png"])
89
-
90
- if uploaded_file is not None:
91
  image = Image.open(uploaded_file)
92
- st.image(image, caption="Uploaded Screenshot", use_column_width=True)
93
-
94
- with st.spinner("๐Ÿง  Extracting text via OCR..."):
95
- ocr_text = pytesseract.image_to_string(image, lang="chi_sim+eng")
96
- st.markdown("#### ๐Ÿ“‹ Extracted Text:")
97
- st.code(ocr_text.strip())
98
-
99
- translated, label, score, reason = classify_emoji_text(ocr_text.strip())
100
- st.markdown("### ๐Ÿ”„ Translated sentence:")
101
- st.code(translated, language="text")
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
- elif section == "๐Ÿ“Š Text Analysis":
109
- st.title("๐Ÿ“Š Violation Analysis Dashboard")
110
- if st.session_state.history:
111
- st.markdown("### ๐Ÿงพ Offensive Terms & Suggestions")
112
- for item in st.session_state.history:
113
- st.markdown(f"- ๐Ÿ”น **Input:** `{item['text']}`")
114
- st.markdown(f" - โœจ **Translated:** `{item['translated']}`")
115
- st.markdown(f" - โ— **Label:** `{item['label']}` with **{item['score']:.2%}** confidence")
116
- st.markdown(f" - ๐Ÿ”ง **Suggestion:** {item['reason']}")
117
-
118
- radar_df = pd.DataFrame({
119
- "Category": ["Insult", "Abuse", "Discrimination", "Hate Speech", "Vulgarity"],
120
- "Score": [0.7, 0.4, 0.3, 0.5, 0.6]
121
- })
122
- radar_fig = px.line_polar(radar_df, r='Score', theta='Category', line_close=True, title="โš ๏ธ Risk Radar by Category")
123
- st.plotly_chart(radar_fig)
124
- else:
125
- st.info("โš ๏ธ No classification data available yet.")
 
 
 
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.")