jzou19950715 commited on
Commit
3a74807
·
verified ·
1 Parent(s): 4667cbb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +175 -0
app.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import Optional
3
+
4
+ import gradio as gr
5
+ import pandas as pd
6
+ from litellm import completion
7
+
8
+ 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,
15
+ api_key: str,
16
+ temperature: float = 0.7,
17
+ ) -> str:
18
+ """Process user request and generate analysis"""
19
+
20
+ if not api_key:
21
+ return "Error: Please provide an API key."
22
+
23
+ if not file:
24
+ return "Error: Please upload a file."
25
+
26
+ try:
27
+ # Set up environment
28
+ os.environ["OPENAI_API_KEY"] = api_key
29
+
30
+ # Load data
31
+ if file.name.endswith('.csv'):
32
+ df = pd.read_csv(file.name)
33
+ elif file.name.endswith(('.xlsx', '.xls')):
34
+ df = pd.read_excel(file.name)
35
+ else:
36
+ return "Error: Unsupported file type."
37
+
38
+ # Initialize analyzers
39
+ analyzer = DataAnalyzer()
40
+
41
+ # Build context
42
+ file_info = f"""
43
+ File: {file.name}
44
+ Shape: {df.shape}
45
+ Columns: {', '.join(df.columns)}
46
+
47
+ Column Types:
48
+ {chr(10).join([f'- {col}: {dtype}' for col, dtype in df.dtypes.items()])}
49
+ """
50
+
51
+ # Get analysis request from GPT-4
52
+ messages = [
53
+ {
54
+ "role": "system",
55
+ "content": """You are a data analysis assistant.
56
+ Interpret the user's query and determine which analysis to perform.
57
+ Return a JSON with:
58
+ - analysis_type: distribution, forecast, or correlation
59
+ - params: dictionary of parameters needed
60
+ - explanation: why this analysis is appropriate"""
61
+ },
62
+ {
63
+ "role": "user",
64
+ "content": f"{file_info}\n\nUser request: {query}"
65
+ }
66
+ ]
67
+
68
+ response = completion(
69
+ model="gpt-4o-mini",
70
+ messages=messages,
71
+ temperature=temperature
72
+ )
73
+
74
+ # Parse response and perform analysis
75
+ analysis_request = eval(response.choices[0].message.content)
76
+ result = analyzer.analyze_data(
77
+ df,
78
+ analysis_request["analysis_type"],
79
+ analysis_request["params"]
80
+ )
81
+
82
+ # Combine results into HTML
83
+ html_output = f"""
84
+ <div class="analysis-container">
85
+ <div class="explanation">
86
+ <h2>Analysis Explanation</h2>
87
+ <p>{analysis_request['explanation']}</p>
88
+ </div>
89
+
90
+ <div class="results">
91
+ <h2>Statistical Results</h2>
92
+ <pre>{str(result.get('statistics', ''))}</pre>
93
+ </div>
94
+
95
+ <div class="visualization">
96
+ <h2>Interactive Visualization</h2>
97
+ {result['visualization']}
98
+ </div>
99
+ </div>
100
+ """
101
+
102
+ return html_output
103
+
104
+ except Exception as e:
105
+ return f"Error occurred: {str(e)}"
106
+
107
+ def create_interface():
108
+ """Create Gradio interface"""
109
+
110
+ with gr.Blocks(title="Interactive Data Analysis") as interface:
111
+ gr.Markdown("""
112
+ # Interactive Data Analysis Assistant
113
+
114
+ Upload your data and get interactive visualizations with statistical analysis.
115
+
116
+ **Features:**
117
+ - Interactive D3 visualizations
118
+ - Statistical analysis
119
+ - Probability distributions
120
+ - Time series forecasting
121
+ - Correlation analysis
122
+
123
+ **Note**: Requires your own OpenAI API key.
124
+ """)
125
+
126
+ with gr.Row():
127
+ with gr.Column():
128
+ file = gr.File(
129
+ label="Upload Data File",
130
+ file_types=[".csv", ".xlsx", ".xls"]
131
+ )
132
+ query = gr.Textbox(
133
+ label="What would you like to analyze?",
134
+ placeholder="e.g., Show distribution of values with statistics",
135
+ lines=3
136
+ )
137
+ api_key = gr.Textbox(
138
+ label="API Key (Required)",
139
+ placeholder="Your OpenAI API key",
140
+ type="password"
141
+ )
142
+ temperature = gr.Slider(
143
+ label="Temperature",
144
+ minimum=0.0,
145
+ maximum=1.0,
146
+ value=0.7,
147
+ step=0.1
148
+ )
149
+ analyze_btn = gr.Button("Analyze")
150
+
151
+ with gr.Column():
152
+ output = gr.HTML(label="Output")
153
+
154
+ analyze_btn.click(
155
+ analyze_data,
156
+ inputs=[file, query, api_key, temperature],
157
+ outputs=output
158
+ )
159
+
160
+ gr.Examples(
161
+ examples=[
162
+ [None, "Show me the distribution of values and calculate statistics"],
163
+ [None, "Create a 10-period probability cone forecast"],
164
+ [None, "Analyze correlations between variables"],
165
+ [None, "Test if the data follows a normal distribution"],
166
+ [None, "Show the data distribution with confidence intervals"],
167
+ ],
168
+ inputs=[file, query]
169
+ )
170
+
171
+ return interface
172
+
173
+ if __name__ == "__main__":
174
+ interface = create_interface()
175
+ interface.launch()