Spaces:
Paused
Paused
add app requirements
Browse files- app.py +77 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 4 |
+
from peft import PeftModel
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import re
|
| 7 |
+
# # ตั้งค่า paths และ quantization
|
| 8 |
+
model_path = "EXt1/KMUTT-CPE35-Typhoon-7B-news-reasoning"
|
| 9 |
+
base_model_name = "scb10x/typhoon-7b"
|
| 10 |
+
|
| 11 |
+
quantization_config = BitsAndBytesConfig(
|
| 12 |
+
load_in_4bit=True,
|
| 13 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 14 |
+
bnb_4bit_quant_type="nf4",
|
| 15 |
+
bnb_4bit_use_double_quant=True
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
+
base_model_name,
|
| 20 |
+
quantization_config=quantization_config,
|
| 21 |
+
)
|
| 22 |
+
model = PeftModel.from_pretrained(base_model,model_path)
|
| 23 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
|
| 24 |
+
|
| 25 |
+
def generate_reasoning(title_text, label):
|
| 26 |
+
prompt = f"""[Instruction]: วิเคราะห์และอธิบายว่าข่าวนี้เป็นข่าวจริงหรือเท็จ พร้อมเหตุผลประกอบแบบเป็นขั้นตอน
|
| 27 |
+
[News Title]: {title_text}
|
| 28 |
+
[Label]: {label}
|
| 29 |
+
|
| 30 |
+
[Reasoning]:"""
|
| 31 |
+
|
| 32 |
+
# เข้ารหัส prompt
|
| 33 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 34 |
+
|
| 35 |
+
# สร้างคำตอบ
|
| 36 |
+
with torch.no_grad():
|
| 37 |
+
outputs = model.generate(
|
| 38 |
+
**inputs,
|
| 39 |
+
max_new_tokens=256,
|
| 40 |
+
temperature=0.65,
|
| 41 |
+
top_p=0.9,
|
| 42 |
+
do_sample=True,
|
| 43 |
+
repetition_penalty=1.08,
|
| 44 |
+
no_repeat_ngram_size=2,
|
| 45 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 46 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# ถอดรหัสเป็นข้อความ
|
| 50 |
+
full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 51 |
+
|
| 52 |
+
# ===== ตัดเฉพาะ Reasoning =====
|
| 53 |
+
if "[Reasoning]:" in full_response:
|
| 54 |
+
reasoning_part = full_response.split("[Reasoning]:")[-1].strip()
|
| 55 |
+
else:
|
| 56 |
+
reasoning_part = full_response.strip()
|
| 57 |
+
|
| 58 |
+
# ===== ตัดตรงที่เริ่ม hallucinate =====
|
| 59 |
+
# ใช้ regular expression เพื่อตัดคำที่ไม่ต้องการ
|
| 60 |
+
reasoning_part = re.split(r"News Title:|Instruction:|Label:", reasoning_part)[0].strip()
|
| 61 |
+
|
| 62 |
+
return reasoning_part
|
| 63 |
+
|
| 64 |
+
iface = gr.Interface(
|
| 65 |
+
fn=generate_reasoning,
|
| 66 |
+
inputs=[
|
| 67 |
+
gr.Textbox(label="News Title", placeholder="ใส่ชื่อข่าวที่ต้องการวิเคราะห์", lines=2),
|
| 68 |
+
gr.Textbox(label="Label", placeholder="ใส่ label เช่น 'ข่าวจริง' หรือ 'ข่าวปลอม'", lines=1)
|
| 69 |
+
],
|
| 70 |
+
outputs=gr.Textbox(label="Reasoning"),
|
| 71 |
+
live=True,
|
| 72 |
+
title="Thai News Fact-Checking", # ชื่อแอปพลิเคชัน
|
| 73 |
+
description="กรอกข่าวและ label เพื่อให้ระบบวิเคราะห์ว่าเป็นข่าวจริงหรือเท็จ พร้อมเหตุผลประกอบ"
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# รันแอปพลิเคชัน
|
| 77 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
re
|
| 2 |
+
peft
|
| 3 |
+
bitsandbytes
|
| 4 |
+
torch==1.13.0
|
| 5 |
+
transformers==4.30.0
|
| 6 |
+
gradio==3.35.1
|