Update README, app.py, and Dockerfile for AI Product Helper configuration
Browse files- README.md +9 -8
- app.py +12 -12
- dockerfile +1 -1
README.md
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk:
|
| 7 |
-
app_port: 7860
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
-
short_description: Test
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
# filepath: README.md
|
| 2 |
---
|
| 3 |
+
title: AI Product Helper
|
| 4 |
+
emoji: 🚀
|
| 5 |
+
colorFrom: blue
|
| 6 |
+
colorTo: green
|
| 7 |
+
sdk: fastapi
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# AI Product Helper API
|
| 13 |
+
|
| 14 |
+
This Space hosts the API for the AI Product Helper application.
|
app.py
CHANGED
|
@@ -18,7 +18,7 @@ app = FastAPI()
|
|
| 18 |
|
| 19 |
# Load environment variables
|
| 20 |
load_dotenv()
|
| 21 |
-
API_KEY = os.getenv("
|
| 22 |
if not API_KEY:
|
| 23 |
raise ValueError("API_KEY not set. Please configure your .env file or system environment.")
|
| 24 |
|
|
@@ -29,26 +29,26 @@ tokenizer = None
|
|
| 29 |
executor = ThreadPoolExecutor(max_workers=4)
|
| 30 |
|
| 31 |
# Ensure ONNX model path is set
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
async def download_model_from_hf(repo_id: str, filename: str) -> str:
|
| 35 |
try:
|
| 36 |
-
#
|
| 37 |
-
model_dir = os.path.join(tempfile.gettempdir(), "hf_models")
|
| 38 |
-
os.makedirs(model_dir, exist_ok=True)
|
| 39 |
-
|
| 40 |
-
# Download model
|
| 41 |
model_path = hf_hub_download(
|
| 42 |
repo_id=repo_id,
|
| 43 |
filename=filename,
|
| 44 |
-
cache_dir=
|
| 45 |
-
local_dir=
|
| 46 |
-
force_download=
|
| 47 |
)
|
| 48 |
-
print(f"
|
| 49 |
return model_path
|
| 50 |
except Exception as e:
|
| 51 |
-
print(f"Error downloading {filename}: {str(e)}")
|
| 52 |
raise
|
| 53 |
|
| 54 |
|
|
|
|
| 18 |
|
| 19 |
# Load environment variables
|
| 20 |
load_dotenv()
|
| 21 |
+
API_KEY = os.getenv("APIKEY")
|
| 22 |
if not API_KEY:
|
| 23 |
raise ValueError("API_KEY not set. Please configure your .env file or system environment.")
|
| 24 |
|
|
|
|
| 29 |
executor = ThreadPoolExecutor(max_workers=4)
|
| 30 |
|
| 31 |
# Ensure ONNX model path is set
|
| 32 |
+
HF_CACHE_DIR = "/app/hf_models_cache"
|
| 33 |
+
os.makedirs(HF_CACHE_DIR, exist_ok=True)
|
| 34 |
+
|
| 35 |
+
os.environ["XDG_CACHE_HOME"] = "/app/onnx_cache"
|
| 36 |
+
os.makedirs(os.environ["XDG_CACHE_HOME"], exist_ok=True)
|
| 37 |
|
| 38 |
async def download_model_from_hf(repo_id: str, filename: str) -> str:
|
| 39 |
try:
|
| 40 |
+
# Use the defined cache directory
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
model_path = hf_hub_download(
|
| 42 |
repo_id=repo_id,
|
| 43 |
filename=filename,
|
| 44 |
+
cache_dir=HF_CACHE_DIR, # Use defined cache dir
|
| 45 |
+
local_dir=HF_CACHE_DIR, # Ensure download to this dir
|
| 46 |
+
force_download=False # Avoid re-downloading if already cached
|
| 47 |
)
|
| 48 |
+
print(f"Using model {filename} from {model_path}")
|
| 49 |
return model_path
|
| 50 |
except Exception as e:
|
| 51 |
+
print(f"Error downloading/finding {filename}: {str(e)}")
|
| 52 |
raise
|
| 53 |
|
| 54 |
|
dockerfile
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
# you will also find guides on how best to write your Dockerfile
|
| 3 |
|
| 4 |
-
FROM python:3.
|
| 5 |
|
| 6 |
RUN useradd -m -u 1000 user
|
| 7 |
USER user
|
|
|
|
| 1 |
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
# you will also find guides on how best to write your Dockerfile
|
| 3 |
|
| 4 |
+
FROM python:3.11-slim
|
| 5 |
|
| 6 |
RUN useradd -m -u 1000 user
|
| 7 |
USER user
|