Upload 3 files
Browse files- Dockerfile +4 -9
- main.py +12 -27
Dockerfile
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
FROM python:3.9
|
| 2 |
|
| 3 |
-
#
|
| 4 |
RUN apt-get update && apt-get install -y \
|
| 5 |
libnss3 \
|
| 6 |
libatk1.0-0 \
|
|
@@ -14,28 +14,23 @@ RUN apt-get update && apt-get install -y \
|
|
| 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"]
|
|
|
|
| 1 |
FROM python:3.9
|
| 2 |
|
| 3 |
+
# 必要なライブラリのインストール
|
| 4 |
RUN apt-get update && apt-get install -y \
|
| 5 |
libnss3 \
|
| 6 |
libatk1.0-0 \
|
|
|
|
| 14 |
libgbm1 \
|
| 15 |
fonts-liberation \
|
| 16 |
libasound2 \
|
|
|
|
|
|
|
| 17 |
&& rm -rf /var/lib/apt/lists/*
|
| 18 |
|
| 19 |
WORKDIR /code
|
| 20 |
|
|
|
|
| 21 |
COPY ./requirements.txt /code/requirements.txt
|
| 22 |
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 23 |
|
| 24 |
+
# Playwright ブラウザ本体をインストール
|
| 25 |
RUN python -m playwright install
|
| 26 |
|
| 27 |
+
# Hugging Face Spaces の実行ユーザーと作業ディレクトリ
|
| 28 |
RUN useradd -m -u 1000 user
|
| 29 |
USER user
|
| 30 |
ENV HOME=/home/user \
|
| 31 |
PATH=/home/user/.local/bin:$PATH
|
|
|
|
| 32 |
|
| 33 |
+
WORKDIR $HOME/app
|
| 34 |
COPY --chown=user . $HOME/app
|
| 35 |
|
|
|
|
| 36 |
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
CHANGED
|
@@ -1,47 +1,32 @@
|
|
| 1 |
-
import os
|
| 2 |
-
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from playwright.sync_api import sync_playwright
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
@app.
|
| 9 |
-
def
|
| 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 |
-
|
| 30 |
-
|
| 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 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from playwright.sync_api import sync_playwright
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
|
| 6 |
+
@app.on_event("startup")
|
| 7 |
+
def on_startup():
|
| 8 |
+
# 起動時に自動的にMHTMLを取得
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
url = "https://civitai.com/models/1055452/akashicpulse"
|
| 10 |
output_path = "akashicpulse.mhtml"
|
| 11 |
+
|
| 12 |
try:
|
| 13 |
with sync_playwright() as p:
|
| 14 |
browser = p.chromium.launch(headless=True)
|
| 15 |
page = browser.new_page()
|
| 16 |
page.goto(url, wait_until="networkidle")
|
| 17 |
|
| 18 |
+
cdp = page.context.new_cdp_session(page)
|
| 19 |
+
snapshot = cdp.send("Page.captureSnapshot", {"format": "mhtml"})
|
|
|
|
| 20 |
|
| 21 |
browser.close()
|
| 22 |
|
|
|
|
| 23 |
with open(output_path, "w", encoding="utf-8") as f:
|
| 24 |
f.write(snapshot)
|
| 25 |
+
print(f"[INFO] MHTML saved to: {output_path}")
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
+
print(f"[ERROR] Failed to save MHTML: {e}")
|
| 29 |
+
|
| 30 |
+
@app.get("/")
|
| 31 |
+
def read_root():
|
| 32 |
+
return {"message": "Startup event attempted to save MHTML to akashicpulse.mhtml."}
|