Spaces:
Sleeping
CSV-First Deployment for Hugging Face Spaces
This document explains how to deploy your Optimus Agent Performance dashboard to Hugging Face Spaces using a CSV-first approach to avoid API rate limiting issues.
Overview
The CSV-first approach works by:
- Local Data Generation: Run data fetching and preprocessing locally where you have full API access
- CSV Export: Save preprocessed data to CSV files
- Space Deployment: Upload CSV files to your Hugging Face Space
- CSV-First Loading: The Space app prioritizes loading from CSV files over making API calls
Files Added for CSV-First Deployment
1. load_from_csv.py
Contains functions to load data from CSV files:
load_apr_data_from_csv()
- Loads APR data from CSVload_roi_data_from_csv()
- Loads ROI data from CSVload_statistics_from_csv()
- Loads statistics from CSVcheck_csv_data_availability()
- Checks which CSV files are availableget_data_freshness_info()
- Analyzes how fresh the CSV data is
2. generate_csv_for_space.py
Interactive script to generate CSV files locally:
- Fetches fresh data from the API
- Applies all preprocessing (including
initial_value_fixer.py
) - Saves to CSV files ready for Space deployment
- Provides deployment guidance
3. Modified app.py
Updated visualization functions with CSV-first logic:
generate_apr_visualizations()
- Now tries CSV first, falls back to APIgenerate_roi_visualizations()
- Now tries CSV first, falls back to API- Imports CSV loading functions
- Maintains backward compatibility
Deployment Workflow
Step 1: Generate CSV Files Locally
Run the CSV generation script on your local machine:
python generate_csv_for_space.py
This will:
- Check existing CSV files and their freshness
- Fetch fresh data from the API (no rate limits locally)
- Apply preprocessing using
initial_value_fixer.py
- Save CSV files:
optimus_apr_values.csv
,optimus_apr_statistics.csv
,optimus_roi_values.csv
Step 2: Upload Files to Hugging Face Space
Upload these files to your Hugging Face Space repository:
app.py
(modified with CSV-first logic)load_from_csv.py
(new CSV loading functions)initial_value_fixer.py
(existing preprocessing)fetch_and_preprocess_data.py
(existing data functions)optimus_apr_values.csv
(generated data)optimus_apr_statistics.csv
(generated statistics)optimus_roi_values.csv
(generated data)
Step 3: Deploy Space
Your Space will now:
- Try CSV first: Load data from uploaded CSV files
- Fast loading: No API calls needed, instant visualization
- Fallback: If CSV files are missing, fall back to API (with rate limits)
- Error handling: Show appropriate messages if both CSV and API fail
CSV File Structure
APR Data (optimus_apr_values.csv
)
apr,adjusted_apr,timestamp,agent_id,agent_name,is_dummy,metric_type,roi,address
15.5,12.3,2025-06-14 10:30:00,123,Agent_1,False,APR,0.85,0x123...
ROI Data (optimus_roi_values.csv
)
roi,timestamp,agent_id,agent_name,is_dummy,metric_type
0.85,2025-06-14 10:30:00,123,Agent_1,False,ROI
Statistics (optimus_apr_statistics.csv
)
agent_id,agent_name,total_points,apr_points,avg_apr,avg_adjusted_apr,latest_timestamp
123,Agent_1,100,50,15.5,12.3,2025-06-14 10:30:00
Benefits
β Advantages
- No Rate Limits: Space doesn't make API calls
- Fast Loading: Instant visualization from CSV
- Reliable: No dependency on external API availability
- Preprocessed: Data includes all transformations from
initial_value_fixer.py
- Fresh Data: Update CSV files as needed locally
β οΈ Considerations
- Manual Updates: Need to regenerate CSV files for fresh data
- File Size: CSV files add to Space storage
- Data Staleness: CSV data becomes stale over time
Updating Data
To update your Space with fresh data:
- Run locally:
python generate_csv_for_space.py
- Upload new CSV files to your Space repository
- Space auto-updates with new data
Monitoring
Data Freshness
The app logs data freshness information:
from load_from_csv import get_data_freshness_info
freshness = get_data_freshness_info()
# Shows how old the CSV data is
CSV Availability
Check which CSV files are available:
from load_from_csv import check_csv_data_availability
availability = check_csv_data_availability()
# Shows file status, size, modification date
Troubleshooting
CSV Files Not Loading
- Check file names match exactly:
optimus_apr_values.csv
,optimus_roi_values.csv
- Verify CSV files are in the Space root directory
- Check Space logs for CSV loading errors
Data Issues
- Regenerate CSV files locally with fresh API data
- Verify preprocessing ran correctly in
generate_csv_for_space.py
- Check CSV file structure matches expected format
Fallback to API
If CSV loading fails, the app falls back to API calls:
- This may trigger rate limits in Hugging Face Spaces
- Check Space logs for API errors
- Ensure CSV files are properly uploaded
Best Practices
- Regular Updates: Run
generate_csv_for_space.py
daily or weekly - Monitor Logs: Check Space logs for CSV loading status
- Backup CSV: Keep local copies of generated CSV files
- Test Locally: Test CSV loading locally before deploying
- Version Control: Track CSV generation timestamps
Example Space Configuration
Your Space should include these files:
βββ app.py # Modified with CSV-first logic
βββ load_from_csv.py # CSV loading functions
βββ initial_value_fixer.py # Preprocessing logic
βββ fetch_and_preprocess_data.py # Data functions
βββ optimus_apr_values.csv # Generated APR data
βββ optimus_apr_statistics.csv # Generated statistics
βββ optimus_roi_values.csv # Generated ROI data
βββ requirements.txt # Dependencies
βββ README.md # Space documentation
This approach ensures your Hugging Face Space runs reliably without hitting API rate limits while maintaining all the preprocessing and visualization features of your application.