Skip to content

Commit f501156

Browse files
authored
fix: gradio interface crash on types that are not json serializable (#47)
1 parent 7a3c2e8 commit f501156

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/dbally/gradio/gradio_interface.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import json
12
from io import StringIO
2-
from typing import Tuple
3+
from typing import Any, Dict, List, Tuple
34

45
import gradio
56
import pandas as pd
@@ -85,7 +86,9 @@ def _load_preview_data(self, selected_view_name: str) -> pd.DataFrame:
8586
selected_view = self.collection.get(selected_view_name)
8687
if issubclass(type(selected_view), BaseStructuredView):
8788
selected_view_results = selected_view.execute()
88-
preview_dataframe = pd.DataFrame.from_records(selected_view_results.results).head(self.preview_limit)
89+
preview_dataframe = self._load_results_into_dataframe(selected_view_results.results).head(
90+
self.preview_limit
91+
)
8992
else:
9093
preview_dataframe = pd.DataFrame()
9194

@@ -112,7 +115,7 @@ async def _ui_ask_query(
112115
question=question_query, return_natural_response=natural_language_flag
113116
)
114117
generated_query = str(execution_result.context)
115-
data = pd.DataFrame.from_records(execution_result.results)
118+
data = self._load_results_into_dataframe(execution_result.results)
116119
textual_response = str(execution_result.textual_response) if natural_language_flag else textual_response
117120
except UnsupportedQueryError:
118121
generated_query = {"Query": "unsupported"}
@@ -147,6 +150,19 @@ def _clear_results(self) -> Tuple[gradio.DataFrame, gradio.Label, gradio.Text, g
147150
gradio.Text(visible=False),
148151
)
149152

153+
@staticmethod
154+
def _load_results_into_dataframe(results: List[Dict[str, Any]]) -> pd.DataFrame:
155+
"""
156+
Load the results into a pandas DataFrame. Makes sure that the results are json serializable.
157+
158+
Args:
159+
results: The results to load into the DataFrame.
160+
161+
Returns:
162+
The loaded DataFrame.
163+
"""
164+
return pd.DataFrame(json.loads(json.dumps(results, default=str)))
165+
150166
async def create_interface(self, user_collection: Collection, preview_limit: int) -> gradio.Interface:
151167
"""
152168
Creates a Gradio interface for interacting with the user collection and similarity stores.

0 commit comments

Comments
 (0)