davidberenstein1957 HF staff commited on
Commit
d872a02
1 Parent(s): ce0d80f

fix: avoid while loop

Browse files
Files changed (1) hide show
  1. app.py +15 -19
app.py CHANGED
@@ -1,5 +1,3 @@
1
- import time
2
-
3
  import duckdb
4
  import gradio as gr
5
  import polars as pl
@@ -63,23 +61,21 @@ def vectorize_dataset(split: str, column: str):
63
 
64
 
65
  def run_query(query: str, column: str):
66
- global df
67
- while f"{column}_embeddings" not in df.columns:
68
- sleeper = 5
69
- gr.Info(
70
- f"Waiting for vectorization to complete... ({sleeper}s)", duration=sleeper
71
- )
72
- time.sleep(sleeper)
73
- vector = model.encode(query)
74
- df_results = duckdb.sql(
75
- query=f"""
76
- SELECT *
77
- FROM df
78
- ORDER BY array_cosine_distance({column}_embeddings, {vector.tolist()}::FLOAT[256])
79
- LIMIT 5
80
- """
81
- ).to_df()
82
- return gr.Dataframe(df_results, visible=True)
83
 
84
 
85
  def hide_components():
 
 
 
1
  import duckdb
2
  import gradio as gr
3
  import polars as pl
 
61
 
62
 
63
  def run_query(query: str, column: str):
64
+ try:
65
+ global df
66
+
67
+ vector = model.encode(query)
68
+ df_results = duckdb.sql(
69
+ query=f"""
70
+ SELECT *
71
+ FROM df
72
+ ORDER BY array_cosine_distance({column}_embeddings, {vector.tolist()}::FLOAT[256])
73
+ LIMIT 5
74
+ """
75
+ ).to_df()
76
+ return gr.Dataframe(df_results, visible=True)
77
+ except Exception as e:
78
+ raise gr.Error(f"Error running query: {e}")
 
 
79
 
80
 
81
  def hide_components():