Skip to content

Commit

Permalink
fix: gradio interface crash on types that are not json serializable (#47
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mhordynski committed Jun 17, 2024
1 parent 7a3c2e8 commit f501156
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/dbally/gradio/gradio_interface.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from io import StringIO
from typing import Tuple
from typing import Any, Dict, List, Tuple

import gradio
import pandas as pd
Expand Down Expand Up @@ -85,7 +86,9 @@ def _load_preview_data(self, selected_view_name: str) -> pd.DataFrame:
selected_view = self.collection.get(selected_view_name)
if issubclass(type(selected_view), BaseStructuredView):
selected_view_results = selected_view.execute()
preview_dataframe = pd.DataFrame.from_records(selected_view_results.results).head(self.preview_limit)
preview_dataframe = self._load_results_into_dataframe(selected_view_results.results).head(
self.preview_limit
)
else:
preview_dataframe = pd.DataFrame()

Expand All @@ -112,7 +115,7 @@ async def _ui_ask_query(
question=question_query, return_natural_response=natural_language_flag
)
generated_query = str(execution_result.context)
data = pd.DataFrame.from_records(execution_result.results)
data = self._load_results_into_dataframe(execution_result.results)
textual_response = str(execution_result.textual_response) if natural_language_flag else textual_response
except UnsupportedQueryError:
generated_query = {"Query": "unsupported"}
Expand Down Expand Up @@ -147,6 +150,19 @@ def _clear_results(self) -> Tuple[gradio.DataFrame, gradio.Label, gradio.Text, g
gradio.Text(visible=False),
)

@staticmethod
def _load_results_into_dataframe(results: List[Dict[str, Any]]) -> pd.DataFrame:
"""
Load the results into a pandas DataFrame. Makes sure that the results are json serializable.
Args:
results: The results to load into the DataFrame.
Returns:
The loaded DataFrame.
"""
return pd.DataFrame(json.loads(json.dumps(results, default=str)))

async def create_interface(self, user_collection: Collection, preview_limit: int) -> gradio.Interface:
"""
Creates a Gradio interface for interacting with the user collection and similarity stores.
Expand Down

0 comments on commit f501156

Please sign in to comment.