baconnier commited on
Commit
dfb32d0
·
verified ·
1 Parent(s): aa0bda3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -6
app.py CHANGED
@@ -39,6 +39,28 @@ class ArtExplorer:
39
  )
40
  return fig
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  def get_llm_response(self, query: str, zoom_context: dict = None) -> dict:
43
  try:
44
  current_zoom_states = {
@@ -70,16 +92,23 @@ class ArtExplorer:
70
  )
71
  print("Received response from LLM")
72
 
73
- # Clean and parse the response
74
- content = response.choices[0].message.content.strip()
 
 
 
 
 
 
75
  try:
76
- result = json.loads(content)
 
 
77
  except json.JSONDecodeError as e:
78
  print(f"JSON parsing error: {str(e)}")
79
- print(f"Raw content: {content}")
80
  return self.get_default_response()
81
 
82
- return result
83
  except Exception as e:
84
  print(f"Error in LLM response: {str(e)}")
85
  return self.get_default_response()
@@ -118,7 +147,7 @@ class ArtExplorer:
118
 
119
  with gr.Row():
120
  style_select = gr.Dropdown(
121
- choices=["Classical", "Modern"], # Set initial choices
122
  multiselect=True,
123
  label="Artistic Styles",
124
  allow_custom_value=True # Allow new values
 
39
  )
40
  return fig
41
 
42
+ def clean_json_string(self, json_str: str) -> str:
43
+ """Clean and prepare JSON string for parsing"""
44
+ # Remove any leading/trailing whitespace and newlines
45
+ json_str = json_str.strip()
46
+
47
+ # Remove any BOM or special characters at the start
48
+ if json_str.startswith('\ufeff'):
49
+ json_str = json_str[1:]
50
+
51
+ # Ensure it starts with {
52
+ if not json_str.startswith('{'):
53
+ start_idx = json_str.find('{')
54
+ if start_idx != -1:
55
+ json_str = json_str[start_idx:]
56
+
57
+ # Ensure it ends with }
58
+ end_idx = json_str.rfind('}')
59
+ if end_idx != -1:
60
+ json_str = json_str[:end_idx+1]
61
+
62
+ return json_str
63
+
64
  def get_llm_response(self, query: str, zoom_context: dict = None) -> dict:
65
  try:
66
  current_zoom_states = {
 
92
  )
93
  print("Received response from LLM")
94
 
95
+ # Get the response content and clean it
96
+ content = response.choices[0].message.content
97
+ print(f"Raw LLM response: {content}")
98
+
99
+ # Clean the JSON string
100
+ cleaned_content = self.clean_json_string(content)
101
+ print(f"Cleaned JSON string: {cleaned_content}")
102
+
103
  try:
104
+ result = json.loads(cleaned_content)
105
+ print("Successfully parsed JSON")
106
+ return result
107
  except json.JSONDecodeError as e:
108
  print(f"JSON parsing error: {str(e)}")
109
+ print(f"Failed to parse JSON: {cleaned_content}")
110
  return self.get_default_response()
111
 
 
112
  except Exception as e:
113
  print(f"Error in LLM response: {str(e)}")
114
  return self.get_default_response()
 
147
 
148
  with gr.Row():
149
  style_select = gr.Dropdown(
150
+ choices=["Classical", "Modern"], # Initial choices
151
  multiselect=True,
152
  label="Artistic Styles",
153
  allow_custom_value=True # Allow new values