Spaces:
Sleeping
Sleeping
cjell
commited on
Commit
·
8f81868
1
Parent(s):
a3fe9c3
idek
Browse files- Dockerfile +22 -2
- requirements.txt +4 -3
- test_local.py +62 -0
Dockerfile
CHANGED
|
@@ -1,10 +1,30 @@
|
|
| 1 |
-
FROM python:3.10
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
WORKDIR /code
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
COPY requirements.txt .
|
|
|
|
| 6 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
|
|
|
|
| 8 |
COPY . .
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
# Set environment variables
|
| 4 |
+
ENV PYTHONUNBUFFERED=1
|
| 5 |
+
ENV HF_HOME=/tmp
|
| 6 |
|
| 7 |
WORKDIR /code
|
| 8 |
|
| 9 |
+
# Install system dependencies
|
| 10 |
+
RUN apt-get update && apt-get install -y \
|
| 11 |
+
curl \
|
| 12 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 13 |
+
|
| 14 |
+
# Copy and install Python dependencies
|
| 15 |
COPY requirements.txt .
|
| 16 |
+
RUN pip install --no-cache-dir --upgrade pip
|
| 17 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 18 |
|
| 19 |
+
# Copy application code
|
| 20 |
COPY . .
|
| 21 |
|
| 22 |
+
# Expose port
|
| 23 |
+
EXPOSE 7860
|
| 24 |
+
|
| 25 |
+
# Health check
|
| 26 |
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
|
| 27 |
+
CMD curl -f http://localhost:7860/ || exit 1
|
| 28 |
+
|
| 29 |
+
# Run the application
|
| 30 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--log-level", "info"]
|
requirements.txt
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
fastapi
|
| 2 |
-
uvicorn
|
| 3 |
transformers
|
| 4 |
-
torch
|
| 5 |
-
pydantic>=
|
| 6 |
huggingface_hub
|
|
|
|
|
|
| 1 |
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
transformers
|
| 4 |
+
torch --index-url https://download.pytorch.org/whl/cpu
|
| 5 |
+
pydantic>=2.0
|
| 6 |
huggingface_hub
|
| 7 |
+
requests
|
test_local.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Test script to verify FastAPI app works locally
|
| 3 |
+
"""
|
| 4 |
+
import subprocess
|
| 5 |
+
import time
|
| 6 |
+
import requests
|
| 7 |
+
import threading
|
| 8 |
+
import sys
|
| 9 |
+
|
| 10 |
+
def start_server():
|
| 11 |
+
"""Start the FastAPI server in background"""
|
| 12 |
+
try:
|
| 13 |
+
subprocess.run([
|
| 14 |
+
sys.executable, "-m", "uvicorn",
|
| 15 |
+
"app:app", "--host", "127.0.0.1", "--port", "8000"
|
| 16 |
+
], check=False)
|
| 17 |
+
except Exception as e:
|
| 18 |
+
print(f"Server failed to start: {e}")
|
| 19 |
+
|
| 20 |
+
def test_endpoints():
|
| 21 |
+
"""Test the API endpoints"""
|
| 22 |
+
base_url = "http://127.0.0.1:8000"
|
| 23 |
+
|
| 24 |
+
# Wait a moment for server to start
|
| 25 |
+
time.sleep(3)
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
# Test health endpoint
|
| 29 |
+
print("🔹 Testing health endpoint...")
|
| 30 |
+
health_resp = requests.get(base_url, timeout=5)
|
| 31 |
+
print(f"Health Status: {health_resp.status_code}")
|
| 32 |
+
if health_resp.status_code == 200:
|
| 33 |
+
print(f"Health Response: {health_resp.json()}")
|
| 34 |
+
else:
|
| 35 |
+
print(f"Health Error: {health_resp.text}")
|
| 36 |
+
|
| 37 |
+
# Test prediction endpoint
|
| 38 |
+
print("\n🔹 Testing prediction endpoint...")
|
| 39 |
+
pred_resp = requests.post(
|
| 40 |
+
base_url,
|
| 41 |
+
json={"text": "Congratulations! You've won a free cruise!"},
|
| 42 |
+
headers={"Content-Type": "application/json"},
|
| 43 |
+
timeout=10
|
| 44 |
+
)
|
| 45 |
+
print(f"Prediction Status: {pred_resp.status_code}")
|
| 46 |
+
if pred_resp.status_code == 200:
|
| 47 |
+
print(f"Prediction Response: {pred_resp.json()}")
|
| 48 |
+
else:
|
| 49 |
+
print(f"Prediction Error: {pred_resp.text}")
|
| 50 |
+
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"Test failed: {e}")
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
print("Starting FastAPI server...")
|
| 56 |
+
|
| 57 |
+
# Start server in a separate thread
|
| 58 |
+
server_thread = threading.Thread(target=start_server, daemon=True)
|
| 59 |
+
server_thread.start()
|
| 60 |
+
|
| 61 |
+
# Run tests
|
| 62 |
+
test_endpoints()
|