app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# โหลด Zero-Shot Classifier
|
| 5 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 6 |
+
|
| 7 |
+
# ฟังก์ชันหลักสำหรับจัดหมวดหมู่ข้อความ
|
| 8 |
+
def classify(text, labels):
|
| 9 |
+
labels = [label.strip() for label in labels.split(",") if label.strip()]
|
| 10 |
+
if not labels:
|
| 11 |
+
return {"Error": "กรุณาใส่หมวดหมู่อย่างน้อยหนึ่งรายการ"}
|
| 12 |
+
result = classifier(text, candidate_labels=labels)
|
| 13 |
+
return dict(zip(result["labels"], [round(score, 3) for score in result["scores"]]))
|
| 14 |
+
|
| 15 |
+
# อินเทอร์เฟซ Gradio
|
| 16 |
+
demo = gr.Interface(
|
| 17 |
+
fn=classify,
|
| 18 |
+
inputs=[
|
| 19 |
+
gr.Textbox(label="ข้อความ", placeholder="ฉันสนใจด้านการเขียนโปรแกรมและ AI", lines=2),
|
| 20 |
+
gr.Textbox(label="หมวดหมู่ (คั่นด้วย ,)", placeholder="วิทยาการคอมพิวเตอร์, ศิลปะ, วรรณกรรม"),
|
| 21 |
+
],
|
| 22 |
+
outputs=gr.Label(label="การจำแนกหมวดหมู่"),
|
| 23 |
+
title="Zero-Shot Text Classifier",
|
| 24 |
+
description="ใส่ข้อความที่คุณสนใจ และให้โมเดลประเมินว่าคล้ายกับหมวดหมู่ใดมากที่สุด โดยใช้ BART-MNLI"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
demo.launch()
|