|
import gradio as gr |
|
from transformers import 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'] |
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
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() |
|
|