Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,12 @@ import requests
|
|
| 3 |
from selenium import webdriver
|
| 4 |
from bs4 import BeautifulSoup
|
| 5 |
from selenium.webdriver.chrome.options import Options
|
|
|
|
| 6 |
from fastapi import FastAPI, Response, HTTPException
|
| 7 |
-
from fastapi.responses import FileResponse
|
| 8 |
from fastapi.staticfiles import StaticFiles
|
| 9 |
import os
|
|
|
|
| 10 |
|
| 11 |
# Initialize the FastAPI app
|
| 12 |
app = FastAPI()
|
|
@@ -28,6 +30,9 @@ def getimage(url: str) -> str:
|
|
| 28 |
driver = None
|
| 29 |
try:
|
| 30 |
# 2. Initialize the WebDriver
|
|
|
|
|
|
|
|
|
|
| 31 |
driver = webdriver.Chrome(options=chrome_options)
|
| 32 |
|
| 33 |
# 3. Navigate and Wait
|
|
@@ -99,6 +104,7 @@ def fetch_image_endpoint(input_url: str):
|
|
| 99 |
"filename": saved_filename
|
| 100 |
}
|
| 101 |
except Exception as e:
|
|
|
|
| 102 |
raise HTTPException(status_code=500, detail=str(e))
|
| 103 |
|
| 104 |
# Ensure the 'static' directory exists for mounting, preventing the RuntimeError
|
|
@@ -119,3 +125,11 @@ def index() -> FileResponse:
|
|
| 119 |
else:
|
| 120 |
# If running without a UI, just return a simple message
|
| 121 |
return {"message": "Image Scraper API Running. Access /fetch_profile_image?input_url=<URL> to test."}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from selenium import webdriver
|
| 4 |
from bs4 import BeautifulSoup
|
| 5 |
from selenium.webdriver.chrome.options import Options
|
| 6 |
+
from selenium.webdriver.chrome.service import Service # New: Import Service for modern Selenium setup
|
| 7 |
from fastapi import FastAPI, Response, HTTPException
|
| 8 |
+
from fastapi.responses import FileResponse
|
| 9 |
from fastapi.staticfiles import StaticFiles
|
| 10 |
import os
|
| 11 |
+
import uvicorn # Required for explicit startup
|
| 12 |
|
| 13 |
# Initialize the FastAPI app
|
| 14 |
app = FastAPI()
|
|
|
|
| 30 |
driver = None
|
| 31 |
try:
|
| 32 |
# 2. Initialize the WebDriver
|
| 33 |
+
# Note: If 'chromedriver' is not in the system PATH,
|
| 34 |
+
# this will fail. For custom environments, you may need to
|
| 35 |
+
# specify the driver path using Service(executable_path='...')
|
| 36 |
driver = webdriver.Chrome(options=chrome_options)
|
| 37 |
|
| 38 |
# 3. Navigate and Wait
|
|
|
|
| 104 |
"filename": saved_filename
|
| 105 |
}
|
| 106 |
except Exception as e:
|
| 107 |
+
# We catch the RuntimeError raised in getimage and return a 500 status
|
| 108 |
raise HTTPException(status_code=500, detail=str(e))
|
| 109 |
|
| 110 |
# Ensure the 'static' directory exists for mounting, preventing the RuntimeError
|
|
|
|
| 125 |
else:
|
| 126 |
# If running without a UI, just return a simple message
|
| 127 |
return {"message": "Image Scraper API Running. Access /fetch_profile_image?input_url=<URL> to test."}
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
# --- Explicit Uvicorn Startup Block (CRITICAL FIX) ---
|
| 131 |
+
if __name__ == "__main__":
|
| 132 |
+
# This block ensures the application starts listening on a network port,
|
| 133 |
+
# resolving the "application does not seem to be initialized" error.
|
| 134 |
+
# We use 0.0.0.0 for compatibility with containerized/sandboxed environments.
|
| 135 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|