yeq6x commited on
Commit
ff3f921
·
1 Parent(s): 46b0d09

Refactor Gradio UI in QIE_prompt_generator.py to encapsulate the layout within a build_ui function, enhancing modularity and readability. The update includes improved organization of input fields and maintains the existing functionality for prompt generation.

Browse files
Files changed (1) hide show
  1. QIE_prompt_generator.py +27 -23
QIE_prompt_generator.py CHANGED
@@ -105,10 +105,11 @@ def call_openai_chat(api_key: str, a_data_url: Optional[str], b_data_url: Option
105
  return english, names, japanese
106
 
107
 
108
- # ===== Gradio UI =====
109
 
110
- with gr.Blocks(title="A→B 変換プロンプト自動生成(GPT-5固定)") as demo:
111
- gr.Markdown("""
 
112
  # 🎨 A→B 変換プロンプト自動生成
113
  画像A(入力)と画像B(出力)、補足説明を入力すると、
114
  A→B の変換内容を**英語プロンプト**として自動生成し、
@@ -116,29 +117,32 @@ A→B の変換内容を**英語プロンプト**として自動生成し、
116
  モデルは `gpt-5` を使用します。
117
  """)
118
 
119
- api_key = gr.Textbox(label="OpenAI API Key", type="password", placeholder="sk-...")
120
- with gr.Row():
121
- img_a = gr.Image(type="filepath", label="Image A (Input)", height=300)
122
- img_b = gr.Image(type="filepath", label="Image B (Output)", height=300)
123
 
124
- notes = gr.Textbox(label="補足説明(日本語可)", lines=4, placeholder="例)髪・服・背景は消す。目は四角。鎖骨はピンク線。など", value="この画像は例であって、汎用的なプロンプトにする")
125
- want_japanese = gr.Checkbox(label="日本語訳を含める", value=True)
126
- run_btn = gr.Button("生成する", variant="primary")
127
 
128
- english_out = gr.Textbox(label="English Prompt", lines=8)
129
- names_out = gr.Textbox(label="Name Suggestions", lines=4)
130
- japanese_out = gr.Textbox(label="日本語訳(任意)", lines=8)
131
 
132
- def on_click(api_key_in, a_path, b_path, notes_in, ja_flag):
133
- a_url = file_to_data_url(a_path) if a_path else None
134
- b_url = file_to_data_url(b_path) if b_path else None
135
- return call_openai_chat(api_key_in, a_url, b_url, notes_in, ja_flag)
 
 
 
 
 
 
 
 
136
 
137
- run_btn.click(
138
- fn=on_click,
139
- inputs=[api_key, img_a, img_b, notes, want_japanese],
140
- outputs=[english_out, names_out, japanese_out],
141
- )
142
 
143
  if __name__ == "__main__":
144
- demo.launch()
 
105
  return english, names, japanese
106
 
107
 
108
+ # ===== Gradio UI (standalone) =====
109
 
110
+ def build_ui() -> gr.Blocks:
111
+ with gr.Blocks(title="A→B 変換プロンプト自動生成(GPT-5固定)") as demo:
112
+ gr.Markdown("""
113
  # 🎨 A→B 変換プロンプト自動生成
114
  画像A(入力)と画像B(出力)、補足説明を入力すると、
115
  A→B の変換内容を**英語プロンプト**として自動生成し、
 
117
  モデルは `gpt-5` を使用します。
118
  """)
119
 
120
+ api_key = gr.Textbox(label="OpenAI API Key", type="password", placeholder="sk-...")
121
+ with gr.Row():
122
+ img_a = gr.Image(type="filepath", label="Image A (Input)", height=300)
123
+ img_b = gr.Image(type="filepath", label="Image B (Output)", height=300)
124
 
125
+ notes = gr.Textbox(label="補足説明(日本語可)", lines=4, placeholder="例)髪・服・背景は消す。目は四角。鎖骨はピンク線。など", value="この画像は例であって、汎用的なプロンプトにする")
126
+ want_japanese = gr.Checkbox(label="日本語訳を含める", value=True)
127
+ run_btn = gr.Button("生成する", variant="primary")
128
 
129
+ english_out = gr.Textbox(label="English Prompt", lines=8)
130
+ names_out = gr.Textbox(label="Name Suggestions", lines=4)
131
+ japanese_out = gr.Textbox(label="日本語訳(任意)", lines=8)
132
 
133
+ def on_click(api_key_in, a_path, b_path, notes_in, ja_flag):
134
+ a_url = file_to_data_url(a_path) if a_path else None
135
+ b_url = file_to_data_url(b_path) if b_path else None
136
+ return call_openai_chat(api_key_in, a_url, b_url, notes_in, ja_flag)
137
+
138
+ run_btn.click(
139
+ fn=on_click,
140
+ inputs=[api_key, img_a, img_b, notes, want_japanese],
141
+ outputs=[english_out, names_out, japanese_out],
142
+ )
143
+
144
+ return demo
145
 
 
 
 
 
 
146
 
147
  if __name__ == "__main__":
148
+ build_ui().launch()