Nischal Subedi commited on
Commit
b9b1527
·
1 Parent(s): d25b399
Files changed (1) hide show
  1. app.py +17 -15
app.py CHANGED
@@ -249,13 +249,13 @@ Answer:"""
249
  # Basic client-side validation for immediate feedback (redundant but good UX)
250
  if not api_key or not api_key.strip() or not api_key.startswith("sk-"):
251
  return "<div class='error-message'><span class='error-icon'></span>Please provide a valid OpenAI API key (starting with 'sk-'). <a href='https://platform.openai.com/api-keys' target='_blank'>OpenAI</a>.</div>"
252
- if not state or state is None or state == "Error: Critical failure loading states": # Added check for error state
253
  return "<div class='error-message'><span class='error-icon'></span>Please select a valid state from the list.</div>"
254
  if not query or not query.strip():
255
  return "<div class='error-message'><span class='error-icon'></span>Please enter your question in the text box.</div>"
256
 
257
- # Call the core processing logic
258
- result = self.assistant.process_query(query=query, state=state, openai_api_key=api_key)
259
  answer = result.get("answer", "<div class='error-message'><span class='error-icon'>⚠️</span>An unexpected error occurred.</div>")
260
 
261
  # Check if the answer already contains an error message
@@ -266,16 +266,16 @@ Answer:"""
266
  return f"<div class='animated-output-content'>{formatted_response_content}</div>"
267
 
268
  try:
269
- available_states_list = self.assistant.get_states()
270
  print(f"DEBUG: States loaded for selection: {available_states_list}")
271
  # Ensure radio_choices is not empty if there's an error and handling the error message
272
- self.state_choices = available_states_list if available_states_list and "Error" not in available_states_list[0] else ["Error: States unavailable"]
273
  # Set initial value only if there are valid choices
274
- self.initial_state_value = self.state_choices[0] if self.state_choices and "Error" not in self.state_choices[0] else None
275
  except Exception as e:
276
  print(f"DEBUG: Error loading states for selection: {e}")
277
- self.state_choices = ["Error: Critical failure loading states"]
278
- self.initial_state_value = None
279
 
280
  example_queries_base = [
281
  ["What are the rules for security deposit returns?", "California"],
@@ -285,11 +285,11 @@ Answer:"""
285
  ["What is an implied warranty of habitability?", "Illinois"]
286
  ]
287
  example_queries = []
288
- if self.state_choices and "Error" not in self.state_choices[0] and len(self.state_choices) > 0:
289
- loaded_states_set = set(self.state_choices)
290
  example_queries = [ex for ex in example_queries_base if ex[1] in loaded_states_set]
291
  if not example_queries: # Fallback if no matching examples found
292
- example_queries.append(["What basic rights do tenants have?", self.state_choices[0]])
293
  else: # If states failed to load, provide a generic example
294
  example_queries.append(["What basic rights do tenants have?", "California"])
295
 
@@ -856,7 +856,8 @@ Answer:"""
856
  }}
857
  """
858
 
859
- # Ensure theme_mode="light" is explicitly set
 
860
  with gr.Blocks(css=custom_css, title="Landlord-Tenant Rights Assistant", theme_mode="light") as demo:
861
  with gr.Group(elem_classes="app-header-wrapper"):
862
  gr.Markdown(
@@ -911,8 +912,8 @@ Answer:"""
911
  with gr.Column(elem_classes="input-field", scale=1):
912
  state_input = gr.Radio(
913
  label="Select State",
914
- choices=self.state_choices, # Use self.state_choices
915
- value=self.initial_state_value, # Use self.initial_state_value
916
  elem_classes=["input-field-group", "gradio-radio-custom"],
917
  interactive=True
918
  )
@@ -968,7 +969,7 @@ Answer:"""
968
 
969
  clear_button.click(
970
  fn=lambda: (
971
- "", "", self.initial_state_value, "<div class='placeholder'>Inputs cleared. Ready for your next question.</div>"
972
  ),
973
  inputs=[],
974
  outputs=[api_key_input, query_input, state_input, output]
@@ -977,6 +978,7 @@ Answer:"""
977
  return demo
