Spaces:
Paused
Paused
Update app.py
Browse files12345678910114 unmodified lines1617181920212223242526273 unmodified lines313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384854 unmodified lines909192939495969798991001011021031041051061073 unmodified lines1111121131141151161171181191201211221 unmodified line12412512612712812913013113213313413513613713813914021 unmodified lines16216316416516616716816913 unmodified lines1831841851861871881892 unmodified lines1921931941951961971981992002012022032042052062072082092102112121 unmodified line2142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802 unmodified lines2832842852862872882892902912922932942952962972982996 unmodified lines3063073083093103113123133143153163173183193203213223233243253263273283293303313323331 unmodified line335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409# app.py (Gradio 5.x 対応フルバージョン)# =========================================# 主要変更点# • gradio==4.19.2 → gradio>=5.29.0 (requirements.txt で指定)# • Blocks の theme を Origin() に変更(4.x の外観を継承)# • mount_gradio_app に ssr_mode=False を追加# • Gradio 内部アセットの手動マウントを撤去(5.x では不要)# =========================================import osimport time4 unmodified linesfrom io import BytesIOfrom concurrent.futures import ThreadPoolExecutorimport numpy as np # 画像トリミング高速化from PIL import Imageimport gradio as gr # ★ 5.xfrom fastapi import FastAPI, HTTPExceptionfrom fastapi.responses import StreamingResponsefrom fastapi.middleware.cors import CORSMiddlewarefrom pydantic import BaseModel3 unmodified linesfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECimport google.generativeai as genai # Geminifrom huggingface_hub import hf_hub_download# -------------------- ロガー設定 --------------------logging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)# ====================================================# WebDriver プール実装# ====================================================class WebDriverPool:def __init__(self, max_drivers: int = 3):self.driver_queue = queue.Queue()self.max_drivers = max_driversself.lock = threading.Lock()self.count = 0logger.info(f"WebDriver プール初期化: 最大 {max_drivers}")def get_driver(self):# 既存if not self.driver_queue.empty():logger.info("既存 WebDriver を取得")return self.driver_queue.get()# 新規with self.lock:if self.count < self.max_drivers:self.count += 1logger.info(f"新規 WebDriver 作成 ({self.count}/{self.max_drivers})")options = Options()options.add_argument("--headless")options.add_argument("--no-sandbox")options.add_argument("--disable-dev-shm-usage")options.add_argument("--force-device-scale-factor=1")options.add_argument("--disable-features=NetworkService")options.add_argument("--dns-prefetch-disable")driver_path = os.environ.get("CHROMEDRIVER_PATH")if driver_path and os.path.exists(driver_path):logger.info(f"環境変数 CHROMEDRIVER_PATH 使用: {driver_path}")service = webdriver.ChromeService(executable_path=driver_path)return webdriver.Chrome(service=service, options=options)return webdriver.Chrome(options=options)# プール満杯logger.info("プール満杯。返却待ち…")return self.driver_queue.get()def release_driver(self, driver):if driver:try:driver.get("about:blank")4 unmodified linesself.driver_queue.put(driver)logger.info("WebDriver をプールに返却")except Exception as e:logger.error(f"返却時エラー: {e}")driver.quit()with self.lock:self.count -= 1def close_all(self):logger.info("全 WebDriver 終了処理")closed = 0while not self.driver_queue.empty():try:driver = self.driver_queue.get(block=False)driver.quit()closed += 1except queue.Empty:break3 unmodified linesself.count = 0logger.info(f"{closed} 個の WebDriver を終了")# グローバルプールdriver_pool = WebDriverPool(max_drivers=int(os.environ.get("MAX_WEBDRIVERS", "3")))# ====================================================# Pydantic モデル# ====================================================class GeminiRequest(BaseModel):text: strextension_percentage: float = 10.01 unmodified linetrim_whitespace: bool = Truestyle: str = "standard"class ScreenshotRequest(BaseModel):html_code: strextension_percentage: float = 10.0trim_whitespace: bool = Truestyle: str = "standard"# ====================================================# 補助関数# ====================================================def enhance_font_awesome_layout(html_code: str) -> str:"""Font Awesome レイアウトを調整し preload タグを付与"""fa_preload = """<link rel="preload" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/webfonts/fa-solid-900.woff2" as="font" type="font/woff2" crossorigin><link rel="preload" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/webfonts/fa-regular-400.woff2" as="font" type="font/woff2" crossorigin>21 unmodified linesreturn html_code[:body_start] + f'<head>{fa_preload}{fa_css}</head>' + html_code[body_start:]return f'<html><head>{fa_preload}{fa_css}</head>{html_code}</html>'def load_system_instruction(style: str = "standard") -> str:"""テーマ別 prompt.txt を読み込み"""valid = ["standard", "cute", "resort", "cool", "dental", "school", "KOKUGO"]if style not in valid:logger.warning(f"無効 style '{style}' → 'standard'")13 unmodified lineswith open(file_path, encoding="utf-8") as f:return f.read()except Exception as e:logger.warning(f"HuggingFace 取得失敗 ({e}) → デフォルト prompt.txt")file_path = hf_hub_download(repo_id="tomo2chin2/GURAREKOstlyle",filename="prompt.txt",2 unmodified lineswith open(file_path, encoding="utf-8") as f:return f.read()def generate_html_from_text(text: str, temperature: float = 0.5, style: str = "standard") -> str:"""Gemini で HTML 生成"""api_key = os.environ.get("GEMINI_API_KEY")if not api_key:raise ValueError("環境変数 GEMINI_API_KEY が未設定")model_name = os.environ.get("GEMINI_MODEL", "gemini-1.5-pro")genai.configure(api_key=api_key)system_instruction = load_system_instruction(style)model = genai.GenerativeModel(model_name)generation_config = dict(temperature=temperature, top_p=0.7, top_k=20, max_output_tokens=8192,candidate_count=1)safety_settings = [{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},1 unmodified line{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}]prompt = f"{system_instruction}\n\n{text}"response = model.generate_content(prompt, generation_config=generation_config, safety_settings=safety_settings)raw = response.texthtml_start = raw.find("```html")html_end = raw.rfind("```")if html_start != -1 and html_end != -1 and html_start < html_end:html_code = raw[html_start + 7:html_end].strip()else:html_code = rawhtml_code = enhance_font_awesome_layout(html_code)return html_codedef trim_image_whitespace(image: Image.Image, threshold: int = 250, padding: int = 10) -> Image.Image:"""白余白トリミング(NumPy 高速化)"""gray = image.convert("L")arr = np.array(gray)mask = arr < thresholdrows = np.any(mask, axis=1)cols = np.any(mask, axis=0)if np.any(rows) and np.any(cols):y_min, y_max = np.where(rows)[0][[0, -1]]x_min, x_max = np.where(cols)[0][[0, -1]]y_min = max(0, y_min - padding)x_min = max(0, x_min - padding)y_max = min(image.height - 1, y_max + padding)x_max = min(image.width - 1, x_max + padding)return image.crop((x_min, y_min, x_max + 1, y_max + 1))return image# ----------------------------------------------------# HTML → スクリーンショット# ----------------------------------------------------def render_fullpage_screenshot(html_code: str, extension_percentage: float = 6.0,trim_whitespace: bool = True,driver=None) -> Image.Image:driver_from_pool = Falsetmp_path = Nonetry:if driver is None:driver = driver_pool.get_driver()driver_from_pool = Truewith tempfile.NamedTemporaryFile(suffix=".html", delete=False, mode="w", encoding="utf-8") as tmp:tmp_path = tmp.nametmp.write(html_code)initial_w, initial_h = 1200, 1000driver.set_window_size(initial_w, initial_h)driver.get(f"file://{tmp_path}")WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "body")))total_h = driver.execute_script("return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)")viewport_h = driver.execute_script("return window.innerHeight")scrolls = max(1, min(5, total_h // viewport_h))for i in range(scrolls):driver.execute_script(f"window.scrollTo(0, {i * (viewport_h - 100)})")time.sleep(0.1)driver.execute_script("window.scrollTo(0, 0)")time.sleep(0.2)dims = driver.execute_script("""2 unmodified linesheight: Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)}""")w = min(max(dims["width"], 100), 2000)h = min(max(dims["height"], 100), 4000)h = int(h * (1 + extension_percentage / 100.0))driver.set_window_size(w, h)time.sleep(0.5)png = driver.get_screenshot_as_png()img = Image.open(BytesIO(png))if trim_whitespace:img = trim_image_whitespace(img, threshold=248, padding=20)return imgexcept Exception as e:logger.error(f"Screenshot error: {e}", exc_info=True)return Image.new("RGB", (1, 1), (0, 0, 0))6 unmodified linesexcept Exception:pass# ----------------------------------------------------# テキスト → スクリーンショット(並列)# ----------------------------------------------------def text_to_screenshot_parallel(text: str, extension_percentage: float, temperature: float = 0.5,trim_whitespace: bool = True, style: str = "standard") -> Image.Image:start = time.time()with ThreadPoolExecutor(max_workers=2) as exe:html_fut = exe.submit(generate_html_from_text, text, temperature, style)driver_fut = exe.submit(driver_pool.get_driver)html_code = html_fut.result()driver = driver_fut.result()img = render_fullpage_screenshot(html_code, extension_percentage, trim_whitespace, driver)logger.info(f"並列処理 完了 {time.time() - start:.2f}s")return imgdef text_to_screenshot(*args, **kwargs) -> Image.Image:return text_to_screenshot_parallel(*args, **kwargs)# ====================================================# FastAPI セットアップ# ====================================================app = FastAPI()app.add_middleware(CORSMiddleware,1 unmodified lineallow_methods=["*"], allow_headers=["*"])# ------------ API エンドポイント ---------------@app.post("/api/screenshot", response_class=StreamingResponse, tags=["Screenshot"])async def api_render_screenshot(req: ScreenshotRequest):img = render_fullpage_screenshot(req.html_code, req.extension_percentage, req.trim_whitespace)buf = BytesIO()img.save(buf, format="PNG"); buf.seek(0)return StreamingResponse(buf, media_type="image/png")@app.post("/api/text-to-screenshot", response_class=StreamingResponse, tags=["Screenshot", "Gemini"])async def api_text_to_screenshot(req: GeminiRequest):img = text_to_screenshot_parallel(req.text, req.extension_percentage,req.temperature, req.trim_whitespace, req.style)buf = BytesIO()img.save(buf, format="PNG"); buf.seek(0)return StreamingResponse(buf, media_type="image/png")# ====================================================# Gradio UI# ====================================================def process_input(mode, input_text, ext_perc, temp, trim_ws, style):if mode == "HTML入力":return render_fullpage_screenshot(input_text, ext_perc, trim_ws)return text_to_screenshot_parallel(input_text, ext_perc, temp, trim_ws, style)with gr.Blocks(title="Full Page Screenshot (テキスト変換対応)",theme=gr.themes.Origin()) as iface:gr.Markdown("# HTMLビューア & テキスト→インフォグラフィック")with gr.Row():input_mode = gr.Radio(["HTML入力", "テキスト入力"], value="HTML入力", label="入力モード")input_text = gr.Textbox(lines=15, label="入力")with gr.Row():style_dropdown = gr.Dropdown(["standard", "cute", "resort", "cool", "dental", "school", "KOKUGO"],value="standard", label="デザインスタイル", visible=False)with gr.Column(scale=2):ext_slider = gr.Slider(0, 30, value=10, step=1, label="上下高さ拡張率(%)")temp_slider = gr.Slider(0.0, 1.0, value=0.5, step=0.1,label="生成時の温度", visible=False)trim_ws_chk = gr.Checkbox(value=True, label="余白を自動トリミング")submit_btn = gr.Button("生成")out_img = gr.Image(type="pil", label="スクリーンショット")def toggle_controls(mode):is_text = mode == "テキスト入力"return [gr.update(visible=is_text), gr.update(visible=is_text)]input_mode.change(toggle_controls, input_mode, [temp_slider, style_dropdown])submit_btn.click(process_input,[input_mode, input_text, ext_slider, temp_slider, trim_ws_chk, style_dropdown],out_img)gemini_model = os.environ.get("GEMINI_MODEL", "gemini-1.5-pro")gr.Markdown(f"""**API**- `/api/screenshot` – HTML → PNG- `/api/text-to-screenshot` – テキスト → インフォグラフィック PNG**設定**- 使用モデル: `{gemini_model}`- スタイル: standard / cute / resort / cool / dental / school / KOKUGO- WebDriver 最大数: {driver_pool.max_drivers}""")# FastAPI へマウント(SSR 無効)app = gr.mount_gradio_app(app, iface, path="/", ssr_mode=False)# ローカル実行用if __name__ == "__main__":import uvicornuvicorn.run(app, host="0.0.0.0", port=7860)# 終了時のクリーンアップimport atexitatexit.register(driver_pool.close_all)1234567894 unmodified lines1415161718192021222324253 unmodified lines293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788894 unmodified lines9495969798991001011021031041051061071081091101113 unmodified lines1151161171181191201211221231241251261271281 unmodified line13013113213313413513613713813914014114214314414514614714821 unmodified lines17017117217317417517617717813 unmodified lines1921931941951961971982 unmodified lines2012022032042052062072082092102112122132142152162172182192202212222231 unmodified line2252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862 unmodified lines2892902912922932942952962972982993003013026 unmodified lines3093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393401 unmodified line342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441# ===============================================================# app.py ― Gradio 5.x + FastAPI + Gemini + Selenium# ・Gradio 5.29.0 以上を前提に最適化# ・UI/API を維持したまま 307 ループを解消# ・/gradio にサブマウントし / はリダイレクトのみ# ===============================================================import osimport time4 unmodified linesfrom io import BytesIOfrom concurrent.futures import ThreadPoolExecutorimport numpy as npfrom PIL import Imageimport gradio as grfrom fastapi import FastAPI, HTTPExceptionfrom fastapi.responses import StreamingResponse, RedirectResponsefrom fastapi.middleware.cors import CORSMiddlewarefrom pydantic import BaseModel3 unmodified linesfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECimport google.generativeai as genaifrom huggingface_hub import hf_hub_download# ===============================================================# ロガー# ===============================================================logging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)# ===============================================================# WebDriver プール# ===============================================================class WebDriverPool:"""複数の WebDriver を使い回すシンプルなプール"""def __init__(self, max_drivers: int = 3):self.driver_queue: "queue.Queue[webdriver.Chrome]" = queue.Queue()self.max_drivers = max_driversself.lock = threading.Lock()self.count = 0logger.info(f"WebDriver プール初期化: 最大 {max_drivers}")def _create_driver(self) -> webdriver.Chrome:options = Options()options.add_argument("--headless")options.add_argument("--no-sandbox")options.add_argument("--disable-dev-shm-usage")options.add_argument("--force-device-scale-factor=1")options.add_argument("--disable-features=NetworkService")options.add_argument("--dns-prefetch-disable")chromedriver_path = os.environ.get("CHROMEDRIVER_PATH")if chromedriver_path and os.path.exists(chromedriver_path):logger.info(f"環境変数 CHROMEDRIVER_PATH を使用: {chromedriver_path}")service = webdriver.ChromeService(executable_path=chromedriver_path)return webdriver.Chrome(service=service, options=options)return webdriver.Chrome(options=options)def get_driver(self) -> webdriver.Chrome:if not self.driver_queue.empty():logger.info("プールから既存 WebDriver を取得")return self.driver_queue.get()with self.lock:if self.count < self.max_drivers:self.count += 1logger.info(f"新規 WebDriver 作成 ({self.count}/{self.max_drivers})")return self._create_driver()# ここに来るのはプール満杯時logger.info("WebDriver プール満杯。空きを待機")return self.driver_queue.get()def release_driver(self, driver: webdriver.Chrome):if driver:try:driver.get("about:blank")4 unmodified linesself.driver_queue.put(driver)logger.info("WebDriver をプールに返却")except Exception as e:logger.error(f"返却エラー: {e}")driver.quit()with self.lock:self.count -= 1def close_all(self):logger.info("WebDriver 全終了")closed = 0while not self.driver_queue.empty():try:drv = self.driver_queue.get(block=False)drv.quit()closed += 1except queue.Empty:break3 unmodified linesself.count = 0logger.info(f"{closed} 個の WebDriver を終了")# グローバルプールdriver_pool = WebDriverPool(max_drivers=int(os.environ.get("MAX_WEBDRIVERS", "3")))# ===============================================================# Pydantic モデル# ===============================================================class GeminiRequest(BaseModel):text: strextension_percentage: float = 10.01 unmodified linetrim_whitespace: bool = Truestyle: str = "standard"class ScreenshotRequest(BaseModel):html_code: strextension_percentage: float = 10.0trim_whitespace: bool = Truestyle: str = "standard"# ===============================================================# 補助関数# ===============================================================def enhance_font_awesome_layout(html_code: str) -> str:"""Font Awesome をプリロード + レイアウト微調整 CSS を注入"""fa_preload = """<link rel="preload" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/webfonts/fa-solid-900.woff2" as="font" type="font/woff2" crossorigin><link rel="preload" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/webfonts/fa-regular-400.woff2" as="font" type="font/woff2" crossorigin>21 unmodified linesreturn html_code[:body_start] + f'<head>{fa_preload}{fa_css}</head>' + html_code[body_start:]return f'<html><head>{fa_preload}{fa_css}</head>{html_code}</html>'def load_system_instruction(style: str = "standard") -> str:"""スタイル別の prompt.txt をローカル or HF から読み込み"""valid = ["standard", "cute", "resort", "cool", "dental", "school", "KOKUGO"]if style not in valid:logger.warning(f"無効 style '{style}' → 'standard'")13 unmodified lineswith open(file_path, encoding="utf-8") as f:return f.read()except Exception as e:logger.warning(f"HF 取得失敗 ({e}) → デフォルト prompt.txt")file_path = hf_hub_download(repo_id="tomo2chin2/GURAREKOstlyle",filename="prompt.txt",2 unmodified lineswith open(file_path, encoding="utf-8") as f:return f.read()def generate_html_from_text(text: str, temperature: float = 0.5, style: str = "standard") -> str:"""Gemini で与えられたテキストを HTML に整形して返す"""api_key = os.environ.get("GEMINI_API_KEY")if not api_key:raise ValueError("環境変数 GEMINI_API_KEY が未設定")model_name = os.environ.get("GEMINI_MODEL", "gemini-1.5-pro")genai.configure(api_key=api_key)model = genai.GenerativeModel(model_name)system_instruction = load_system_instruction(style)prompt = f"{system_instruction}\n\n{text}"generation_config = dict(temperature=temperature, top_p=0.7, top_k=20,max_output_tokens=8192, candidate_count=1)safety_settings = [{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},1 unmodified line{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}]resp = model.generate_content(prompt, generation_config=generation_config, safety_settings=safety_settings)raw = resp.text# ```html … ``` 抜き取りstart = raw.find("```html")end = raw.rfind("```")html_code = raw[start + 7:end].strip() if start != -1 and end != -1 else rawreturn enhance_font_awesome_layout(html_code)def trim_image_whitespace(image: Image.Image, threshold: int = 250, padding: int = 10) -> Image.Image:"""白背景をトリミング"""gray = image.convert("L")arr = np.array(gray)mask = arr < thresholdrows, cols = np.any(mask, axis=1), np.any(mask, axis=0)if np.any(rows) and np.any(cols):y_min, y_max = np.where(rows)[0][[0, -1]]x_min, x_max = np.where(cols)[0][[0, -1]]return image.crop((max(0, x_min - padding), max(0, y_min - padding),min(image.width - 1, x_max + padding), min(image.height - 1, y_max + padding)))return image# ===============================================================# HTML → スクリーンショット# ===============================================================def render_fullpage_screenshot(html_code: str, extension_percentage: float = 6.0,trim_whitespace: bool = True, driver=None) -> Image.Image:tmp_path = Nonedriver_from_pool = Falsetry:if driver is None:driver = driver_pool.get_driver()driver_from_pool = True# 一時 HTML 保存with tempfile.NamedTemporaryFile(suffix=".html", delete=False, mode="w", encoding="utf-8") as tmp:tmp_path = tmp.nametmp.write(html_code)driver.set_window_size(1200, 1000)driver.get("file://" + tmp_path)# body 待機WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "body")))total_height = driver.execute_script("return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)")viewport_height = driver.execute_script("return window.innerHeight")for i in range(max(1, min(5, total_height // viewport_height))):driver.execute_script(f"window.scrollTo(0, {i * (viewport_height - 100)})")time.sleep(0.1)driver.execute_script("window.scrollTo(0,0)")time.sleep(0.2)dims = driver.execute_script("""2 unmodified linesheight: Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)}""")width = min(max(dims["width"], 100), 2000)height = min(max(dims["height"], 100), 4000)height = int(height * (1 + extension_percentage / 100.0))driver.set_window_size(width, height)time.sleep(0.5)img = Image.open(BytesIO(driver.get_screenshot_as_png()))return trim_image_whitespace(img, 248, 20) if trim_whitespace else imgexcept Exception as e:logger.error(f"Screenshot error: {e}", exc_info=True)return Image.new("RGB", (1, 1), (0, 0, 0))6 unmodified linesexcept Exception:pass# ===============================================================# テキスト → スクリーンショット(並列)# ===============================================================def text_to_screenshot_parallel(text: str, extension_percentage: float, temperature: float = 0.5,trim_whitespace: bool = True, style: str = "standard") -> Image.Image:start = time.time()with ThreadPoolExecutor(max_workers=2) as ex:html_future = ex.submit(generate_html_from_text, text, temperature, style)driver_future = ex.submit(driver_pool.get_driver)html_code = html_future.result()driver = driver_future.result()img = render_fullpage_screenshot(html_code, extension_percentage, trim_whitespace, driver)logger.info(f"並列生成完了: {time.time() - start:.2f}s")return imgdef text_to_screenshot(*args, **kwargs):"""後方互換用エイリアス"""return text_to_screenshot_parallel(*args, **kwargs)# ===============================================================# FastAPI# ===============================================================app = FastAPI()app.add_middleware(CORSMiddleware,1 unmodified lineallow_methods=["*"], allow_headers=["*"])@app.post("/api/screenshot", response_class=StreamingResponse, tags=["Screenshot"])async def api_render_screenshot(req: ScreenshotRequest):img = render_fullpage_screenshot(req.html_code, req.extension_percentage, req.trim_whitespace)buf = BytesIO(); img.save(buf, "PNG"); buf.seek(0)return StreamingResponse(buf, media_type="image/png")@app.post("/api/text-to-screenshot", response_class=StreamingResponse, tags=["Screenshot", "Gemini"])async def api_text_to_screenshot(req: GeminiRequest):img = text_to_screenshot_parallel(req.text, req.extension_percentage, req.temperature, req.trim_whitespace, req.style)buf = BytesIO(); img.save(buf, "PNG"); buf.seek(0)return StreamingResponse(buf, media_type="image/png")# ===============================================================# Gradio UI# ===============================================================def process_input(mode, text, ext, temp, trim, style):if mode == "HTML入力":return render_fullpage_screenshot(text, ext, trim)return text_to_screenshot_parallel(text, ext, temp, trim, style)with gr.Blocks(title="Full Page Screenshot (テキスト変換対応)",theme=gr.themes.Origin()) as iface:gr.Markdown("# HTMLビューア & テキスト→インフォグラフィック変換")with gr.Row():input_mode = gr.Radio(["HTML入力", "テキスト入力"], value="HTML入力", label="入力モード")input_text = gr.Textbox(lines=15, label="入力")with gr.Row():style_dropdown = gr.Dropdown(["standard", "cute", "resort", "cool", "dental", "school", "KOKUGO"],value="standard", label="デザインスタイル", visible=False)with gr.Column(scale=2):ext_slider = gr.Slider(0, 30, value=10, step=1, label="上下高さ拡張率(%)")temp_slider = gr.Slider(0.0, 1.0, value=0.5, step=0.1,label="生成時の温度", visible=False)trim_chk = gr.Checkbox(value=True, label="余白を自動トリミング")submit_btn = gr.Button("生成")out_img = gr.Image(type="pil", label="スクリーンショット")def toggle(mode):is_text = mode == "テキスト入力"return [gr.update(visible=is_text), gr.update(visible=is_text)]input_mode.change(toggle, input_mode, [temp_slider, style_dropdown])submit_btn.click(process_input,[input_mode, input_text, ext_slider, temp_slider, trim_chk, style_dropdown],out_img)model_name = os.environ.get("GEMINI_MODEL", "gemini-1.5-pro")gr.Markdown(f"""**API**- `/api/screenshot` – HTML → PNG- `/api/text-to-screenshot` – テキスト → インフォグラフィック PNG**設定**- 使用モデル: `{model_name}`- 対応スタイル: standard / cute / resort / cool / dental / school / KOKUGO- WebDriver 最大数: {driver_pool.max_drivers}""")# ===============================================================# FastAPI へマウント & ルートリダイレクト# ===============================================================GRADIO_PATH = "/gradio"app = gr.mount_gradio_app(app, iface, path=GRADIO_PATH, ssr_mode=False)@app.get("/")def _root():"""ルートに来たら /gradio へリダイレクト"""return RedirectResponse(GRADIO_PATH)# ===============================================================# ローカルデバッグ用# ===============================================================if __name__ == "__main__":import uvicornlogger.info("Uvicorn 起動 (ローカル)")uvicorn.run(app, host="0.0.0.0", port=7860)# ===============================================================# 終了時 WebDriver 後始末# ===============================================================import atexitatexit.register(driver_pool.close_all)
app.py+169-137