import os
import streamlit as st
from dotenv import load_dotenv
from huggingface_hub import InferenceClient
load_dotenv()
ENDPOINT_URL = os.environ.get("HF_ENDPOINT_URL")
st.set_page_config(
page_title="Thai Fake News Detector",
page_icon="🔍",
layout="centered",
)
# ── Premium Dark Theme CSS with Animations ────────────────────────────────────
st.markdown("""
""", unsafe_allow_html=True)
# ── Sidebar ──────────────────────────────────────────────────────────────────
with st.sidebar:
st.markdown("""
""", unsafe_allow_html=True)
st.markdown("""
Project Links
Training Dataset
6,004
Thai news articles (2017-2024)
from Antifakenewscenter Thailand
""", unsafe_allow_html=True)
# ── Header ────────────────────────────────────────────────────────────────────
st.markdown('ตรวจสอบข่าวปลอม
', unsafe_allow_html=True)
st.markdown('Thai Fake News Detector · Powered by Qwen2.5-1.5B
', unsafe_allow_html=True)
st.markdown("""
โมเดลนี้ใช้ Qwen2.5-1.5B ที่ผ่านการ fine-tune
บนชุดข้อมูลข่าวภาษาไทย 6,004 บทความ เพื่อจำแนกว่าข่าวนั้นเป็น
ข่าวจริง หรือ
ข่าวปลอม
""", unsafe_allow_html=True)
# ── Input ─────────────────────────────────────────────────────────────────────
if "news_input" not in st.session_state:
st.session_state.news_input = ""
news_input = st.text_area(
"ใส่หัวข้อข่าวหรือเนื้อหาข่าวที่ต้องการตรวจสอบ",
placeholder="เช่น: รัฐบาลแจกเงินคนละ 10,000 บาท ผ่านแอปเป๋าตัง",
height=160,
key="news_input",
)
run = st.button("ตรวจสอบข่าว", type="primary", use_container_width=True)
# ── Inference ─────────────────────────────────────────────────────────────────
if run:
if not news_input.strip():
st.warning("กรุณาใส่ข้อความข่าวก่อนตรวจสอบ")
else:
loading_placeholder = st.empty()
loading_placeholder.markdown("""
""", unsafe_allow_html=True)
try:
client = InferenceClient(base_url=ENDPOINT_URL, token=os.environ.get("HF_TOKEN"))
response = client.text_generation(
news_input.strip(),
max_new_tokens=20,
temperature=0.05,
do_sample=True,
)
loading_placeholder.empty()
generated = response.split("### Response:")[-1].strip()
st.markdown('', unsafe_allow_html=True)
st.markdown('ผลการตรวจสอบ
', unsafe_allow_html=True)
if "จริง" in generated:
st.markdown("""
✓
ข่าวจริง
โมเดลประเมินว่าข่าวนี้มีแนวโน้มเป็น ข่าวจริง
""", unsafe_allow_html=True)
elif "ปลอม" in generated:
st.markdown("""
✗
ข่าวปลอม
โมเดลประเมินว่าข่าวนี้มีแนวโน้มเป็น ข่าวปลอม
""", unsafe_allow_html=True)
else:
st.markdown("""
?
ไม่สามารถวิเคราะห์ได้
โมเดลไม่สามารถระบุได้ว่าเป็นข่าวจริงหรือข่าวปลอม
""", unsafe_allow_html=True)
with st.expander("ดูผลลัพธ์ดิบจากโมเดล"):
st.code(response, language=None)
except Exception as exc:
loading_placeholder.empty()
st.error(f"เกิดข้อผิดพลาด: {exc}")
st.markdown("""
แนวทางแก้ไข:
• ตรวจสอบการเชื่อมต่ออินเทอร์เน็ต
• โมเดลอาจยังไม่พร้อมใช้งานผ่าน Inference API
• ลองเปิด HF Space แทน
""", unsafe_allow_html=True)
# ── Footer ────────────────────────────────────────────────────────────────────
st.markdown("""
""", unsafe_allow_html=True)