Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import base64
|
|
2 |
import io
|
3 |
import ast
|
4 |
import traceback
|
|
|
5 |
from threading import Thread
|
6 |
|
7 |
import dash
|
@@ -19,7 +20,6 @@ app.layout = dbc.Container([
|
|
19 |
html.H1("Data Analysis Dashboard", className="my-4"),
|
20 |
dbc.Card([
|
21 |
dbc.CardBody([
|
22 |
-
dbc.Input(id="api-key", placeholder="Enter your Gemini API key", type="password", className="mb-3"),
|
23 |
dcc.Upload(
|
24 |
id='upload-data',
|
25 |
children=html.Div([
|
@@ -73,8 +73,13 @@ def parse_contents(contents, filename):
|
|
73 |
print(e)
|
74 |
return None
|
75 |
|
76 |
-
def process_data(df, instructions
|
77 |
try:
|
|
|
|
|
|
|
|
|
|
|
78 |
# Initialize Gemini with provided API key
|
79 |
genai.configure(api_key=api_key)
|
80 |
model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
|
@@ -153,22 +158,18 @@ def generate_plot(df, plot_info):
|
|
153 |
[Input('submit-button', 'n_clicks')],
|
154 |
[State('upload-data', 'contents'),
|
155 |
State('upload-data', 'filename'),
|
156 |
-
State('instructions', 'value')
|
157 |
-
State('api-key', 'value')]
|
158 |
)
|
159 |
-
def update_output(n_clicks, contents, filename, instructions
|
160 |
if n_clicks is None or contents is None:
|
161 |
return dash.no_update, dash.no_update, dash.no_update, ""
|
162 |
-
|
163 |
-
if not api_key:
|
164 |
-
return dash.no_update, dash.no_update, dash.no_update, "Please enter a valid API key."
|
165 |
|
166 |
try:
|
167 |
df = parse_contents(contents, filename)
|
168 |
if df is None:
|
169 |
return dash.no_update, dash.no_update, dash.no_update, "Unable to parse the uploaded file."
|
170 |
|
171 |
-
plots = process_data(df, instructions
|
172 |
if plots is None or len(plots) < 3:
|
173 |
return dash.no_update, dash.no_update, dash.no_update, "Unable to generate visualizations. Please check your instructions and try again."
|
174 |
|
@@ -179,4 +180,6 @@ def update_output(n_clicks, contents, filename, instructions, api_key):
|
|
179 |
return dash.no_update, dash.no_update, dash.no_update, error_message
|
180 |
|
181 |
if __name__ == '__main__':
|
182 |
-
|
|
|
|
|
|
2 |
import io
|
3 |
import ast
|
4 |
import traceback
|
5 |
+
import os
|
6 |
from threading import Thread
|
7 |
|
8 |
import dash
|
|
|
20 |
html.H1("Data Analysis Dashboard", className="my-4"),
|
21 |
dbc.Card([
|
22 |
dbc.CardBody([
|
|
|
23 |
dcc.Upload(
|
24 |
id='upload-data',
|
25 |
children=html.Div([
|
|
|
73 |
print(e)
|
74 |
return None
|
75 |
|
76 |
+
def process_data(df, instructions):
|
77 |
try:
|
78 |
+
# Get API key from environment variable
|
79 |
+
api_key = os.getenv('GEMINI_API_KEY')
|
80 |
+
if not api_key:
|
81 |
+
raise ValueError("Gemini API key not found in environment variables")
|
82 |
+
|
83 |
# Initialize Gemini with provided API key
|
84 |
genai.configure(api_key=api_key)
|
85 |
model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
|
|
|
158 |
[Input('submit-button', 'n_clicks')],
|
159 |
[State('upload-data', 'contents'),
|
160 |
State('upload-data', 'filename'),
|
161 |
+
State('instructions', 'value')]
|
|
|
162 |
)
|
163 |
+
def update_output(n_clicks, contents, filename, instructions):
|
164 |
if n_clicks is None or contents is None:
|
165 |
return dash.no_update, dash.no_update, dash.no_update, ""
|
|
|
|
|
|
|
166 |
|
167 |
try:
|
168 |
df = parse_contents(contents, filename)
|
169 |
if df is None:
|
170 |
return dash.no_update, dash.no_update, dash.no_update, "Unable to parse the uploaded file."
|
171 |
|
172 |
+
plots = process_data(df, instructions)
|
173 |
if plots is None or len(plots) < 3:
|
174 |
return dash.no_update, dash.no_update, dash.no_update, "Unable to generate visualizations. Please check your instructions and try again."
|
175 |
|
|
|
180 |
return dash.no_update, dash.no_update, dash.no_update, error_message
|
181 |
|
182 |
if __name__ == '__main__':
|
183 |
+
print("Starting the Dash application...")
|
184 |
+
app.run(debug=True, host='0.0.0.0', port=7860)
|
185 |
+
print("Dash application has finished running.")
|