import gradio as gr from fastapi import FastAPI, HTTPException from fastapi.responses import StreamingResponse from fastapi.staticfiles import StaticFiles from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from PIL import Image from io import BytesIO import tempfile import time import os import logging from huggingface_hub import hf_hub_download # --- Gemini SDK (v1.x) --------------------------------- from google import genai from google.genai import types # ------------------------------------------------------- # ロギング設定 logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # ---------- データモデル ---------- class GeminiRequest(BaseModel): text: str extension_percentage: float = 10.0 temperature: float = 0.5 trim_whitespace: bool = True style: str = "standard" class ScreenshotRequest(BaseModel): html_code: str extension_percentage: float = 10.0 trim_whitespace: bool = True style: str = "standard" # ---------- ユーティリティ ---------- def enhance_font_awesome_layout(html_code): fa_fix_css = """ """ if '
' in html_code: return html_code.replace('', f'{fa_fix_css}') elif '') if head_end > 0: return html_code[:head_end] + fa_fix_css + html_code[head_end:] body_start = html_code.find(' 0: return html_code[:body_start] + f'{fa_fix_css}' + html_code[body_start:] return f'{fa_fix_css}' + html_code + '' def load_system_instruction(style="standard"): valid_styles = ["standard", "cute", "resort", "cool", "dental"] if style not in valid_styles: logger.warning(f"無効なスタイル '{style}'。'standard' を使用") style = "standard" local_path = os.path.join(os.path.dirname(__file__), style, "prompt.txt") if os.path.exists(local_path): with open(local_path, encoding="utf-8") as f: return f.read() try: file_path = hf_hub_download( repo_id="tomo2chin2/GURAREKOstlyle", filename=f"{style}/prompt.txt", repo_type="dataset" ) with open(file_path, encoding="utf-8") as f: return f.read() except Exception: file_path = hf_hub_download( repo_id="tomo2chin2/GURAREKOstlyle", filename="prompt.txt", repo_type="dataset" ) with open(file_path, encoding="utf-8") as f: return f.read() # ---------- Gemini HTML 生成 ---------- def generate_html_from_text(text, temperature=0.3, style="standard"): """Gemini で HTML を生成。2.5 Flash Preview の場合は thinking_off""" 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") client = genai.Client(api_key=api_key) system_instruction = load_system_instruction(style) prompt = f"{system_instruction}\n\n{text}" safety_settings = [ {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, ] # ---- モデル分岐 ---- if model_name == "gemini-2.5-flash-preview-04-17": generation_cfg = types.GenerateContentConfig( temperature=temperature, top_p=0.7, top_k=20, max_output_tokens=8192, candidate_count=1, thinking_config=types.ThinkingConfig(thinking_budget=0) # thinking OFF ) else: generation_cfg = types.GenerateContentConfig( temperature=temperature, top_p=0.7, top_k=20, max_output_tokens=8192, candidate_count=1, ) response = client.models.generate_content( model=model_name, contents=prompt, config=generation_cfg, safety_settings=safety_settings ) raw = response.text # ```html ... ``` 抽出 start = raw.find("```html") end = raw.rfind("```") if start != -1 and end != -1 and start < end: html_code = raw[start + 7:end].strip() return enhance_font_awesome_layout(html_code) logger.warning("```html``` ブロックが見つからず全文返却") return raw # ---------- 画像トリミング ---------- def trim_image_whitespace(image, threshold=250, padding=10): gray = image.convert("L") data = list(gray.getdata()) w, h = gray.size pixels = [data[i * w:(i + 1) * w] for i in range(h)] min_x, min_y, max_x, max_y = w, h, 0, 0 for y in range(h): for x in range(w): if pixels[y][x] < threshold: min_x, min_y = min(min_x, x), min(min_y, y) max_x, max_y = max(max_x, x), max(max_y, y) if min_x > max_x or min_y > max_y: return image min_x = max(0, min_x - padding) min_y = max(0, min_y - padding) max_x = min(w - 1, max_x + padding) max_y = min(h - 1, max_y + padding) return image.crop((min_x, min_y, max_x + 1, max_y + 1)) # ---------- HTML → スクリーンショット(Selenium) ---------- def render_fullpage_screenshot(html_code, extension_percentage=6.0, trim_whitespace=True): tmp_path, driver = None, None try: with tempfile.NamedTemporaryFile(suffix=".html", delete=False, mode="w", encoding="utf-8") as f: tmp_path = f.name f.write(html_code) options = Options() options.add_argument("--headless") options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") driver = webdriver.Chrome(options=options) driver.set_window_size(1200, 1000) driver.get("file://" + tmp_path) WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.TAG_NAME, "body"))) time.sleep(3) total_height = driver.execute_script("return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)") viewport = driver.execute_script("return window.innerHeight") for i in range(max(1, total_height // viewport) + 1): driver.execute_script(f"window.scrollTo(0, {i * (viewport - 200)})") time.sleep(0.2) driver.execute_script("window.scrollTo(0, 0)") driver.execute_script("document.documentElement.style.overflow='hidden';document.body.style.overflow='hidden'") dims = driver.execute_script("return {w:document.documentElement.scrollWidth,h:document.documentElement.scrollHeight}") driver.set_window_size(dims["w"], int(dims["h"] * (1 + extension_percentage / 100))) time.sleep(1) png = driver.get_screenshot_as_png() img = Image.open(BytesIO(png)) return trim_image_whitespace(img, 248, 20) if trim_whitespace else img except Exception as e: logger.error(f"Selenium error: {e}", exc_info=True) return Image.new("RGB", (1, 1)) finally: if driver: driver.quit() if tmp_path and os.path.exists(tmp_path): os.remove(tmp_path) # ---------- 統合 ---------- def text_to_screenshot(text, ext, temp=0.3, trim=True, style="standard"): html = generate_html_from_text(text, temp, style) return render_fullpage_screenshot(html, ext, trim) # ---------- FastAPI ---------- app = FastAPI() app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Gradio 静的ファイル gradio_dir = os.path.dirname(gr.__file__) app.mount("/static", StaticFiles(directory=os.path.join(gradio_dir, "templates/frontend/static")), name="static") # ---------- API ---------- @app.post("/api/screenshot", response_class=StreamingResponse) async def api_render(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) async def api_text_screenshot(req: GeminiRequest): img = text_to_screenshot(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 ---------- def process_input(mode, txt, ext, temp, trim, style): if mode == "HTML入力": return render_fullpage_screenshot(txt, ext, trim) return text_to_screenshot(txt, ext, temp, trim, style) # ---------- Gradio UI ---------- with gr.Blocks(title="Full Page Screenshot (テキスト変換対応)", theme=gr.themes.Base()) as iface: # 見出し gr.Markdown( "