Create app_png.py
Browse files- app_png.py +77 -0
app_png.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import httpcore
|
2 |
+
setattr(httpcore, 'SyncHTTPTransport', 'AsyncHTTPProxy')
|
3 |
+
|
4 |
+
# Beta3.1.1: 加入了自訂義方塊,有分享功能,png
|
5 |
+
|
6 |
+
import gradio as gr
|
7 |
+
from googletrans import Translator
|
8 |
+
from huggingface_hub import InferenceClient
|
9 |
+
from PIL import Image
|
10 |
+
import time
|
11 |
+
|
12 |
+
# 定義模型名稱列表
|
13 |
+
models = [
|
14 |
+
"Yntec/NostalgicLife",
|
15 |
+
"Yntec/Genuine",
|
16 |
+
"Yntec/Abased",
|
17 |
+
"Yntec/CuteFurry",
|
18 |
+
"Yntec/GOLDFish",
|
19 |
+
"Yntec/Isabelia",
|
20 |
+
"Yntec/incha_re_zoro",
|
21 |
+
"Yntec/InsaneM3U",
|
22 |
+
"digiplay/2K",
|
23 |
+
"digiplay/2K-VAE",
|
24 |
+
"digiplay/ya3_VAE",
|
25 |
+
"digiplay/ya3p_VAE",
|
26 |
+
"digiplay/pan04",
|
27 |
+
"digiplay/AM-mix1",
|
28 |
+
"digiplay/MRMD_0505",
|
29 |
+
]
|
30 |
+
|
31 |
+
# 初始化 Google 翻譯器
|
32 |
+
translator = Translator()
|
33 |
+
|
34 |
+
# 定義翻譯函數
|
35 |
+
def translate_to_english(prompt):
|
36 |
+
try:
|
37 |
+
translated_prompt = translator.translate(prompt, src='auto', dest='en').text
|
38 |
+
return translated_prompt
|
39 |
+
except Exception as e:
|
40 |
+
return str(e)
|
41 |
+
|
42 |
+
# 修改 respond_with_timestamp 函數,使其返回三個圖片對象
|
43 |
+
def respond_with_timestamp(prompt, model_name, custom_model_name):
|
44 |
+
if custom_model_name: # 檢查是否提供了自訂模型名稱
|
45 |
+
client = InferenceClient(model=custom_model_name)
|
46 |
+
else: # 如果沒有提供自訂模型名稱,就使用下拉選的模型
|
47 |
+
client = InferenceClient(model=model_name)
|
48 |
+
|
49 |
+
# 將提示詞翻譯成英文
|
50 |
+
translated_prompt = translate_to_english(prompt)
|
51 |
+
|
52 |
+
# 使用時間戳記加在提示語後面
|
53 |
+
prompt_with_timestamps = [f"{translated_prompt} {time.time() + i}" for i in range(3)]
|
54 |
+
|
55 |
+
# 生成三張圖片
|
56 |
+
try:
|
57 |
+
images = [client.text_to_image(text) for text in prompt_with_timestamps]
|
58 |
+
except Exception as e:
|
59 |
+
return [f"Error generating images: {str(e)}"] * 3 # 返回錯誤訊息而非圖片
|
60 |
+
|
61 |
+
# 返回三個圖片對象
|
62 |
+
return images
|
63 |
+
|
64 |
+
# 建立和啟動 Gradio 介面
|
65 |
+
demo = gr.Interface(
|
66 |
+
fn=respond_with_timestamp,
|
67 |
+
inputs=[
|
68 |
+
gr.Textbox(label="請輸入提示語 Please input a prompt"),
|
69 |
+
gr.Dropdown(label="選擇模型 Choose a model", choices=models),
|
70 |
+
gr.Textbox(label="自訂模型名稱 Or type Any model you like") # 讓自訂模型名稱變成選擇性
|
71 |
+
],
|
72 |
+
outputs=[gr.Image(type="pil", format="PNG", label=f"img-{i+1}", show_share_button=True) for i in range(3)], # 顯示三張圖片
|
73 |
+
title="Text-to-Image with Auto Translation"
|
74 |
+
)
|
75 |
+
|
76 |
+
if __name__ == "__main__":
|
77 |
+
demo.launch()
|