978
 
979
 
 
980
  # --- Main Execution Block (UNCHANGED from original logic) ---
981
  if __name__ == "__main__":
982
  logging.info("Starting Landlord-Tenant Rights Bot application...")
 
249
  # Basic client-side validation for immediate feedback (redundant but good UX)
250
  if not api_key or not api_key.strip() or not api_key.startswith("sk-"):
251
  return "<div class='error-message'><span class='error-icon'></span>Please provide a valid OpenAI API key (starting with 'sk-'). <a href='https://platform.openai.com/api-keys' target='_blank'>OpenAI</a>.</div>"
252
+ if not state or state is None or state == "Error: Critical failure loading states" or state == "Error: States unavailable": # Added check for error state
253
  return "<div class='error-message'><span class='error-icon'></span>Please select a valid state from the list.</div>"
254
  if not query or not query.strip():
255
  return "<div class='error-message'><span class='error-icon'></span>Please enter your question in the text box.</div>"
256
 
257
+ # Call the core processing logic (using self.process_query)
258
+ result = self.process_query(query=query, state=state, openai_api_key=api_key)
259
  answer = result.get("answer", "<div class='error-message'><span class='error-icon'>⚠️</span>An unexpected error occurred.</div>")
260
 
261
  # Check if the answer already contains an error message
 
266
  return f"<div class='animated-output-content'>{formatted_response_content}</div>"
267
 
268
  try:
269
+ available_states_list = self.get_states() # Call self.get_states() directly
270
  print(f"DEBUG: States loaded for selection: {available_states_list}")
271
  # Ensure radio_choices is not empty if there's an error and handling the error message
272
+ radio_choices = available_states_list if available_states_list and "Error" not in available_states_list[0] else ["Error: States unavailable"]
273
  # Set initial value only if there are valid choices
274
+ initial_value_radio = radio_choices[0] if radio_choices and "Error" not in radio_choices[0] else None
275
  except Exception as e:
276
  print(f"DEBUG: Error loading states for selection: {e}")
277
+ radio_choices = ["Error: Critical failure loading states"]
278
+ initial_value_radio = None
279
 
280
  example_queries_base = [
281
  ["What are the rules for security deposit returns?", "California"],
 
285
  ["What is an implied warranty of habitability?", "Illinois"]
286
  ]
287
  example_queries = []
288
+ if radio_choices and "Error" not in radio_choices[0] and len(radio_choices) > 0:
289
+ loaded_states_set = set(radio_choices)
290
  example_queries = [ex for ex in example_queries_base if ex[1] in loaded_states_set]
291
  if not example_queries: # Fallback if no matching examples found
292
+ example_queries.append(["What basic rights do tenants have?", radio_choices[0]])
293
  else: # If states failed to load, provide a generic example
294
  example_queries.append(["What basic rights do tenants have?", "California"])
295
 
 
856
  }}
857
  """
858
 
859
+ # MODIFIED LINE: Added theme_mode="light"
860
+ # This will work after upgrading Gradio
861
  with gr.Blocks(css=custom_css, title="Landlord-Tenant Rights Assistant", theme_mode="light") as demo:
862
  with gr.Group(elem_classes="app-header-wrapper"):
863
  gr.Markdown(
 
912
  with gr.Column(elem_classes="input-field", scale=1):
913
  state_input = gr.Radio(
914
  label="Select State",
915
+ choices=radio_choices,
916
+ value=initial_value_radio,
917
  elem_classes=["input-field-group", "gradio-radio-custom"],
918
  interactive=True
919
  )
 
969
 
970
  clear_button.click(
971
  fn=lambda: (
972
+ "", "", initial_value_radio, "<div class='placeholder'>Inputs cleared. Ready for your next question.</div>"
973
  ),
974
  inputs=[],
975
  outputs=[api_key_input, query_input, state_input, output]
 
978
  return demo
979
 
980
 
981
+
982
  # --- Main Execution Block (UNCHANGED from original logic) ---
983
  if __name__ == "__main__":
984
  logging.info("Starting Landlord-Tenant Rights Bot application...")