Spaces:
Running
Running
Commit ·
11256ba
1
Parent(s): 609c209
refactor: migrate architecture to fastapi for hugging face spaces deployment
Browse files- Dockerfile +27 -4
- main.py +57 -0
- requirements.txt +2 -0
Dockerfile
CHANGED
|
@@ -1,4 +1,27 @@
|
|
| 1 |
-
FROM
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM mcr.microsoft.com/playwright/python:v1.42.0-jammy
|
| 2 |
+
|
| 3 |
+
# Create the user required by Hugging Face Spaces (user ID 1000)
|
| 4 |
+
RUN useradd -m -u 1000 user
|
| 5 |
+
USER user
|
| 6 |
+
ENV HOME=/home/user \
|
| 7 |
+
PATH=/home/user/.local/bin:$PATH
|
| 8 |
+
WORKDIR $HOME/app
|
| 9 |
+
|
| 10 |
+
# Install Python dependencies first (for caching)
|
| 11 |
+
COPY --chown=user:user requirements.txt $HOME/app/
|
| 12 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 13 |
+
|
| 14 |
+
# Install Playwright browsers (if not already fully packaged)
|
| 15 |
+
RUN playwright install chromium
|
| 16 |
+
|
| 17 |
+
# Copy the rest of the application
|
| 18 |
+
COPY --chown=user:user . $HOME/app/
|
| 19 |
+
|
| 20 |
+
# Ensure the cache directory exists and is writable
|
| 21 |
+
RUN mkdir -p $HOME/app/assets/cache_png
|
| 22 |
+
|
| 23 |
+
# Hugging Face Spaces route traffic to port 7860
|
| 24 |
+
EXPOSE 7860
|
| 25 |
+
|
| 26 |
+
# Run the FastAPI server
|
| 27 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import os
|
| 3 |
+
from contextlib import asynccontextmanager
|
| 4 |
+
from fastapi import FastAPI
|
| 5 |
+
from fastapi.staticfiles import StaticFiles
|
| 6 |
+
from fastapi.responses import FileResponse
|
| 7 |
+
import logging
|
| 8 |
+
|
| 9 |
+
from scripts import scrape
|
| 10 |
+
from scripts import scrape_crypto
|
| 11 |
+
|
| 12 |
+
logging.basicConfig(level=logging.INFO)
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
+
|
| 15 |
+
async def background_scraper_loop():
|
| 16 |
+
logger.info("Background scraper loop started.")
|
| 17 |
+
while True:
|
| 18 |
+
try:
|
| 19 |
+
logger.info("Running market scraper...")
|
| 20 |
+
await scrape.run_async()
|
| 21 |
+
|
| 22 |
+
logger.info("Running crypto scraper...")
|
| 23 |
+
# Crypto scraper is synchronous, so we run it in a thread to prevent blocking
|
| 24 |
+
await asyncio.to_thread(scrape_crypto.run)
|
| 25 |
+
|
| 26 |
+
logger.info("Scraping cycle complete. Sleeping for 10 minutes.")
|
| 27 |
+
except Exception as e:
|
| 28 |
+
logger.error(f"Error during scraping cycle: {e}")
|
| 29 |
+
|
| 30 |
+
# Sleep for 10 minutes (600 seconds)
|
| 31 |
+
await asyncio.sleep(600)
|
| 32 |
+
|
| 33 |
+
@asynccontextmanager
|
| 34 |
+
async def lifespan(app: FastAPI):
|
| 35 |
+
# Startup: Start the background task
|
| 36 |
+
task = asyncio.create_task(background_scraper_loop())
|
| 37 |
+
yield
|
| 38 |
+
# Shutdown: Cancel the task
|
| 39 |
+
task.cancel()
|
| 40 |
+
|
| 41 |
+
app = FastAPI(lifespan=lifespan)
|
| 42 |
+
|
| 43 |
+
# Ensure cache directory exists before mounting
|
| 44 |
+
os.makedirs('assets/cache_png', exist_ok=True)
|
| 45 |
+
|
| 46 |
+
# Mount directories as static files
|
| 47 |
+
app.mount("/assets", StaticFiles(directory="assets"), name="assets")
|
| 48 |
+
app.mount("/css", StaticFiles(directory="css"), name="css")
|
| 49 |
+
app.mount("/src", StaticFiles(directory="src"), name="src")
|
| 50 |
+
|
| 51 |
+
@app.get("/")
|
| 52 |
+
async def root():
|
| 53 |
+
return FileResponse("index.html")
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
import uvicorn
|
| 57 |
+
uvicorn.run("main:app", host="0.0.0.0", port=7860)
|
requirements.txt
CHANGED
|
@@ -1 +1,3 @@
|
|
| 1 |
playwright==1.42.0
|
|
|
|
|
|
|
|
|
| 1 |
playwright==1.42.0
|
| 2 |
+
fastapi>=0.100.0
|
| 3 |
+
uvicorn>=0.23.0
|