Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
import pandas as pd
|
|
@@ -9,6 +10,29 @@ from components.analysis import DataAnalyzer
|
|
| 9 |
from components.statistical import StatisticalAnalyzer
|
| 10 |
from components.visualization import D3Visualizer
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def analyze_data(
|
| 13 |
file: gr.File,
|
| 14 |
query: str,
|
|
@@ -53,11 +77,19 @@ def analyze_data(
|
|
| 53 |
{
|
| 54 |
"role": "system",
|
| 55 |
"content": """You are a data analysis assistant.
|
| 56 |
-
Interpret the user's query and
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
},
|
| 62 |
{
|
| 63 |
"role": "user",
|
|
@@ -72,7 +104,14 @@ def analyze_data(
|
|
| 72 |
)
|
| 73 |
|
| 74 |
# Parse response and perform analysis
|
| 75 |
-
analysis_request =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
result = analyzer.analyze_data(
|
| 77 |
df,
|
| 78 |
analysis_request["analysis_type"],
|
|
@@ -102,7 +141,9 @@ def analyze_data(
|
|
| 102 |
return html_output
|
| 103 |
|
| 104 |
except Exception as e:
|
| 105 |
-
|
|
|
|
|
|
|
| 106 |
|
| 107 |
def create_interface():
|
| 108 |
"""Create Gradio interface"""
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
+
from typing import Optional, Dict
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
import pandas as pd
|
|
|
|
| 10 |
from components.statistical import StatisticalAnalyzer
|
| 11 |
from components.visualization import D3Visualizer
|
| 12 |
|
| 13 |
+
def parse_gpt_response(response: str) -> Dict:
|
| 14 |
+
"""Safely parse GPT response into analysis request"""
|
| 15 |
+
try:
|
| 16 |
+
# Try to fix common JSON issues
|
| 17 |
+
cleaned_response = response.replace("```json\n", "").replace("```", "")
|
| 18 |
+
cleaned_response = cleaned_response.strip()
|
| 19 |
+
if not cleaned_response.startswith("{"):
|
| 20 |
+
# Extract JSON if it's embedded in text
|
| 21 |
+
start = cleaned_response.find("{")
|
| 22 |
+
end = cleaned_response.rfind("}") + 1
|
| 23 |
+
if start >= 0 and end > 0:
|
| 24 |
+
cleaned_response = cleaned_response[start:end]
|
| 25 |
+
|
| 26 |
+
# Parse JSON
|
| 27 |
+
return json.loads(cleaned_response)
|
| 28 |
+
except json.JSONDecodeError:
|
| 29 |
+
# Fallback to default analysis
|
| 30 |
+
return {
|
| 31 |
+
"analysis_type": "distribution",
|
| 32 |
+
"params": {"column": "all"},
|
| 33 |
+
"explanation": "Performing basic distribution analysis as fallback."
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
def analyze_data(
|
| 37 |
file: gr.File,
|
| 38 |
query: str,
|
|
|
|
| 77 |
{
|
| 78 |
"role": "system",
|
| 79 |
"content": """You are a data analysis assistant.
|
| 80 |
+
Interpret the user's query and provide analysis details in JSON format.
|
| 81 |
+
|
| 82 |
+
Return ONLY a JSON object with these fields:
|
| 83 |
+
{
|
| 84 |
+
"analysis_type": "distribution" or "forecast" or "correlation",
|
| 85 |
+
"params": {"column": "column_name", ...},
|
| 86 |
+
"explanation": "why this analysis is appropriate"
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
For timeseries data, prefer 'forecast' type.
|
| 90 |
+
For multiple columns, prefer 'correlation' type.
|
| 91 |
+
For single column analysis, prefer 'distribution' type.
|
| 92 |
+
"""
|
| 93 |
},
|
| 94 |
{
|
| 95 |
"role": "user",
|
|
|
|
| 104 |
)
|
| 105 |
|
| 106 |
# Parse response and perform analysis
|
| 107 |
+
analysis_request = parse_gpt_response(response.choices[0].message.content)
|
| 108 |
+
|
| 109 |
+
# Set default column if not specified
|
| 110 |
+
if "params" not in analysis_request:
|
| 111 |
+
analysis_request["params"] = {}
|
| 112 |
+
if "column" not in analysis_request["params"]:
|
| 113 |
+
analysis_request["params"]["column"] = df.select_dtypes(include=['number']).columns[0]
|
| 114 |
+
|
| 115 |
result = analyzer.analyze_data(
|
| 116 |
df,
|
| 117 |
analysis_request["analysis_type"],
|
|
|
|
| 141 |
return html_output
|
| 142 |
|
| 143 |
except Exception as e:
|
| 144 |
+
import traceback
|
| 145 |
+
error_details = traceback.format_exc()
|
| 146 |
+
return f"Error occurred: {str(e)}\n\nDetails:\n{error_details}"
|
| 147 |
|
| 148 |
def create_interface():
|
| 149 |
"""Create Gradio interface"""
|