| """ |
| Example usage of UAP Analytics utilities |
| Demonstrates best practices and common workflows |
| """ |
|
|
| import streamlit as st |
| import pandas as pd |
| from utils import ( |
| SessionStateManager, |
| DataProcessor, |
| UAP_Visualizer, |
| MemoryManager, |
| APIKeyValidator, |
| create_uap_analysis_pipeline, |
| log_performance |
| ) |
|
|
| |
| SessionStateManager.initialize() |
|
|
| st.title("UAP Analytics Example") |
|
|
| |
| st.header("1. Data Loading with Memory Optimization") |
|
|
| @st.cache_data |
| @log_performance |
| def load_uap_data(file_path): |
| """Load UAP data with automatic optimization based on file size""" |
| import os |
| |
| file_size_gb = os.path.getsize(file_path) / (1024**3) |
| |
| if file_size_gb > 1: |
| st.info(f"Large file ({file_size_gb:.1f}GB) - using sampling") |
| return MemoryManager.sample_large_dataset(file_path, sample_size=50000) |
| else: |
| data = DataProcessor.load_data(file_path) |
| return MemoryManager.optimize_dataframe_memory(data) |
|
|
| |
| if st.button("Load Sample Data"): |
| try: |
| data = load_uap_data("sample_data.h5") |
| SessionStateManager.set('data', data) |
| st.success(f"Loaded {len(data)} records") |
| except Exception as e: |
| st.error(f"Error loading data: {e}") |
|
|
| |
| st.header("2. Interactive Data Filtering") |
|
|
| if SessionStateManager.exists('data'): |
| data = SessionStateManager.get('data') |
| |
| |
| filtered_data = DataProcessor.filter_dataframe(data) |
| |
| |
| SessionStateManager.set('filtered_data', filtered_data) |
| |
| |
| st.write(f"Filtered to {len(filtered_data)} records") |
| st.dataframe(filtered_data.head()) |
|
|
| |
| st.header("3. API Key Management") |
|
|
| api_key = st.text_input("Enter API Key", type="password") |
| api_provider = st.selectbox("Select Provider", ["openai", "cohere", "gemini"]) |
|
|
| if api_key and st.button("Validate API Key"): |
| |
| cache_key = f"{api_provider}_validated" |
| |
| if SessionStateManager.get(cache_key, False): |
| st.info("API key already validated!") |
| else: |
| with st.spinner("Validating..."): |
| validators = { |
| 'openai': APIKeyValidator.validate_openai_key, |
| 'cohere': APIKeyValidator.validate_cohere_key, |
| 'gemini': APIKeyValidator.validate_gemini_key |
| } |
| |
| if validators[api_provider](api_key): |
| SessionStateManager.set(cache_key, True) |
| st.success("API key is valid!") |
| else: |
| st.error("Invalid API key") |
|
|
| |
| st.header("4. Data Pipeline Processing") |
|
|
| uploaded_file = st.file_uploader("Upload UAP Data", type=['csv', 'h5', 'xlsx']) |
|
|
| if uploaded_file and st.button("Process with Pipeline"): |
| |
| pipeline = create_uap_analysis_pipeline() |
| |
| try: |
| with st.spinner("Running pipeline..."): |
| |
| temp_path = f"temp_{uploaded_file.name}" |
| with open(temp_path, 'wb') as f: |
| f.write(uploaded_file.getbuffer()) |
| |
| |
| result = pipeline.run(temp_path) |
| |
| |
| st.success("Pipeline completed!") |
| st.dataframe(result.head()) |
| |
| |
| summary = pipeline.get_execution_summary() |
| st.write("Execution Summary:") |
| st.dataframe(summary) |
| |
| |
| import os |
| os.remove(temp_path) |
| |
| except Exception as e: |
| st.error(f"Pipeline error: {e}") |
|
|
| |
| st.header("5. Cached Visualizations") |
|
|
| if SessionStateManager.exists('filtered_data'): |
| data = SessionStateManager.get('filtered_data') |
| |
| col1, col2 = st.columns(2) |
| |
| with col1: |
| |
| cat_columns = data.select_dtypes(include=['object', 'category']).columns |
| if len(cat_columns) > 0: |
| selected_cat = st.selectbox("Select category column", cat_columns) |
| if selected_cat: |
| fig = UAP_Visualizer.plot_treemap(data, selected_cat, top_n=15) |
| st.pyplot(fig) |
| |
| with col2: |
| |
| num_columns = data.select_dtypes(include=['number']).columns |
| if len(num_columns) > 0: |
| selected_num = st.selectbox("Select numeric column", num_columns) |
| if selected_num: |
| fig = UAP_Visualizer.plot_hist(data, selected_num) |
| st.pyplot(fig) |
|
|
| |
| st.header("6. Memory Monitoring") |
|
|
| col1, col2, col3 = st.columns(3) |
|
|
| memory_stats = MemoryManager.get_memory_usage() |
|
|
| col1.metric("Memory Used", f"{memory_stats['rss_mb']:.1f} MB") |
| col2.metric("Memory %", f"{memory_stats['percent']:.1f}%") |
| col3.metric("Available", f"{memory_stats['available_mb']:.1f} MB") |
|
|
| if st.button("Clear All Caches"): |
| MemoryManager.clear_memory_cache() |
| SessionStateManager.clear(['data', 'filtered_data']) |
| st.success("Caches cleared!") |
| st.experimental_rerun() |
|
|
| |
| st.header("7. Session State Debugging") |
|
|
| with st.expander("Session State Inspector", expanded=False): |
| |
| st.json(SessionStateManager.get_state_summary()) |
| |
| |
| col1, col2 = st.columns(2) |
| |
| with col1: |
| key = st.text_input("State Key") |
| value = st.text_input("State Value") |
| if st.button("Set State"): |
| SessionStateManager.set(key, value) |
| st.success(f"Set {key} = {value}") |
| |
| with col2: |
| get_key = st.text_input("Get Key") |
| if st.button("Get State"): |
| value = SessionStateManager.get(get_key, "Not found") |
| st.write(f"Value: {value}") |
|
|
| |
| st.markdown("---") |
| st.markdown(""" |
| ### Performance Tips: |
| - 🚀 GPU acceleration is {'**enabled**' if torch.cuda.is_available() else '**disabled**'} |
| - 💾 Use chunked processing for files > 1GB |
| - 🔑 API keys are cached for 1 hour after validation |
| - 📊 Visualizations are cached automatically |
| - 🧹 Clear caches periodically to free memory |
| """) |
|
|
| if __name__ == "__main__": |
| import torch |
| |
| |
| if torch.cuda.is_available(): |
| st.sidebar.success("🚀 GPU Acceleration Active") |
| st.sidebar.write(f"GPU: {torch.cuda.get_device_name(0)}") |
| else: |
| st.sidebar.info("�� Running on CPU") |