Enhance app.py by adding tokenizer initialization and refining response handling in the chat function
Browse files
app.py
CHANGED
@@ -2,15 +2,20 @@ import gradio as gr
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
from dotenv import load_dotenv
|
4 |
import os
|
|
|
5 |
|
6 |
# โหลดตัวแปรจาก .env
|
7 |
load_dotenv()
|
8 |
|
|
|
9 |
# ดึง token จาก environment variable
|
10 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
11 |
|
12 |
# สร้าง InferenceClient ด้วย token
|
13 |
client = InferenceClient("iapp/chinda-qwen3-4b", token=HF_TOKEN)
|
|
|
|
|
|
|
14 |
|
15 |
# ฟังก์ชันสำหรับประมวลผลข้อความสนทนา
|
16 |
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
|
@@ -23,18 +28,29 @@ def respond(message, history: list[tuple[str, str]], system_message, max_tokens,
|
|
23 |
messages.append({"role": "assistant", "content": bot_msg})
|
24 |
messages.append({"role": "user", "content": message})
|
25 |
|
|
|
|
|
|
|
|
|
26 |
response = ""
|
27 |
# เรียกใช้งานแบบ streaming
|
28 |
-
for
|
29 |
messages,
|
30 |
max_tokens=max_tokens,
|
31 |
stream=True,
|
32 |
temperature=temperature,
|
33 |
top_p=top_p,
|
34 |
):
|
35 |
-
token =
|
36 |
response += token
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
# สร้าง UI ด้วย Gradio
|
40 |
demo = gr.ChatInterface(
|
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
from dotenv import load_dotenv
|
4 |
import os
|
5 |
+
from transformers import AutoTokenizer
|
6 |
|
7 |
# โหลดตัวแปรจาก .env
|
8 |
load_dotenv()
|
9 |
|
10 |
+
|
11 |
# ดึง token จาก environment variable
|
12 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
13 |
|
14 |
# สร้าง InferenceClient ด้วย token
|
15 |
client = InferenceClient("iapp/chinda-qwen3-4b", token=HF_TOKEN)
|
16 |
+
# โหลด tokenizer สำหรับ apply_chat_template
|
17 |
+
model_name = "iapp/chinda-qwen3-4b"
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
19 |
|
20 |
# ฟังก์ชันสำหรับประมวลผลข้อความสนทนา
|
21 |
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
|
|
|
28 |
messages.append({"role": "assistant", "content": bot_msg})
|
29 |
messages.append({"role": "user", "content": message})
|
30 |
|
31 |
+
# ใช้ tokenizer.apply_chat_template เพื่อเตรียม prompt (optional, เฉพาะถ้าต้องการปรับ prompt)
|
32 |
+
# text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True, enable_thinking=True)
|
33 |
+
# แต่ InferenceClient รองรับ messages โดยตรง
|
34 |
+
|
35 |
response = ""
|
36 |
# เรียกใช้งานแบบ streaming
|
37 |
+
for msg in client.chat_completion(
|
38 |
messages,
|
39 |
max_tokens=max_tokens,
|
40 |
stream=True,
|
41 |
temperature=temperature,
|
42 |
top_p=top_p,
|
43 |
):
|
44 |
+
token = msg.choices[0].delta.content
|
45 |
response += token
|
46 |
+
# แยก 🧠 Thinking กับ 💬 Response ถ้ามี </think>
|
47 |
+
if "</think>" in response:
|
48 |
+
think_split = response.split("</think>", 1)
|
49 |
+
thinking = think_split[0].replace("<think>", "").strip()
|
50 |
+
content = think_split[1].strip()
|
51 |
+
yield f"🧠 Thinking: {thinking}\n\n💬 Response: {content}"
|
52 |
+
else:
|
53 |
+
yield response
|
54 |
|
55 |
# สร้าง UI ด้วย Gradio
|
56 |
demo = gr.ChatInterface(
|