youplala commited on
Commit
caa1de5
·
1 Parent(s): 96f1db8

Remove Paywall w/ by upgrading chartgpt

Browse files
Files changed (2) hide show
  1. app.py +14 -75
  2. requirements.txt +1 -1
app.py CHANGED
@@ -31,61 +31,6 @@ body = dmc.Stack(
31
  size="md",
32
  breakpoint="sm",
33
  children=[
34
- dmc.StepperStep(
35
- label="Add your OpenAI API key",
36
- icon=DashIconify(
37
- icon="material-symbols:lock",
38
- ),
39
- progressIcon=DashIconify(
40
- icon="material-symbols:lock",
41
- ),
42
- completedIcon=DashIconify(
43
- icon="material-symbols:lock-open",
44
- ),
45
- children=[
46
- dmc.Stack(
47
- [
48
- dmc.Stack(
49
- [
50
- dmc.Blockquote(
51
- """Welcome to ChartGPT! To get started, fetch your OpenAI API key and paste it below.\
52
- Then, upload your CSV file and ask ChartGPT to plot your data. Happy charting 🥳""",
53
- icon=DashIconify(
54
- icon="line-md:coffee-half-empty-twotone-loop"
55
- ),
56
- ),
57
- dmc.Center(
58
- dmc.Button(
59
- dmc.Anchor(
60
- "Get your API key",
61
- href="https://platform.openai.com/account/api-keys",
62
- target="_blank",
63
- style={
64
- "textDecoration": "none",
65
- "color": "white",
66
- },
67
- ),
68
- fullWidth=False,
69
- rightIcon=DashIconify(
70
- icon="material-symbols:open-in-new"
71
- ),
72
- ),
73
- ),
74
- ],
75
- ),
76
- dmc.PasswordInput(
77
- id="input-api-key",
78
- label="API Key",
79
- description="Please add your OpenAI API key. It will be used to generate your visualization",
80
- placeholder="Your OpenAI API Key",
81
- icon=DashIconify(icon="material-symbols:key"),
82
- size="sm",
83
- required=True,
84
- ),
85
- ]
86
- )
87
- ],
88
- ),
89
  dmc.StepperStep(
90
  label="Upload your CSV file",
91
  icon=DashIconify(icon="material-symbols:upload"),
@@ -352,7 +297,7 @@ def store_data(contents, filename):
352
  )
353
  def load_data(dataset):
354
  if dataset is not None:
355
- df = pd.read_json(dataset, orient="split")
356
  table_preview = dag.AgGrid(
357
  id="data-preview",
358
  rowData=df.to_dict("records"),
@@ -401,7 +346,7 @@ def load_data(dataset):
401
  def update_stepper(stepper_next, stepper_back, current):
402
  ctx = dash.callback_context
403
  id_clicked = ctx.triggered[0]["prop_id"]
404
- if id_clicked == "stepper-next.n_clicks" and current < 3:
405
  return current + 1
406
  elif id_clicked == "stepper-back.n_clicks":
407
  return current - 1
@@ -416,15 +361,10 @@ def update_stepper(stepper_next, stepper_back, current):
416
  Output("stepper-next", "children"),
417
  Output("icon-next", "icon"),
418
  Input("stepper", "active"),
419
- Input("input-api-key", "value"),
420
  Input("dataset-store", "data"),
421
  )
422
- def update_stepper_buttons(current, api_key, data):
423
- if current == 0 and api_key != "":
424
- return False, True, "block", "none", "Next", "ic:round-arrow-forward"
425
- elif current == 0 and api_key == "":
426
- return True, True, "block", "none", "Next", "ic:round-arrow-forward"
427
- elif current == 1 and data is not None:
428
  return (
429
  False,
430
  False,
@@ -433,7 +373,7 @@ def update_stepper_buttons(current, api_key, data):
433
  "Next",
434
  "ic:round-arrow-forward",
435
  )
436
- elif current == 1 and data is None:
437
  return (
438
  True,
439
  False,
@@ -442,7 +382,7 @@ def update_stepper_buttons(current, api_key, data):
442
  "Next",
443
  "ic:round-arrow-forward",
444
  )
445
- elif current == 2:
446
  return (
447
  False,
448
  False,
@@ -451,7 +391,7 @@ def update_stepper_buttons(current, api_key, data):
451
  "Ask ChartGPT",
452
  "ph:flask-bold",
453
  )
454
- elif current == 3:
455
  return (False, False, "block", "block", "Ask again", "ic:refresh")
456
 
457
 
@@ -462,29 +402,28 @@ def update_stepper_buttons(current, api_key, data):
462
  Output("alert-error", "children"),
463
  Input("stepper-next", "n_clicks"),
464
  State("stepper", "active"),
465
- State("input-api-key", "value"),
466
  State("dataset-store", "data"),
467
  State("input-text", "value"),
468
  State("input-text-retry", "value"),
469
  prevent_initial_call=True,
470
  )
471
- def update_graph(n_clicks, active, api_key, df, prompt, prompt_retry):
472
- if n_clicks is not None and active == 2:
473
  try:
474
- return prompt, predict(api_key, df, prompt), True, ""
475
  except Exception as e:
476
  return no_update, no_update, False, str(e)
477
- elif n_clicks is not None and active == 3:
478
  try:
479
- return prompt_retry, predict(api_key, df, prompt_retry), True, ""
480
  except Exception as e:
481
  return no_update, no_update, False, str(e)
482
  return no_update
483
 
484
 
485
- def predict(api_key, df, prompt):
486
  df = pd.read_json(df, orient="split")
487
- chart = cg.Chart(df, api_key=api_key)
488
  fig = chart.plot(prompt, return_fig=True)
489
  output = show_graph_card(graph=dcc.Graph(figure=fig), code=chart.last_run_code)
490
  return output
 
31
  size="md",
32
  breakpoint="sm",
33
  children=[
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  dmc.StepperStep(
35
  label="Upload your CSV file",
36
  icon=DashIconify(icon="material-symbols:upload"),
 
297
  )
298
  def load_data(dataset):
299
  if dataset is not None:
300
+ df = pd.read_json(io.StringIO(dataset), orient="split")
301
  table_preview = dag.AgGrid(
302
  id="data-preview",
303
  rowData=df.to_dict("records"),
 
346
  def update_stepper(stepper_next, stepper_back, current):
347
  ctx = dash.callback_context
348
  id_clicked = ctx.triggered[0]["prop_id"]
349
+ if id_clicked == "stepper-next.n_clicks" and current < 2:
350
  return current + 1
351
  elif id_clicked == "stepper-back.n_clicks":
352
  return current - 1
 
361
  Output("stepper-next", "children"),
362
  Output("icon-next", "icon"),
363
  Input("stepper", "active"),
 
364
  Input("dataset-store", "data"),
365
  )
366
+ def update_stepper_buttons(current, data):
367
+ if current == 0 and data is not None:
 
 
 
 
368
  return (
369
  False,
370
  False,
 
373
  "Next",
374
  "ic:round-arrow-forward",
375
  )
376
+ elif current == 0 and data is None:
377
  return (
378
  True,
379
  False,
 
382
  "Next",
383
  "ic:round-arrow-forward",
384
  )
385
+ elif current == 1:
386
  return (
387
  False,
388
  False,
 
391
  "Ask ChartGPT",
392
  "ph:flask-bold",
393
  )
394
+ elif current == 2:
395
  return (False, False, "block", "block", "Ask again", "ic:refresh")
396
 
397
 
 
402
  Output("alert-error", "children"),
403
  Input("stepper-next", "n_clicks"),
404
  State("stepper", "active"),
 
405
  State("dataset-store", "data"),
406
  State("input-text", "value"),
407
  State("input-text-retry", "value"),
408
  prevent_initial_call=True,
409
  )
410
+ def update_graph(n_clicks, active, df, prompt, prompt_retry):
411
+ if n_clicks is not None and active == 1:
412
  try:
413
+ return prompt, predict(df, prompt), True, ""
414
  except Exception as e:
415
  return no_update, no_update, False, str(e)
416
+ elif n_clicks is not None and active == 2:
417
  try:
418
+ return prompt_retry, predict(df, prompt_retry), True, ""
419
  except Exception as e:
420
  return no_update, no_update, False, str(e)
421
  return no_update
422
 
423
 
424
+ def predict(df, prompt):
425
  df = pd.read_json(df, orient="split")
426
+ chart = cg.Chart(df)
427
  fig = chart.plot(prompt, return_fig=True)
428
  output = show_graph_card(graph=dcc.Graph(figure=fig), code=chart.last_run_code)
429
  return output
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- chartgpt>=0.0.6
2
  dash-ag-grid>=2.2.0
3
  dash-iconify>=0.1.2
4
  dash-mantine-components>=0.12.1
 
1
+ chartgpt>=0.0.7
2
  dash-ag-grid>=2.2.0
3
  dash-iconify>=0.1.2
4
  dash-mantine-components>=0.12.1