nolanzandi commited on
Commit
0193c5f
·
verified ·
1 Parent(s): 407895f

Update templates/data_file.py

Browse files
Files changed (1) hide show
  1. templates/data_file.py +135 -135
templates/data_file.py CHANGED
@@ -1,136 +1,136 @@
1
- import gradio as gr
2
- from functions import example_question_generator, chatbot_func
3
- from data_sources import process_data_upload
4
- from utils import message_dict
5
- import ast
6
-
7
- def run_example(input):
8
- return input
9
-
10
- def example_display(input):
11
- if input == None:
12
- display = True
13
- else:
14
- display = False
15
- return [gr.update(visible=display),gr.update(visible=display),gr.update(visible=display),gr.update(visible=display)]
16
-
17
- with gr.Blocks() as demo:
18
- description = gr.HTML("""
19
- <!-- Header -->
20
- <div class="max-w-4xl mx-auto mb-12 text-center">
21
- <div class="bg-blue-50 border border-blue-200 rounded-lg max-w-2xl mx-auto">
22
- <h2 class="font-semibold text-blue-800 ">
23
- <i class="fas fa-info-circle mr-2"></i>Supported Files
24
- </h2>
25
- <div class="flex flex-wrap justify-center gap-3 pb-4 text-blue-700">
26
- <span class="tooltip">
27
- <i class="fas fa-file-csv mr-1"></i>CSV
28
- <span class="tooltip-text">Comma-separated values</span>
29
- </span>
30
- <span class="tooltip">
31
- <i class="fas fa-file-alt mr-1"></i>TSV
32
- <span class="tooltip-text">Tab-separated values</span>
33
- </span>
34
- <span class="tooltip">
35
- <i class="fas fa-file-alt mr-1"></i>TXT
36
- <span class="tooltip-text">Text files</span>
37
- </span>
38
- <span class="tooltip">
39
- <i class="fas fa-file-excel mr-1"></i>XLS/XLSX
40
- <span class="tooltip-text">Excel spreadsheets</span>
41
- </span>
42
- <span class="tooltip">
43
- <i class="fas fa-file-code mr-1"></i>XML
44
- <span class="tooltip-text">XML documents</span>
45
- </span>
46
- <span class="tooltip">
47
- <i class="fas fa-file-code mr-1"></i>JSON
48
- <span class="tooltip-text">JSON data files</span>
49
- </span>
50
- </div>
51
- </div>
52
- </div>
53
- """, elem_classes="description_component")
54
- example_file_1 = gr.File(visible=False, value="samples/bank_marketing_campaign.csv")
55
- example_file_2 = gr.File(visible=False, value="samples/online_retail_data.csv")
56
- example_file_3 = gr.File(visible=False, value="samples/tb_illness_data.csv")
57
- with gr.Row():
58
- example_btn_1 = gr.Button(value="Try Me: bank_marketing_campaign.csv", elem_classes="sample-btn bg-gradient-to-r from-purple-500 to-indigo-600 text-white p-6 rounded-lg text-left hover:shadow-lg", size="md", variant="primary")
59
- example_btn_2 = gr.Button(value="Try Me: online_retail_data.csv", elem_classes="sample-btn bg-gradient-to-r from-purple-500 to-indigo-600 text-white p-6 rounded-lg text-left hover:shadow-lg", size="md", variant="primary")
60
- example_btn_3 = gr.Button(value="Try Me: tb_illness_data.csv", elem_classes="sample-btn bg-gradient-to-r from-purple-500 to-indigo-600 text-white p-6 rounded-lg text-left hover:shadow-lg", size="md", variant="primary")
61
-
62
- file_output = gr.File(label="Data File (CSV, TSV, TXT, XLS, XLSX, XML, JSON)", show_label=True, elem_classes="file_marker drop-zone border-2 border-dashed border-gray-300 rounded-lg hover:border-primary cursor-pointer bg-gray-50 hover:bg-blue-50 transition-colors duration-300", file_types=['.csv','.xlsx','.txt','.json','.ndjson','.xml','.xls','.tsv'])
63
- example_btn_1.click(fn=run_example, inputs=example_file_1, outputs=file_output)
64
- example_btn_2.click(fn=run_example, inputs=example_file_2, outputs=file_output)
65
- example_btn_3.click(fn=run_example, inputs=example_file_3, outputs=file_output)
66
- file_output.change(fn=example_display, inputs=file_output, outputs=[example_btn_1, example_btn_2, example_btn_3, description])
67
-
68
- @gr.render(inputs=file_output)
69
- def data_options(filename, request: gr.Request):
70
- print(filename)
71
- if request.session_hash not in message_dict:
72
- message_dict[request.session_hash] = {}
73
- message_dict[request.session_hash]['file_upload'] = None
74
- if filename:
75
- process_message = process_upload(filename, request.session_hash)
76
- gr.HTML(value=process_message[1], padding=False)
77
- if process_message[0] == "success":
78
- if "bank_marketing_campaign" in filename:
79
- example_questions = [
80
- ["Describe the dataset"],
81
- ["What levels of education have the highest and lowest average balance?"],
82
- ["What job is most and least common for a yes response from the individuals, not counting 'unknown'?"],
83
- ["Can you generate a bar chart of education vs. average balance?"],
84
- ["Can you generate a table of levels of education versus average balance, percent married, percent with a loan, and percent in default?"],
85
- ["Can we predict the relationship between the number of contacts performed before this campaign and the average balance?"],
86
- ["Can you plot the number of contacts performed before this campaign versus the duration and use balance as the size in a bubble chart?"]
87
- ]
88
- elif "online_retail_data" in filename:
89
- example_questions = [
90
- ["Describe the dataset"],
91
- ["What month had the highest revenue?"],
92
- ["Is revenue higher in the morning or afternoon?"],
93
- ["Can you generate a line graph of revenue per month?"],
94
- ["Can you generate a table of revenue per month?"],
95
- ["Can we predict how time of day affects transaction value in this data set?"],
96
- ["Can you plot revenue per month with size being the number of units sold that month in a bubble chart?"]
97
- ]
98
- else:
99
- try:
100
- generated_examples = ast.literal_eval(example_question_generator(request.session_hash, 'file_upload', '', process_message[1], ''))
101
- example_questions = [
102
- ["Describe the dataset"]
103
- ]
104
- for example in generated_examples:
105
- example_questions.append([example])
106
- except Exception as e:
107
- print("DATA FILE QUESTION GENERATION ERROR")
108
- print(e)
109
- example_questions = [
110
- ["Describe the dataset"],
111
- ["List the columns in the dataset"],
112
- ["What could this data be used for?"],
113
- ]
114
- session_hash = gr.Textbox(visible=False, value=request.session_hash)
115
- data_source = gr.Textbox(visible=False, value='file_upload')
116
- schema = gr.Textbox(visible=False, value='')
117
- titles = gr.Textbox(value=process_message[1], interactive=False, visible=False)
118
- bot = gr.Chatbot(type='messages', label="CSV Chat Window", render_markdown=True, sanitize_html=False, show_label=True, render=False, visible=True, elem_classes="chatbot")
119
- chat = gr.ChatInterface(
120
- fn=chatbot_func,
121
- type='messages',
122
- chatbot=bot,
123
- title="Chat with your data file",
124
- concurrency_limit=None,
125
- examples=example_questions,
126
- additional_inputs=[session_hash, data_source, titles, schema]
127
- )
128
-
129
- def process_upload(upload_value, session_hash):
130
- if upload_value:
131
- process_message = process_data_upload(upload_value, session_hash)
132
- return process_message
133
-
134
-
135
- if __name__ == "__main__":
136
  demo.launch()
 
