Therdpoom commited on
Commit
3e48aa0
·
verified ·
1 Parent(s): 2f10d89

Upload 2 files

Browse files
Files changed (2) hide show
  1. Dockerfile +17 -0
  2. README.md +138 -0
Dockerfile ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.12-slim
2
+
3
+ # HuggingFace Spaces กำหนดให้รันด้วย non-root user UID 1000
4
+ RUN useradd -m -u 1000 user
5
+ USER user
6
+ ENV PATH="/home/user/.local/bin:$PATH"
7
+
8
+ WORKDIR /app
9
+
10
+ COPY --chown=user requirements.txt .
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ COPY --chown=user . .
14
+
15
+ EXPOSE 8000
16
+
17
+ CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
README.md ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: line-webhook
3
+ emoji: 🤖
4
+ colorFrom: green
5
+ colorTo: blue
6
+ sdk: docker
7
+ app_port: 8000
8
+ pinned: false
9
+ ---
10
+
11
+ # LINE Webhook → HuggingFace Sentiment Server
12
+
13
+ รับ message event จาก LINE Official Account → วิเคราะห์ sentiment → ส่งต่อให้ Lovable AI Dashboard
14
+
15
+ ---
16
+
17
+ ## Architecture
18
+
19
+ ```
20
+ LINE OA → Webhook Server → HuggingFace Inference API
21
+ ↓ ↓
22
+ PostgreSQL ←─────── sentiment result
23
+
24
+ Redis Pub/Sub
25
+
26
+ Lovable AI Dashboard (real-time)
27
+ ```
28
+
29
+ ---
30
+
31
+ ## Quick Start
32
+
33
+ ### 1. Clone & ติดตั้ง
34
+
35
+ ```bash
36
+ git clone <repo>
37
+ cd line-webhook
38
+ cp .env.example .env # แก้ไขค่าใน .env
39
+ ```
40
+
41
+ ### 2. ใส่ค่าใน .env
42
+
43
+ ```env
44
+ LINE_CHANNEL_SECRET=<จาก LINE Developers Console>
45
+ LINE_CHANNEL_ACCESS_TOKEN=<จาก LINE Developers Console>
46
+ HF_API_TOKEN=<จาก huggingface.co/settings/tokens>
47
+ ```
48
+
49
+ ### 3. รันด้วย Docker Compose
50
+
51
+ ```bash
52
+ docker compose up -d
53
+ ```
54
+
55
+ Server พร้อมที่ `http://localhost:8000`
56
+
57
+ ### 4. รัน Local (ไม่ใช้ Docker)
58
+
59
+ ```bash
60
+ python -m venv venv
61
+ source venv/bin/activate # Windows: venv\Scripts\activate
62
+ pip install -r requirements.txt
63
+ uvicorn app.main:app --reload
64
+ ```
65
+
66
+ ---
67
+
68
+ ## ตั้งค่า LINE Webhook URL
69
+
70
+ 1. ไปที่ [LINE Developers Console](https://developers.line.biz/console/)
71
+ 2. เลือก Channel → **Messaging API**
72
+ 3. ตั้ง Webhook URL เป็น:
73
+ ```
74
+ https://your-domain.com/webhook
75
+ ```
76
+ > ใช้ **ngrok** สำหรับ local dev:
77
+ > ```bash
78
+ > ngrok http 8000
79
+ > ```
80
+ > แล้วใช้ URL ที่ ngrok ให้ เช่น `https://abc123.ngrok.io/webhook`
81
+ 4. เปิด **Use webhook** → Verify
82
+
83
+ ---
84
+
85
+ ## ตั้งค่า Secrets บน HuggingFace Spaces
86
+
87
+ > ⚠️ ห้าม upload ไฟล์ `.env` ขึ้น Space โดยตรง ให้ตั้งผ่าน Settings แทน
88
+
89
+ 1. เปิด Space → **Settings** → **Variables and secrets**
90
+ 2. เพิ่ม Secret ดังนี้ (เลือก **Secret** ไม่ใช่ Variable):
91
+
92
+ | Key | ค่า |
93
+ |-----|-----|
94
+ | `LINE_CHANNEL_SECRET` | จาก LINE Developers Console |
95
+ | `LINE_CHANNEL_ACCESS_TOKEN` | จาก LINE Developers Console |
96
+ | `HF_API_TOKEN` | จาก huggingface.co/settings/tokens |
97
+ | `DATABASE_URL` | connection string ของ PostgreSQL |
98
+ | `REDIS_URL` | connection string ของ Redis |
99
+
100
+ ---
101
+
102
+ ## API Endpoints
103
+
104
+ | Method | Path | คำอธิบาย |
105
+ |--------|------|-----------|
106
+ | `POST` | `/webhook` | รับ event จาก LINE (signature verified) |
107
+ | `GET` | `/health` | Health check |
108
+ | `GET` | `/api/conversations` | ดึง conversation list พร้อม sentiment |
109
+ | `GET` | `/api/sentiment-summary` | สรุป sentiment ภาพรวม 7 วัน |
110
+
111
+ ---
112
+
113
+ ## HuggingFace Model
114
+
115
+ แนะนำใช้ model เหล่านี้ (ตั้งใน `HF_MODEL_NAME`):
116
+
117
+ | Model | ภาษา | ความแม่นยำ |
118
+ |-------|-------|-----------|
119
+ | `poom-sci/WangchanBERTa-finetuned-sentiment` | ไทยโดยเฉพาะ | ดีที่สุด |
120
+ | `cardiffnlp/twitter-xlm-roberta-base-sentiment` | Multilingual (ไทยได้) | ดี |
121
+ | `airesearch/wangchanberta-base-att-spm-uncased` | ไทยเป็นหลัก | ดีมากสำหรับไทย |
122
+
123
+ ---
124
+
125
+ ## Alert System
126
+
127
+ เมื่อ negative score ≥ `NEGATIVE_ALERT_THRESHOLD` (default 0.80):
128
+ - Push event ไปที่ Redis channel `sentiment:alert_live`
129
+ - Lovable Dashboard รับ alert แสดง notification ทันที
130
+
131
+ ---
132
+
133
+ ## รัน Tests
134
+
135
+ ```bash
136
+ pip install pytest pytest-asyncio
137
+ pytest tests/ -v
138
+ ```