esilver commited on
Commit
bd13f5e
·
1 Parent(s): 115a2ec

compare to chicory-ui

Browse files
Files changed (1) hide show
  1. ui.py +47 -38
ui.py CHANGED
@@ -96,31 +96,11 @@ def create_demo():
96
  hr {
97
  border-color: #333 !important;
98
  }
99
- """, js="""
100
- // Add keyboard shortcut support for Cmd+Enter (Mac) or Ctrl+Enter (Windows/Linux)
101
- document.addEventListener('keydown', function(e) {
102
- if ((e.metaKey || e.ctrlKey) && e.key === 'Enter') {
103
- // Find the active tab
104
- const activeTabButton = document.querySelector('.tab-nav button[aria-selected="true"]');
105
- if (!activeTabButton) return;
106
-
107
- // Find the active tab content
108
- const activeTabContent = document.querySelector('.tab-panel[style*="display: block"]');
109
- if (!activeTabContent) return;
110
-
111
- // Find the submit button in the active tab
112
- const submitButton = activeTabContent.querySelector('button');
113
- if (submitButton) {
114
- submitButton.click();
115
- e.preventDefault();
116
- }
117
- }
118
- });
119
  """) as demo:
120
  gr.Markdown("# Product Categorization Tool\nAnalyze products and find the most similar ingredients using AI embeddings.")
121
  gr.Markdown("Use **Cmd+Enter** (Mac) or **Ctrl+Enter** (Windows/Linux) to quickly categorize products.")
122
 
123
- with gr.Tabs():
124
  with gr.TabItem("Text Input"):
125
  with gr.Row():
126
  with gr.Column(scale=1):
@@ -134,21 +114,12 @@ def create_demo():
134
  with input_controls:
135
  top_n = gr.Slider(1, 10, 5, label="Top N Results")
136
  confidence = gr.Slider(0.1, 0.9, 0.5, label="Confidence Threshold")
137
- categorize_btn = gr.Button("Categorize")
138
 
139
  with gr.Column(scale=1):
140
  # Results section
141
  text_output = gr.HTML(label="Categorization Results", elem_classes="results-container")
142
 
143
- # Connect button click to the categorize function
144
- categorize_btn.click(
145
- fn=categorize_products,
146
- inputs=[text_input, gr.State(False), top_n, confidence],
147
- outputs=text_output
148
- )
149
-
150
- # Custom JS handling of Cmd+Enter/Ctrl+Enter is configured in js parameter above
151
-
152
  with gr.TabItem("File Upload"):
153
  with gr.Row():
154
  with gr.Column(scale=1):
@@ -158,17 +129,55 @@ def create_demo():
158
  with file_controls:
159
  file_top_n = gr.Slider(1, 10, 5, label="Top N Results")
160
  file_confidence = gr.Slider(0.1, 0.9, 0.5, label="Confidence Threshold")
161
- process_btn = gr.Button("Process File")
162
 
163
  with gr.Column(scale=1):
164
  # Results section
165
  file_output = gr.HTML(label="Categorization Results", elem_classes="results-container")
166
-
167
- process_btn.click(
168
- fn=categorize_products,
169
- inputs=[file_input, gr.State(True), file_top_n, file_confidence],
170
- outputs=file_output
171
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172
 
173
  gr.Markdown("Powered by Voyage AI embeddings • Built with Gradio")
 
174
  return demo
 
96
  hr {
97
  border-color: #333 !important;
98
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  """) as demo:
100
  gr.Markdown("# Product Categorization Tool\nAnalyze products and find the most similar ingredients using AI embeddings.")
101
  gr.Markdown("Use **Cmd+Enter** (Mac) or **Ctrl+Enter** (Windows/Linux) to quickly categorize products.")
102
 
103
+ with gr.Tabs() as tabs:
104
  with gr.TabItem("Text Input"):
105
  with gr.Row():
106
  with gr.Column(scale=1):
 
114
  with input_controls:
115
  top_n = gr.Slider(1, 10, 5, label="Top N Results")
116
  confidence = gr.Slider(0.1, 0.9, 0.5, label="Confidence Threshold")
117
+ categorize_btn = gr.Button("Categorize", variant="primary")
118
 
119
  with gr.Column(scale=1):
120
  # Results section
121
  text_output = gr.HTML(label="Categorization Results", elem_classes="results-container")
122
 
 
 
 
 
 
 
 
 
 
123
  with gr.TabItem("File Upload"):
124
  with gr.Row():
125
  with gr.Column(scale=1):
 
129
  with file_controls:
130
  file_top_n = gr.Slider(1, 10, 5, label="Top N Results")
131
  file_confidence = gr.Slider(0.1, 0.9, 0.5, label="Confidence Threshold")
132
+ process_btn = gr.Button("Process File", variant="primary")
133
 
134
  with gr.Column(scale=1):
135
  # Results section
136
  file_output = gr.HTML(label="Categorization Results", elem_classes="results-container")
137
+
138
+ # Connect button click to the categorize function - more explicit binding
139
+ categorize_btn.click(
140
+ fn=categorize_products,
141
+ inputs=[text_input, gr.State(False), top_n, confidence],
142
+ outputs=text_output
143
+ )
144
+
145
+ process_btn.click(
146
+ fn=categorize_products,
147
+ inputs=[file_input, gr.State(True), file_top_n, file_confidence],
148
+ outputs=file_output
149
+ )
150
+
151
+ # Add keyboard shortcut handler for both tabs
152
+ text_input.change(None, None, None, js="""
153
+ function() {
154
+ // Set up the keyboard event listener
155
+ document.removeEventListener('keydown', cmdEnterHandler);
156
+ document.addEventListener('keydown', cmdEnterHandler);
157
+
158
+ function cmdEnterHandler(e) {
159
+ if ((e.metaKey || e.ctrlKey) && e.key === 'Enter') {
160
+ // Find the currently visible tab content
161
+ const textTab = document.querySelector('.tabitem:nth-child(1)');
162
+ const fileTab = document.querySelector('.tabitem:nth-child(2)');
163
+
164
+ if (textTab && window.getComputedStyle(textTab).display !== 'none') {
165
+ // Text tab is active
166
+ const button = textTab.querySelector('button');
167
+ if (button) button.click();
168
+ } else if (fileTab && window.getComputedStyle(fileTab).display !== 'none') {
169
+ // File tab is active
170
+ const button = fileTab.querySelector('button');
171
+ if (button) button.click();
172
+ }
173
+ e.preventDefault();
174
+ }
175
+ }
176
+
177
+ return [];
178
+ }
179
+ """)
180
 
181
  gr.Markdown("Powered by Voyage AI embeddings • Built with Gradio")
182
+
183
  return demo