Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +32 -2
- requirements.txt +6 -0
app.py
CHANGED
|
@@ -5,6 +5,8 @@ import gradio as gr
|
|
| 5 |
import ast
|
| 6 |
from functools import lru_cache
|
| 7 |
from collections import Counter
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# --- Constants and Mappings (Unchanged) ---
|
| 10 |
BODY_ORDER = ['Very light-bodied', 'Light-bodied', 'Medium-bodied', 'Full-bodied', 'Very full-bodied']
|
|
@@ -27,14 +29,42 @@ FOOD_EMOJIS = {
|
|
| 27 |
}
|
| 28 |
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# --- OPTIMIZATION 1: Data Loading & Pre-processing ---
|
| 31 |
@lru_cache(maxsize=1)
|
| 32 |
def load_and_preprocess_data():
|
| 33 |
"""Loads and performs expensive one-time preprocessing on the dataset."""
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
-
df = pd.read_csv(
|
| 36 |
except FileNotFoundError:
|
| 37 |
-
raise FileNotFoundError("CSV file '
|
| 38 |
|
| 39 |
def parse_list_string(s):
|
| 40 |
try:
|
|
|
|
| 5 |
import ast
|
| 6 |
from functools import lru_cache
|
| 7 |
from collections import Counter
|
| 8 |
+
import requests
|
| 9 |
+
import os
|
| 10 |
|
| 11 |
# --- Constants and Mappings (Unchanged) ---
|
| 12 |
BODY_ORDER = ['Very light-bodied', 'Light-bodied', 'Medium-bodied', 'Full-bodied', 'Very full-bodied']
|
|
|
|
| 29 |
}
|
| 30 |
|
| 31 |
|
| 32 |
+
# --- Data Download Function ---
|
| 33 |
+
def download_data():
|
| 34 |
+
"""Downloads the dataset from Google Drive if not already present."""
|
| 35 |
+
csv_filename = 'XWines_Full_100K_wines.csv'
|
| 36 |
+
|
| 37 |
+
if os.path.exists(csv_filename):
|
| 38 |
+
return csv_filename
|
| 39 |
+
|
| 40 |
+
# Convert Google Drive share link to direct download link
|
| 41 |
+
file_id = '1uEEipmKNxdiKUAhjH-K14JOSQ2BLRFss'
|
| 42 |
+
download_url = f'https://drive.google.com/uc?export=download&id={file_id}'
|
| 43 |
+
|
| 44 |
+
print(f"Downloading dataset from Google Drive...")
|
| 45 |
+
try:
|
| 46 |
+
response = requests.get(download_url, stream=True)
|
| 47 |
+
response.raise_for_status()
|
| 48 |
+
|
| 49 |
+
with open(csv_filename, 'wb') as f:
|
| 50 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 51 |
+
f.write(chunk)
|
| 52 |
+
|
| 53 |
+
print(f"Dataset downloaded successfully: {csv_filename}")
|
| 54 |
+
return csv_filename
|
| 55 |
+
except Exception as e:
|
| 56 |
+
raise Exception(f"Failed to download dataset: {str(e)}")
|
| 57 |
+
|
| 58 |
# --- OPTIMIZATION 1: Data Loading & Pre-processing ---
|
| 59 |
@lru_cache(maxsize=1)
|
| 60 |
def load_and_preprocess_data():
|
| 61 |
"""Loads and performs expensive one-time preprocessing on the dataset."""
|
| 62 |
+
csv_filename = download_data()
|
| 63 |
+
|
| 64 |
try:
|
| 65 |
+
df = pd.read_csv(csv_filename)
|
| 66 |
except FileNotFoundError:
|
| 67 |
+
raise FileNotFoundError(f"CSV file '{csv_filename}' not found.")
|
| 68 |
|
| 69 |
def parse_list_string(s):
|
| 70 |
try:
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
plotly
|
| 3 |
+
gradio
|
| 4 |
+
numpy
|
| 5 |
+
scipy
|
| 6 |
+
requests
|