digiplay commited on
Commit
e1f1a55
1 Parent(s): f257d99

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import httpcore
2
+ setattr(httpcore, 'SyncHTTPTransport', 'AsyncHTTPProxy')
3
+
4
+ #Beta3: 加入了一些常用的模型,以及將分享按鈕先關閉
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
+ # Define model names list
14
+
15
+ models =[
16
+ "Yntec/NostalgicLife",
17
+ "Yntec/Genuine",
18
+ "Yntec/Abased",
19
+ "Yntec/CuteFurry",
20
+ "Yntec/incha_re_zoro",
21
+ "Yntec/InsaneM3U",
22
+ "digiplay/2K-VAE",
23
+ "digiplay/ya3_VAE",
24
+ "digiplay/ya3p_VAE",
25
+ "digiplay/pan04",
26
+ "digiplay/AM-mix1",
27
+ "digiplay/MRMD_0505",
28
+ ]
29
+
30
+ # 初始化 Google 翻譯器
31
+ # Initialize Google translator
32
+
33
+ translator = Translator()
34
+
35
+ # 定義翻譯函數
36
+ # Define translation function
37
+
38
+ def translate_to_english(prompt):
39
+ try:
40
+ translated_prompt = translator.translate(prompt, src='auto', dest='en').text
41
+ return translated_prompt
42
+ except Exception as e:
43
+ return str(e)
44
+
45
+ # 修改 respond_with_timestamp 函數,使其返回三個圖片對象
46
+ # Modify the respond_with_timestamp function to return three image objects
47
+
48
+ def respond_with_timestamp(prompt, model_name):
49
+ client = InferenceClient(model=model_name)
50
+
51
+ # 將提示詞翻譯成英文 Translate prompt to English
52
+ translated_prompt = translate_to_english(prompt)
53
+
54
+ # 使用時間戳記加在提示語後面
55
+ prompt_with_timestamps = [f"{translated_prompt} {time.time() + i}" for i in range(3)]
56
+
57
+
58
+ # 生成三張圖片 Generate 3 images
59
+ images = [client.text_to_image(text) for text in prompt_with_timestamps]
60
+
61
+ # 直接返回這三個圖片對象
62
+ # Return image objects directly
63
+
64
+ return images
65
+
66
+ # 建立和啟動 Gradio 介面
67
+ # Build and launch Gradio interface
68
+ demo = gr.Interface(
69
+ fn=respond_with_timestamp,
70
+ inputs=[
71
+ gr.Textbox(label="請輸入提示語 Please input a prompt"),
72
+ gr.Dropdown(label="選擇模型 Choose a model", choices=models)
73
+ ],
74
+ outputs=[gr.Image(type="pil", label=f"img-{i+1}", show_share_button = False) for i in range(3)], # 顯示三張圖片 Display three images
75
+ title="Text-to-Image with Google Translation"
76
+ )
77
+
78
+ if __name__ == "__main__":
79
+ demo.launch()