zenityx's picture
Create app.py
bed6910 verified
import gradio as gr
from transformers import pipeline
# สร้าง pipeline สำหรับการแปลภาษาไทยเป็นอังกฤษ
translation_pipeline = pipeline("translation", model="Helsinki-NLP/opus-mt-th-en")
def thai_to_prompt_tags(thai_prompt):
"""
รับข้อความภาษาไทย แปลเป็นภาษาอังกฤษ แล้วแปลงเป็น prompt ในรูปแบบ tag
"""
# แปลข้อความจากภาษาไทยเป็นภาษาอังกฤษ
translation = translation_pipeline(thai_prompt)
english_text = translation[0]['translation_text']
# แปลงเป็น tag:
# 1. แปลงเป็นตัวพิมพ์เล็ก
# 2. แบ่งคำด้วยช่องว่าง
# 3. ลบเครื่องหมายวรรคตอน
tags = english_text.lower().split()
tags = [tag.strip(".,") for tag in tags if len(tag) > 1]
# ลบคำซ้ำ (ถ้ามี)
unique_tags = list(dict.fromkeys(tags))
tag_format = ", ".join(unique_tags)
return english_text, tag_format
# สร้างอินเตอร์เฟซด้วย Gradio
iface = gr.Interface(
fn=thai_to_prompt_tags,
inputs=gr.Textbox(
lines=4,
placeholder="พิมพ์คำอธิบายเป็นภาษาไทยที่นี่...",
label="คำอธิบาย (ภาษาไทย)"
),
outputs=[
gr.Textbox(label="คำอธิบาย (ภาษาอังกฤษ)"),
gr.Textbox(label="Prompt Tag Format")
],
title="Thai-to-Prompt Converter สำหรับ Stable Diffusion",
description="ป้อนคำอธิบายเป็นภาษาไทย ระบบจะแปลเป็นภาษาอังกฤษและแปลงเป็น prompt ในรูปแบบ tag ให้"
)
if __name__ == "__main__":
iface.launch()