TRANS-GEN / app.py
digiplay's picture
Create app.py
e1f1a55 verified
raw
history blame
2.23 kB
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"
)
if __name__ == "__main__":
demo.launch()