cocoat commited on
Commit
4dd49cc
·
verified ·
1 Parent(s): b7e327f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -79
app.py CHANGED
@@ -1,89 +1,104 @@
1
  import gradio as gr
 
 
2
 
3
- # --- CSS: 喫茶店風スタイルとカスタムスピナー ---
4
- custom_css = r'''
5
- <style>
6
- body {
7
- background: #fdf6f0;
8
- font-family: "Yu Mincho", "Hiragino Mincho ProN", serif;
9
- color: #4b2e2e;
10
- }
11
- .gradio-container {
12
- background-color: #fffaf3 !important;
13
- border-radius: 12px;
14
- box-shadow: 0 4px 12px rgba(0,0,0,0.1);
15
- padding: 20px;
16
- }
17
- #custom-loader {
18
- display: none;
19
- justify-content: center;
20
- align-items: center;
21
- font-size: 1.2em;
22
- margin-top: 1em;
23
- color: #7b4c4c;
24
- }
25
- #custom-loader img {
26
- width: 32px;
27
- height: 32px;
28
- margin-left: 8px;
29
- border-radius: 50%;
30
- animation: spin 1.5s linear infinite;
31
- }
32
- @keyframes spin {
33
- 0% { transform: rotate(0deg); }
34
- 100% { transform: rotate(360deg); }
35
- }
36
- </style>
37
- '''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- # --- HTML: カスタムスピナー表示部分 ---
40
- custom_loader_html = r'''
41
- <div id="custom-loader">
42
- 画像を生成中です...
43
- <img src="/icon.png" alt="loading icon">
44
- </div>
45
- '''
46
 
47
- # --- スピナー制御用JS ---
48
- custom_js = r'''
49
- <script>
50
- function showLoader() {
51
- const loader = document.getElementById("custom-loader");
52
- if (loader) loader.style.display = "flex";
53
- }
54
- function hideLoader() {
55
- const loader = document.getElementById("custom-loader");
56
- if (loader) loader.style.display = "none";
57
- }
58
- window.addEventListener("DOMContentLoaded", () => {
59
- const btns = document.querySelectorAll("button");
60
- btns.forEach(btn => {
61
- btn.addEventListener("click", showLoader);
 
 
62
  });
63
- const observer = new MutationObserver(hideLoader);
64
- observer.observe(document.body, { childList: true, subtree: true });
65
- });
66
- </script>
67
- '''
68
 
69
- # --- メイン処理(仮の画像生成関数) ---
70
- def generate_image(text):
71
- from PIL import Image, ImageDraw
72
- img = Image.new("RGB", (256, 256), color=(253, 246, 240))
73
- d = ImageDraw.Draw(img)
74
- d.text((10, 100), text, fill=(75, 46, 46))
75
- return img
76
 
77
- with gr.Blocks() as demo:
78
- gr.HTML(custom_css + custom_loader_html + custom_js)
79
 
80
- with gr.Row():
81
- with gr.Column():
82
- prompt = gr.Textbox(label="プロンプト", placeholder="ほっとする言葉を入力してね")
83
- submit = gr.Button("画像生成")
84
- with gr.Column():
85
- gallery = gr.Image(label="生成された画像", elem_id="result", show_label=False)
86
 
87
- submit.click(fn=generate_image, inputs=prompt, outputs=gallery)
88
 
89
- demo.launch()
 
 
1
  import gradio as gr
2
+ from PIL import Image
3
+ import time
4
 
5
+ def generate_image(text):
6
+ time.sleep(2)
7
+ img = Image.new('RGB', (256, 256), color='burlywood')
8
+ return img
9
+
10
+ def main():
11
+ with gr.Blocks() as demo:
12
+ gr.HTML("""
13
+ <style>
14
+ body {
15
+ background-color: #fdf6e3;
16
+ font-family: 'Segoe UI', sans-serif;
17
+ color: #5b4636;
18
+ }
19
+ .input-card {
20
+ background: #fff3e0;
21
+ padding: 20px;
22
+ border-radius: 12px;
23
+ box-shadow: 2px 2px 10px rgba(0,0,0,0.1);
24
+ margin-bottom: 20px;
25
+ width: fit-content;
26
+ }
27
+ .title {
28
+ font-size: 24px;
29
+ margin-bottom: 16px;
30
+ }
31
+ .footer {
32
+ text-align: center;
33
+ margin-top: 2em;
34
+ color: #a1887f;
35
+ }
36
+ #component-0 > div > div.wrap.loading,
37
+ .loading-box, .loading { display: none !important; }
38
+ #custom-loader {
39
+ display: none;
40
+ justify-content: center;
41
+ align-items: center;
42
+ font-size: 1.2em;
43
+ margin-top: 1em;
44
+ color: #7b4c4c;
45
+ }
46
+ #custom-loader img {
47
+ width: 32px;
48
+ height: 32px;
49
+ margin-left: 8px;
50
+ border-radius: 50%;
51
+ animation: spin 1.5s linear infinite;
52
+ }
53
+ @keyframes spin {
54
+ 0% { transform: rotate(0deg); }
55
+ 100% { transform: rotate(360deg); }
56
+ }
57
+ </style>
58
 
59
+ <div id="custom-loader">
60
+ 画像を生成中です...
61
+ <img src="/icon.png" alt="loading icon">
62
+ </div>
 
 
 
63
 
64
+ <script>
65
+ function showLoader() {
66
+ const loader = document.getElementById("custom-loader");
67
+ if (loader) loader.style.display = "flex";
68
+ }
69
+ function hideLoader() {
70
+ const loader = document.getElementById("custom-loader");
71
+ if (loader) loader.style.display = "none";
72
+ }
73
+ window.addEventListener("DOMContentLoaded", () => {
74
+ const btn = document.querySelector('button');
75
+ if (btn) {
76
+ btn.addEventListener("click", () => {
77
+ showLoader();
78
+ setTimeout(() => hideLoader(), 6000);
79
+ });
80
+ }
81
  });
82
+ </script>
83
+ """)
 
 
 
84
 
85
+ with gr.Row():
86
+ with gr.Column(elem_classes="input-card"):
87
+ gr.Markdown("# ほっとする画像生成器")
88
+ text_input = gr.Textbox(label="キーワードを入力してください")
89
+ submit_btn = gr.Button("生成する")
90
+
91
+ image_output = gr.Image(label="生成された画像")
92
 
93
+ submit_btn.click(fn=generate_image, inputs=text_input, outputs=image_output)
 
94
 
95
+ gr.HTML("""
96
+ <div class="footer">
97
+ まるで喫茶店のような温かみのあるアプリです
98
+ </div>
99
+ """)
 
100
 
101
+ demo.launch()
102
 
103
+ if __name__ == "__main__":
104
+ main()