tomo2chin2 commited on
Commit
2917f52
·
verified ·
1 Parent(s): 4df763d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -20
app.py CHANGED
@@ -246,29 +246,113 @@ def process_input(mode, txt, ext, temp, trim, style):
246
  return render_fullpage_screenshot(txt, ext, trim)
247
  return text_to_screenshot(txt, ext, temp, trim, style)
248
 
249
- with gr.Blocks(title="HTML Viewer & Gemini 2.5 Flash Demo", theme=gr.themes.Base()) as iface:
250
- gr.Markdown("# HTMLビューア & テキスト→インフォグラフィック変換")
251
- input_mode = gr.Radio(["HTML入力", "テキスト入力"], value="HTML入力", label="入力モード")
252
- input_text = gr.Textbox(lines=15, label="入力")
253
- style_dd = gr.Dropdown(["standard", "cute", "resort", "cool", "dental"], value="standard", label="デザインスタイル", visible=False)
254
- ext_slider = gr.Slider(0, 30, value=10, step=1, label="上下高さ拡張率(%)")
255
- temp_slider = gr.Slider(0.0, 1.0, value=0.5, step=0.1, label="生成時の温度", visible=False)
256
- trim_cb = gr.Checkbox(value=True, label="余白を自動トリミング")
257
- out_img = gr.Image(type="pil", label="スクリーンショット")
258
- gen_btn = gr.Button("生成")
259
- gr.Markdown(f"""
260
- **使用モデル**: `{os.environ.get("GEMINI_MODEL", "gemini-1.5-pro")}`
261
- **API**: `/api/screenshot`, `/api/text-to-screenshot`
262
- """)
263
-
264
- def toggle(mode):
265
- show = mode == "テキスト入力"
266
- return [{"visible": show, "__type__": "update"}, {"visible": show, "__type__": "update"}]
267
- input_mode.change(toggle, input_mode, [temp_slider, style_dd])
268
- gen_btn.click(process_input, [input_mode, input_text, ext_slider, temp_slider, trim_cb, style_dd], out_img)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
  app = gr.mount_gradio_app(app, iface, path="/")
271
 
 
272
  if __name__ == "__main__":
273
  import uvicorn
274
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
246
  return render_fullpage_screenshot(txt, ext, trim)
247
  return text_to_screenshot(txt, ext, temp, trim, style)
248
 
249
+ # ---------- Gradio UI ----------
250
+ with gr.Blocks(title="Full Page Screenshot (テキスト変換対応)", theme=gr.themes.Base()) as iface:
251
+ # 見出し
252
+ gr.Markdown(
253
+ "<h1 style='text-align:center;margin:0.2em 0'>HTMLビューア テキスト→インフォグラフィック変換</h1>",
254
+ elem_id="title",
255
+ inline=True,
256
+ )
257
+ gr.Markdown(
258
+ "HTML を直接レンダリングするか、テキストを Gemini API でインフォグラフィックに変換して画像取得できます。",
259
+ elem_id="subtitle",
260
+ )
261
+
262
+ # === 入力モード選択 ===
263
+ with gr.Row():
264
+ input_mode = gr.Radio(
265
+ ["HTML入力", "テキスト入力"],
266
+ value="HTML入力",
267
+ label="入力モード",
268
+ )
269
+
270
+ # === 共通入力テキスト ===
271
+ input_text = gr.Textbox(
272
+ lines=15,
273
+ label="入力",
274
+ placeholder="HTMLコードまたはテキストを入力してください(モードに応じて処理)。",
275
+ )
276
+
277
+ # === スタイル + スライダー類を 2 カラムで ===
278
+ with gr.Row():
279
+ # 左:スタイル
280
+ with gr.Column(scale=1, min_width=180):
281
+ style_dropdown = gr.Dropdown(
282
+ choices=["standard", "cute", "resort", "cool", "dental"],
283
+ value="standard",
284
+ label="デザインスタイル",
285
+ info="テキスト→HTML 変換時のテーマ",
286
+ visible=False, # 初期は非表示
287
+ )
288
+ # 右:拡張率 & 温度
289
+ with gr.Column(scale=3):
290
+ extension_percentage = gr.Slider(
291
+ 0, 30, value=10, step=1,
292
+ label="上下高さ拡張率(%)"
293
+ )
294
+ temperature = gr.Slider(
295
+ 0.0, 1.0, value=0.5, step=0.1,
296
+ label="生成時の温度(低い=一貫性高、高い=創造性高)",
297
+ visible=False, # 初期は非表示
298
+ )
299
+
300
+ # === トリミング & ボタン ===
301
+ trim_whitespace = gr.Checkbox(
302
+ value=True,
303
+ label="余白を自動トリミング",
304
+ )
305
+ submit_btn = gr.Button("生成", variant="primary", size="lg")
306
+
307
+ # === 出力 ===
308
+ output_image = gr.Image(
309
+ type="pil",
310
+ label="ページ全体のスクリーンショット",
311
+ show_label=True,
312
+ show_download_button=True,
313
+ )
314
+
315
+ # ---------- 変化イベント ----------
316
+ def toggle_controls(mode):
317
+ """テキストモードのときだけ温度とスタイルを表示"""
318
+ is_text = mode == "テキスト入力"
319
+ return (
320
+ gr.update(visible=is_text), # temperature
321
+ gr.update(visible=is_text), # style_dropdown
322
+ )
323
+
324
+ input_mode.change(
325
+ fn=toggle_controls,
326
+ inputs=input_mode,
327
+ outputs=[temperature, style_dropdown],
328
+ )
329
 
330
+ submit_btn.click(
331
+ fn=process_input,
332
+ inputs=[
333
+ input_mode,
334
+ input_text,
335
+ extension_percentage,
336
+ temperature,
337
+ trim_whitespace,
338
+ style_dropdown,
339
+ ],
340
+ outputs=output_image,
341
+ )
342
+
343
+ # 下部にモデル表示
344
+ gr.Markdown(
345
+ f"""
346
+ **使用モデル** : `{os.getenv("GEMINI_MODEL", "gemini-1.5-pro")}`
347
+ **API** : `/api/screenshot` / `/api/text-to-screenshot`
348
+ """,
349
+ elem_id="footnote",
350
+ )
351
+
352
+ # --- Gradio を FastAPI にマウント ---
353
  app = gr.mount_gradio_app(app, iface, path="/")
354
 
355
+
356
  if __name__ == "__main__":
357
  import uvicorn
358
  uvicorn.run(app, host="0.0.0.0", port=7860)