|
7 | 7 | import re
|
8 | 8 | import sys
|
9 | 9 | import sqlite3
|
| 10 | +import time |
10 | 11 |
|
11 | 12 | from plotly.subplots import make_subplots
|
12 | 13 | from tabulate import tabulate
|
13 | 14 |
|
14 | 15 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
15 |
| -from create_db import ArxivDatabase |
16 | 16 | from config import DEFAULT_TABLES_DIR, DEFAULT_INTERFACE_MODEL_ID, canned_queries
|
| 17 | +from scripts.create_db import ArxivDatabase |
17 | 18 | from src.utils.utils import set_env_vars
|
18 | 19 |
|
19 | 20 | db = None
|
@@ -216,71 +217,85 @@ def load_database_with_graphs(db_name):
|
216 | 217 | }
|
217 | 218 | """
|
218 | 219 |
|
219 |
| -with gr.Blocks(css=css) as demo: |
220 |
| - gr.Markdown("# ArXiv Database Query Interface") |
221 | 220 |
|
222 |
| - with gr.Row(): |
223 |
| - db_dropdown = gr.Dropdown( |
224 |
| - choices=get_available_databases(), label="Select Database" |
| 221 | +def launch(): |
| 222 | + with gr.Blocks(css=css) as demo: |
| 223 | + gr.Markdown("# ArXiv Database Query Interface") |
| 224 | + |
| 225 | + with gr.Row(): |
| 226 | + db_dropdown = gr.Dropdown( |
| 227 | + choices=get_available_databases(), label="Select Database" |
| 228 | + ) |
| 229 | + load_db_btn = gr.Button("Load Database", size="sm") |
| 230 | + status = gr.Textbox(label="Status") |
| 231 | + |
| 232 | + with gr.Row(): |
| 233 | + graph_output = gr.Plot(label="Concept Co-occurrence Graph") |
| 234 | + |
| 235 | + with gr.Row(): |
| 236 | + wrap_checkbox = gr.Checkbox(label="Wrap long text", value=False) |
| 237 | + canned_query_dropdown = gr.Dropdown( |
| 238 | + choices=[q[0] for q in canned_queries], label="Select Query", scale=3 |
| 239 | + ) |
| 240 | + limit_input = gr.Number( |
| 241 | + label="Limit", value=10000, step=1, minimum=1, scale=1 |
| 242 | + ) |
| 243 | + selected_query = gr.Textbox( |
| 244 | + label="Selected Query", |
| 245 | + interactive=False, |
| 246 | + scale=2, |
| 247 | + show_label=True, |
| 248 | + show_copy_button=True, |
| 249 | + elem_id="selected-query", |
| 250 | + ) |
| 251 | + canned_query_submit = gr.Button("Submit Query", size="sm", scale=1) |
| 252 | + |
| 253 | + with gr.Row(): |
| 254 | + sql_input = gr.Textbox(label="Custom SQL Query", lines=3, scale=4) |
| 255 | + sql_submit = gr.Button("Submit Custom SQL", size="sm", scale=1) |
| 256 | + |
| 257 | + output = gr.DataFrame(label="Results", wrap=True) |
| 258 | + |
| 259 | + def update_selected_query(query_description): |
| 260 | + for desc, sql in canned_queries: |
| 261 | + if desc == query_description: |
| 262 | + return sql |
| 263 | + return "" |
| 264 | + |
| 265 | + def submit_canned_query(query_description, limit, wrap): |
| 266 | + for desc, sql in canned_queries: |
| 267 | + if desc == query_description: |
| 268 | + return query_db(sql, True, limit, wrap) |
| 269 | + return pd.DataFrame({"Error": ["Selected query not found."]}) |
| 270 | + |
| 271 | + load_db_btn.click( |
| 272 | + load_database_with_graphs, |
| 273 | + inputs=[db_dropdown], |
| 274 | + outputs=[status, graph_output], |
225 | 275 | )
|
226 |
| - load_db_btn = gr.Button("Load Database", size="sm") |
227 |
| - status = gr.Textbox(label="Status") |
228 |
| - |
229 |
| - with gr.Row(): |
230 |
| - graph_output = gr.Plot(label="Concept Co-occurrence Graph") |
231 |
| - |
232 |
| - with gr.Row(): |
233 |
| - wrap_checkbox = gr.Checkbox(label="Wrap long text", value=False) |
234 |
| - canned_query_dropdown = gr.Dropdown( |
235 |
| - choices=[q[0] for q in canned_queries], label="Select Query", scale=3 |
| 276 | + canned_query_dropdown.change( |
| 277 | + update_selected_query, |
| 278 | + inputs=[canned_query_dropdown], |
| 279 | + outputs=[selected_query], |
236 | 280 | )
|
237 |
| - limit_input = gr.Number(label="Limit", value=10000, step=1, minimum=1, scale=1) |
238 |
| - selected_query = gr.Textbox( |
239 |
| - label="Selected Query", |
240 |
| - interactive=False, |
241 |
| - scale=2, |
242 |
| - show_label=True, |
243 |
| - show_copy_button=True, |
244 |
| - elem_id="selected-query", |
| 281 | + canned_query_submit.click( |
| 282 | + submit_canned_query, |
| 283 | + inputs=[canned_query_dropdown, limit_input, wrap_checkbox], |
| 284 | + outputs=output, |
| 285 | + ) |
| 286 | + sql_submit.click( |
| 287 | + query_db, |
| 288 | + inputs=[sql_input, gr.Checkbox(value=True), limit_input, wrap_checkbox], |
| 289 | + outputs=output, |
245 | 290 | )
|
246 |
| - canned_query_submit = gr.Button("Submit Query", size="sm", scale=1) |
247 |
| - |
248 |
| - with gr.Row(): |
249 |
| - sql_input = gr.Textbox(label="Custom SQL Query", lines=3, scale=4) |
250 |
| - sql_submit = gr.Button("Submit Custom SQL", size="sm", scale=1) |
251 |
| - |
252 |
| - output = gr.DataFrame(label="Results", wrap=True) |
253 |
| - |
254 |
| - def update_selected_query(query_description): |
255 |
| - for desc, sql in canned_queries: |
256 |
| - if desc == query_description: |
257 |
| - return sql |
258 |
| - return "" |
259 |
| - |
260 |
| - def submit_canned_query(query_description, limit, wrap): |
261 |
| - for desc, sql in canned_queries: |
262 |
| - if desc == query_description: |
263 |
| - return query_db(sql, True, limit, wrap) |
264 |
| - return pd.DataFrame({"Error": ["Selected query not found."]}) |
265 | 291 |
|
266 |
| - load_db_btn.click( |
267 |
| - load_database_with_graphs, inputs=[db_dropdown], outputs=[status, graph_output] |
268 |
| - ) |
269 |
| - canned_query_dropdown.change( |
270 |
| - update_selected_query, inputs=[canned_query_dropdown], outputs=[selected_query] |
271 |
| - ) |
272 |
| - canned_query_submit.click( |
273 |
| - submit_canned_query, |
274 |
| - inputs=[canned_query_dropdown, limit_input, wrap_checkbox], |
275 |
| - outputs=output, |
276 |
| - ) |
277 |
| - sql_submit.click( |
278 |
| - query_db, |
279 |
| - inputs=[sql_input, gr.Checkbox(value=True), limit_input, wrap_checkbox], |
280 |
| - outputs=output, |
| 292 | + print("Launching Gradio app...", flush=True) |
| 293 | + demo.launch(share=True) |
| 294 | + print( |
| 295 | + "Gradio app launched. If you don't see a URL above, there might be network restrictions.", |
| 296 | + flush=True, |
281 | 297 | )
|
282 | 298 |
|
283 |
| -if __name__ == "__main__": |
284 |
| - demo.launch(share=True) |
285 | 299 |
|
286 |
| -demo.launch() |
| 300 | +if __name__ == "__main__": |
| 301 | + launch() |
0 commit comments