Upload 3 files
Browse files- Dockerfile +41 -0
- main.py +47 -0
- requirements.txt +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
|
| 3 |
+
# システムのアップデート & Playwright 用依存ライブラリのインストール
|
| 4 |
+
RUN apt-get update && apt-get install -y \
|
| 5 |
+
libnss3 \
|
| 6 |
+
libatk1.0-0 \
|
| 7 |
+
libatk-bridge2.0-0 \
|
| 8 |
+
libcups2 \
|
| 9 |
+
libdrm2 \
|
| 10 |
+
libxkbcommon0 \
|
| 11 |
+
libwayland-egl1 \
|
| 12 |
+
libwayland-cursor0 \
|
| 13 |
+
libwayland-server0 \
|
| 14 |
+
libgbm1 \
|
| 15 |
+
fonts-liberation \
|
| 16 |
+
libasound2 \
|
| 17 |
+
wget \
|
| 18 |
+
git \
|
| 19 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 20 |
+
|
| 21 |
+
WORKDIR /code
|
| 22 |
+
|
| 23 |
+
# requirements.txt をコピーしてインストール
|
| 24 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 25 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 26 |
+
|
| 27 |
+
# Playwright でブラウザをインストール
|
| 28 |
+
RUN python -m playwright install
|
| 29 |
+
|
| 30 |
+
# Hugging Face Spaces で実行ユーザーを合わせるための設定
|
| 31 |
+
RUN useradd -m -u 1000 user
|
| 32 |
+
USER user
|
| 33 |
+
ENV HOME=/home/user \
|
| 34 |
+
PATH=/home/user/.local/bin:$PATH
|
| 35 |
+
WORKDIR $HOME/app
|
| 36 |
+
|
| 37 |
+
# ソースコードをコピー
|
| 38 |
+
COPY --chown=user . $HOME/app
|
| 39 |
+
|
| 40 |
+
# コンテナ起動時に FastAPI を立ち上げる
|
| 41 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
from playwright.sync_api import sync_playwright
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
@app.get("/")
|
| 9 |
+
def read_root():
|
| 10 |
+
"""
|
| 11 |
+
動作確認用のエンドポイント。
|
| 12 |
+
"""
|
| 13 |
+
return {"message": "Hello from Hugging Face Spaces. Visit /get_mhtml to save MHTML."}
|
| 14 |
+
|
| 15 |
+
@app.get("/get_mhtml")
|
| 16 |
+
def get_mhtml():
|
| 17 |
+
"""
|
| 18 |
+
https://civitai.com/models/1055452/akashicpulse を開き、
|
| 19 |
+
ブラウザレンダリング後のページを MHTML として保存。
|
| 20 |
+
"""
|
| 21 |
+
url = "https://civitai.com/models/1055452/akashicpulse"
|
| 22 |
+
output_path = "akashicpulse.mhtml"
|
| 23 |
+
try:
|
| 24 |
+
with sync_playwright() as p:
|
| 25 |
+
browser = p.chromium.launch(headless=True)
|
| 26 |
+
page = browser.new_page()
|
| 27 |
+
page.goto(url, wait_until="networkidle")
|
| 28 |
+
|
| 29 |
+
# Chrome DevTools Protocol セッションを作成し、MHTML 取得
|
| 30 |
+
cdp_session = page.context.new_cdp_session(page)
|
| 31 |
+
snapshot = cdp_session.send("Page.captureSnapshot", {"format": "mhtml"})
|
| 32 |
+
|
| 33 |
+
browser.close()
|
| 34 |
+
|
| 35 |
+
# ファイルに書き出し
|
| 36 |
+
with open(output_path, "w", encoding="utf-8") as f:
|
| 37 |
+
f.write(snapshot)
|
| 38 |
+
|
| 39 |
+
return {
|
| 40 |
+
"status": "success",
|
| 41 |
+
"message": f"MHTML saved to {output_path}",
|
| 42 |
+
}
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return {
|
| 45 |
+
"status": "error",
|
| 46 |
+
"message": str(e),
|
| 47 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
playwright
|