Spaces:
Sleeping
Sleeping
API
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
#Fast APi Packages
|
2 |
-
from fastapi import FastAPI,File, HTTPException
|
3 |
from pydantic import BaseModel
|
4 |
import json
|
5 |
from typing import List, Dict, Any
|
@@ -11,7 +11,8 @@ from datetime import datetime
|
|
11 |
import warnings
|
12 |
import os
|
13 |
import logging
|
14 |
-
import
|
|
|
15 |
|
16 |
warnings.filterwarnings('ignore')
|
17 |
|
@@ -21,43 +22,21 @@ logger = logging.getLogger(__name__)
|
|
21 |
|
22 |
app = FastAPI()
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
-
'datasetsample.xlsx', # Same directory
|
27 |
-
'./datasetsample.xlsx', # Explicit current directory
|
28 |
-
'../datasetsample.xlsx', # Parent directory
|
29 |
-
'/home/user/app/datasetsample.xlsx', # Hugging Face default path
|
30 |
-
os.path.join(os.path.dirname(os.path.abspath(__file__)), 'datasetsample.xlsx'), # Absolute path
|
31 |
-
]
|
32 |
-
|
33 |
-
logger.info("Checking directory contents:")
|
34 |
-
current_dir = os.path.dirname(os.path.abspath(__file__))
|
35 |
-
logger.info(f"Current directory: {current_dir}")
|
36 |
-
try:
|
37 |
-
for item in os.listdir(current_dir):
|
38 |
-
logger.info(f"Found file: {item}")
|
39 |
-
except Exception as e:
|
40 |
-
logger.error(f"Error listing directory: {str(e)}")
|
41 |
-
|
42 |
-
# Try each possible path
|
43 |
-
purchase_history = None
|
44 |
-
for path in possible_paths:
|
45 |
-
try:
|
46 |
-
logger.info(f"Attempting to load Excel file from: {path}")
|
47 |
-
purchase_history = pd.read_excel(path, sheet_name='Transaction History',
|
48 |
-
parse_dates=['Purchase_Date'])
|
49 |
-
logger.info(f"Successfully loaded Excel file from: {path}")
|
50 |
-
break
|
51 |
-
except Exception as e:
|
52 |
-
logger.warning(f"Failed to load from {path}: {str(e)}")
|
53 |
-
continue
|
54 |
-
|
55 |
-
if purchase_history is None:
|
56 |
-
error_msg = "Failed to load Excel file from any location"
|
57 |
-
logger.error(error_msg)
|
58 |
-
raise FileNotFoundError(error_msg)
|
59 |
|
60 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
# Process the data
|
62 |
purchase_history['Customer_Id'] = purchase_history['Customer_Id'].astype(str)
|
63 |
product_categories = purchase_history[['Product_Id', 'Category']].drop_duplicates().set_index('Product_Id')['Category'].to_dict()
|
@@ -68,7 +47,7 @@ try:
|
|
68 |
logger.info("Data processing completed successfully")
|
69 |
|
70 |
except Exception as e:
|
71 |
-
logger.error(f"Error processing data: {str(e)}")
|
72 |
raise
|
73 |
|
74 |
def get_customer_items_and_recommendations(user_id: str, n: int = 5) -> tuple[List[Dict], List[Dict]]:
|
@@ -148,24 +127,14 @@ async def get_recommendations(customer_id: str, n: int = 5):
|
|
148 |
logger.error(f"Error processing request for customer {customer_id}: {str(e)}")
|
149 |
raise HTTPException(status_code=404, detail=f"Error processing customer ID: {customer_id}. {str(e)}")
|
150 |
|
151 |
-
# Add a health check endpoint that includes file system information
|
152 |
@app.get("/health")
|
153 |
async def health_check():
|
154 |
"""
|
155 |
Health check endpoint that returns system information
|
156 |
"""
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
"current_directory": current_dir,
|
164 |
-
"files_found": files,
|
165 |
-
"data_loaded": purchase_history is not None
|
166 |
-
}
|
167 |
-
except Exception as e:
|
168 |
-
return {
|
169 |
-
"status": "unhealthy",
|
170 |
-
"error": str(e)
|
171 |
-
}
|
|
|
1 |
#Fast APi Packages
|
2 |
+
from fastapi import FastAPI, File, HTTPException
|
3 |
from pydantic import BaseModel
|
4 |
import json
|
5 |
from typing import List, Dict, Any
|
|
|
11 |
import warnings
|
12 |
import os
|
13 |
import logging
|
14 |
+
import requests
|
15 |
+
import io
|
16 |
|
17 |
warnings.filterwarnings('ignore')
|
18 |
|
|
|
22 |
|
23 |
app = FastAPI()
|
24 |
|
25 |
+
# URL of the Excel file
|
26 |
+
EXCEL_URL = "https://huggingface.co/spaces/Vaibhav84/RecommendationAPI/resolve/main/DataSetSample.xlsx"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
try:
|
29 |
+
# Download the file from URL
|
30 |
+
logger.info(f"Attempting to download Excel file from: {EXCEL_URL}")
|
31 |
+
response = requests.get(EXCEL_URL)
|
32 |
+
response.raise_for_status() # Raises an HTTPError if the status is 4xx, 5xx
|
33 |
+
|
34 |
+
# Read the Excel file from the downloaded content
|
35 |
+
excel_content = io.BytesIO(response.content)
|
36 |
+
purchase_history = pd.read_excel(excel_content, sheet_name='Transaction History',
|
37 |
+
parse_dates=['Purchase_Date'])
|
38 |
+
logger.info("Successfully downloaded and loaded Excel file")
|
39 |
+
|
40 |
# Process the data
|
41 |
purchase_history['Customer_Id'] = purchase_history['Customer_Id'].astype(str)
|
42 |
product_categories = purchase_history[['Product_Id', 'Category']].drop_duplicates().set_index('Product_Id')['Category'].to_dict()
|
|
|
47 |
logger.info("Data processing completed successfully")
|
48 |
|
49 |
except Exception as e:
|
50 |
+
logger.error(f"Error downloading or processing data: {str(e)}")
|
51 |
raise
|
52 |
|
53 |
def get_customer_items_and_recommendations(user_id: str, n: int = 5) -> tuple[List[Dict], List[Dict]]:
|
|
|
127 |
logger.error(f"Error processing request for customer {customer_id}: {str(e)}")
|
128 |
raise HTTPException(status_code=404, detail=f"Error processing customer ID: {customer_id}. {str(e)}")
|
129 |
|
|
|
130 |
@app.get("/health")
|
131 |
async def health_check():
|
132 |
"""
|
133 |
Health check endpoint that returns system information
|
134 |
"""
|
135 |
+
return {
|
136 |
+
"status": "healthy",
|
137 |
+
"data_loaded": purchase_history is not None,
|
138 |
+
"number_of_customers": len(purchase_counts.index) if purchase_history is not None else 0,
|
139 |
+
"number_of_products": len(purchase_counts.columns) if purchase_history is not None else 0
|
140 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|