1
+ import gradio as gr
2
+ from functions import example_question_generator, chatbot_func
3
+ from data_sources import process_data_upload
4
+ from utils import message_dict
5
+ import ast
6
+
7
+ def run_example(input):
8
+ return input
9
+
10
+ def example_display(input):
11
+ if input == None:
12
+ display = True
13
+ else:
14
+ display = False
15
+ return [gr.update(visible=display),gr.update(visible=display),gr.update(visible=display),gr.update(visible=display)]
16
+
17
+ with gr.Blocks() as demo:
18
+ description = gr.HTML("""
19
+ <!-- Header -->
20
+ <div class="max-w-4xl mx-auto mb-12 text-center">
21
+ <div class="bg-blue-50 border border-blue-200 rounded-lg max-w-2xl mx-auto">
22
+ <h2 class="font-semibold text-blue-800 ">
23
+ <i class="fas fa-info-circle mr-2"></i>Supported Files
24
+ </h2>
25
+ <div class="flex flex-wrap justify-center gap-3 pb-4 text-blue-700">
26
+ <span class="tooltip">
27
+ <i class="fas fa-file-csv mr-1"></i>CSV
28
+ <span class="tooltip-text">Comma-separated values</span>
29
+ </span>
30
+ <span class="tooltip">
31
+ <i class="fas fa-file-alt mr-1"></i>TSV
32
+ <span class="tooltip-text">Tab-separated values</span>
33
+ </span>
34
+ <span class="tooltip">
35
+ <i class="fas fa-file-alt mr-1"></i>TXT
36
+ <span class="tooltip-text">Text files</span>
37
+ </span>
38
+ <span class="tooltip">
39
+ <i class="fas fa-file-excel mr-1"></i>XLS/XLSX
40
+ <span class="tooltip-text">Excel spreadsheets</span>
41
+ </span>
42
+ <span class="tooltip">
43
+ <i class="fas fa-file-code mr-1"></i>XML
44
+ <span class="tooltip-text">XML documents</span>
45
+ </span>
46
+ <span class="tooltip">
47
+ <i class="fas fa-file-code mr-1"></i>JSON
48
+ <span class="tooltip-text">JSON data files</span>
49
+ </span>
50
+ </div>
51
+ </div>
52
+ </div>
53
+ """, elem_classes="description_component")
54
+ example_file_1 = gr.File(visible=False, value="samples/bank_marketing_campaign.csv")
55
+ example_file_2 = gr.File(visible=False, value="samples/online_retail_data.csv")
56
+ example_file_3 = gr.File(visible=False, value="samples/tb_illness_data.csv")
57
+ with gr.Row():
58
+ example_btn_1 = gr.Button(value="Try Me: bank_marketing_campaign.csv", elem_classes="sample-btn bg-gradient-to-r from-purple-500 to-indigo-600 text-white p-6 rounded-lg text-left hover:shadow-lg", size="md", variant="primary")
59
+ example_btn_2 = gr.Button(value="Try Me: online_retail_data.csv", elem_classes="sample-btn bg-gradient-to-r from-purple-500 to-indigo-600 text-white p-6 rounded-lg text-left hover:shadow-lg", size="md", variant="primary")
60
+ example_btn_3 = gr.Button(value="Try Me: tb_illness_data.csv", elem_classes="sample-btn bg-gradient-to-r from-purple-500 to-indigo-600 text-white p-6 rounded-lg text-left hover:shadow-lg", size="md", variant="primary")
61
+
62
+ file_output = gr.File(label="Data File (CSV, TSV, TXT, XLS, XLSX, XML, JSON)", show_label=True, elem_classes="file_marker drop-zone border-2 border-dashed border-gray-300 rounded-lg hover:border-primary cursor-pointer bg-gray-50 hover:bg-blue-50 transition-colors duration-300", file_types=['.csv','.xlsx','.txt','.json','.ndjson','.xml','.xls','.tsv'])
63
+ example_btn_1.click(fn=run_example, inputs=example_file_1, outputs=file_output)
64
+ example_btn_2.click(fn=run_example, inputs=example_file_2, outputs=file_output)
65
+ example_btn_3.click(fn=run_example, inputs=example_file_3, outputs=file_output)
66
+ file_output.change(fn=example_display, inputs=file_output, outputs=[example_btn_1, example_btn_2, example_btn_3, description])
67
+
68
+ @gr.render(inputs=file_output)
69
+ def data_options(filename, request: gr.Request):
70
+ print(filename)
71
+ if request.session_hash not in message_dict:
72
+ message_dict[request.session_hash] = {}
73
+ message_dict[request.session_hash]['file_upload'] = None
74
+ if filename:
75
+ process_message = process_upload(filename, request.session_hash)
76
+ gr.HTML(value=process_message[1], padding=False)
77
+ if process_message[0] == "success":
78
+ if "bank_marketing_campaign" in filename:
79
+ example_questions = [
80
+ ["Describe the dataset"],
81
+ ["What levels of education have the highest and lowest average balance?"],
82
+ ["What job is most and least common for a yes response from the individuals, not counting 'unknown'?"],
83
+ ["Can you generate a bar chart of education vs. average balance?"],
84
+ ["Can you generate a table of levels of education versus average balance, percent married, percent with a loan, and percent in default?"],
85
+ ["Can we predict the relationship between the number of contacts performed before this campaign and the average balance?"],
86
+ ["Can you plot the number of contacts performed before this campaign versus the duration and use balance as the size in a bubble chart?"]
87
+ ]
88
+ elif "online_retail_data" in filename:
89
+ example_questions = [
90
+ ["Describe the dataset"],
91
+ ["What month had the highest revenue?"],
92
+ ["Is revenue higher in the morning or afternoon?"],
93
+ ["Can you generate a line graph of revenue per month?"],
94
+ ["Can you generate a table of revenue per month?"],
95
+ ["Can we predict how time of day affects transaction value in this data set?"],
96
+ ["Can you plot revenue per month with size being the number of units sold that month in a bubble chart?"]
97
+ ]
98
+ else:
99
+ try:
100
+ generated_examples = ast.literal_eval(example_question_generator(request.session_hash, 'file_upload', '', process_message[1], ''))
101
+ example_questions = [
102
+ ["Describe the dataset"]
103
+ ]
104
+ for example in generated_examples:
105
+ example_questions.append([example])
106
+ except Exception as e:
107
+ print("DATA FILE QUESTION GENERATION ERROR")
108
+ print(e)
109
+ example_questions = [
110
+ ["Describe the dataset"],
111
+ ["List the columns in the dataset"],
112
+ ["What could this data be used for?"],
113
+ ]
114
+ session_hash = gr.Textbox(visible=False, value=request.session_hash)
115
+ data_source = gr.Textbox(visible=False, value='file_upload')
116
+ schema = gr.Textbox(visible=False, value='')
117
+ titles = gr.Textbox(value=process_message[2], interactive=False, visible=False)
118
+ bot = gr.Chatbot(type='messages', label="CSV Chat Window", render_markdown=True, sanitize_html=False, show_label=True, render=False, visible=True, elem_classes="chatbot")
119
+ chat = gr.ChatInterface(
120
+ fn=chatbot_func,
121
+ type='messages',
122
+ chatbot=bot,
123
+ title="Chat with your data file",
124
+ concurrency_limit=None,
125
+ examples=example_questions,
126
+ additional_inputs=[session_hash, data_source, titles, schema]
127
+ )
128
+
129
+ def process_upload(upload_value, session_hash):
130
+ if upload_value:
131
+ process_message = process_data_upload(upload_value, session_hash)
132
+ return process_message
133
+
134
+
135
+ if __name__ == "__main__":
136
  demo.launch()