File size: 2,437 Bytes
e1f1a55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eddfe76
20d86e8
 
e1f1a55
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import httpcore
setattr(httpcore, 'SyncHTTPTransport', 'AsyncHTTPProxy')

#Beta3: 加入了一些常用的模型,以及將分享按鈕先關閉

import gradio as gr
from googletrans import Translator
from huggingface_hub import InferenceClient
from PIL import Image
import time

# 定義模型名稱列表
# Define model names list

models =[
    "Yntec/NostalgicLife",
    "Yntec/Genuine",
    "Yntec/Abased",
    "Yntec/CuteFurry",
    "Yntec/incha_re_zoro",
    "Yntec/InsaneM3U",
    "digiplay/2K-VAE",
    "digiplay/ya3_VAE",
    "digiplay/ya3p_VAE",
    "digiplay/pan04",
    "digiplay/AM-mix1",
    "digiplay/MRMD_0505",
]

# 初始化 Google 翻譯器
# Initialize Google translator

translator = Translator()

# 定義翻譯函數
# Define translation function

def translate_to_english(prompt):
    try:
        translated_prompt = translator.translate(prompt, src='auto', dest='en').text
        return translated_prompt
    except Exception as e:
        return str(e)

# 修改 respond_with_timestamp 函數,使其返回三個圖片對象
# Modify the respond_with_timestamp function to return three image objects

def respond_with_timestamp(prompt, model_name):
    client = InferenceClient(model=model_name)

    # 將提示詞翻譯成英文 Translate prompt to English
    translated_prompt = translate_to_english(prompt)

    # 使用時間戳記加在提示語後面
    prompt_with_timestamps = [f"{translated_prompt} {time.time() + i}" for i in range(3)]


    # 生成三張圖片 Generate 3 images
    images = [client.text_to_image(text) for text in prompt_with_timestamps]

    # 直接返回這三個圖片對象
    # Return image objects directly
    
    return images

# 建立和啟動 Gradio 介面
# Build and launch Gradio interface
demo = gr.Interface(
    fn=respond_with_timestamp,
    inputs=[
        gr.Textbox(label="請輸入提示語 Please input a prompt"),
        gr.Dropdown(label="選擇模型 Choose a model", choices=models)
    ],
    outputs=[gr.Image(type="pil", label=f"img-{i+1}", show_share_button = False) for i in range(3)],  # 顯示三張圖片 Display three images
    title="Text-to-Image with Google Translation",
    description="<center>Supports any languages, as long as Google supports them 😄<br>"
                "Special Thanks : Yntec 🤗 and Every cool developers and artists and you on Huggingface.</center>"
)

if __name__ == "__main__":
    demo.launch()