Create demo.py
Browse files
demo.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Example script to run the demo without AI model dependencies for local testing
|
| 2 |
+
# Saves this as demo.py
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from app import read_file, analyze_data, generate_visualizations, display_analysis
|
| 6 |
+
|
| 7 |
+
def simple_process_file(file):
|
| 8 |
+
"""Simplified version without AI models for testing"""
|
| 9 |
+
# Read the file
|
| 10 |
+
df = read_file(file)
|
| 11 |
+
|
| 12 |
+
if isinstance(df, str): # If error message
|
| 13 |
+
return df, None, None, None
|
| 14 |
+
|
| 15 |
+
# Analyze data
|
| 16 |
+
analysis = analyze_data(df)
|
| 17 |
+
|
| 18 |
+
# Generate visualizations
|
| 19 |
+
visualizations = generate_visualizations(df)
|
| 20 |
+
|
| 21 |
+
# Placeholder for AI recommendations
|
| 22 |
+
cleaning_recommendations = """
|
| 23 |
+
## Data Cleaning Recommendations
|
| 24 |
+
|
| 25 |
+
* Handle missing values by either removing rows or imputing with mean/median/mode
|
| 26 |
+
* Remove duplicate rows if present
|
| 27 |
+
* Convert date-like string columns to proper datetime format
|
| 28 |
+
* Standardize text data by removing extra spaces and converting to lowercase
|
| 29 |
+
* Check for and handle outliers in numerical columns
|
| 30 |
+
|
| 31 |
+
Note: This is a demo recommendation (AI model not connected in demo mode)
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
# Placeholder for AI insights
|
| 35 |
+
analysis_insights = """
|
| 36 |
+
## Data Analysis Insights
|
| 37 |
+
|
| 38 |
+
1. Examine the distribution of each numeric column
|
| 39 |
+
2. Analyze correlations between numeric features
|
| 40 |
+
3. Look for patterns in categorical data
|
| 41 |
+
4. Consider creating visualizations like histograms and scatter plots
|
| 42 |
+
5. Explore relationships between different variables
|
| 43 |
+
|
| 44 |
+
Note: This is a demo insight (AI model not connected in demo mode)
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
return analysis, visualizations, cleaning_recommendations, analysis_insights
|
| 48 |
+
|
| 49 |
+
def demo_ui(file):
|
| 50 |
+
"""Demo mode UI function"""
|
| 51 |
+
if file is None:
|
| 52 |
+
return "Please upload a file to begin analysis.", None, None, None
|
| 53 |
+
|
| 54 |
+
# Process the file
|
| 55 |
+
analysis, visualizations, cleaning_recommendations, analysis_insights = simple_process_file(file)
|
| 56 |
+
|
| 57 |
+
# Format analysis for display
|
| 58 |
+
analysis_html = display_analysis(analysis)
|
| 59 |
+
|
| 60 |
+
# Prepare visualizations for display
|
| 61 |
+
viz_html = ""
|
| 62 |
+
if visualizations and not isinstance(visualizations, str):
|
| 63 |
+
for viz_name, fig in visualizations.items():
|
| 64 |
+
# Convert plotly figure to HTML
|
| 65 |
+
viz_html += f'<div style="margin-bottom: 30px;">{fig.to_html(full_html=False, include_plotlyjs="cdn")}</div>'
|
| 66 |
+
|
| 67 |
+
# Combine analysis and visualizations
|
| 68 |
+
result_html = f"""
|
| 69 |
+
<div style="display: flex; flex-direction: column;">
|
| 70 |
+
<div>{analysis_html}</div>
|
| 71 |
+
<h2>Data Visualizations</h2>
|
| 72 |
+
<div>{viz_html}</div>
|
| 73 |
+
</div>
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
return result_html, visualizations, cleaning_recommendations, analysis_insights
|
| 77 |
+
|
| 78 |
+
# Create Gradio interface for demo mode
|
| 79 |
+
with gr.Blocks(title="Data Visualization & Cleaning AI (Demo Mode)") as demo:
|
| 80 |
+
gr.Markdown("# Data Visualization & Cleaning AI")
|
| 81 |
+
gr.Markdown("**DEMO MODE** - Upload your data file (CSV, Excel, JSON, or TXT) and get automatic analysis and visualizations.")
|
| 82 |
+
|
| 83 |
+
with gr.Row():
|
| 84 |
+
file_input = gr.File(label="Upload Data File")
|
| 85 |
+
|
| 86 |
+
with gr.Tabs():
|
| 87 |
+
with gr.TabItem("Data Analysis"):
|
| 88 |
+
with gr.Row():
|
| 89 |
+
analyze_button = gr.Button("Analyze Data")
|
| 90 |
+
|
| 91 |
+
with gr.Tabs():
|
| 92 |
+
with gr.TabItem("Analysis & Visualizations"):
|
| 93 |
+
output = gr.HTML(label="Results")
|
| 94 |
+
with gr.TabItem("AI Cleaning Recommendations"):
|
| 95 |
+
cleaning_recommendations_output = gr.Markdown(label="AI Recommendations")
|
| 96 |
+
with gr.TabItem("AI Analysis Insights"):
|
| 97 |
+
analysis_insights_output = gr.Markdown(label="Analysis Insights")
|
| 98 |
+
with gr.TabItem("Raw Visualization Objects"):
|
| 99 |
+
viz_output = gr.JSON(label="Visualization Objects")
|
| 100 |
+
|
| 101 |
+
# Connect the button to function
|
| 102 |
+
analyze_button.click(
|
| 103 |
+
fn=demo_ui,
|
| 104 |
+
inputs=[file_input],
|
| 105 |
+
outputs=[output, viz_output, cleaning_recommendations_output, analysis_insights_output]
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# Launch the demo
|
| 109 |
+
if __name__ == "__main__":
|
| 110 |
+
demo.launch()
|