PanagiotisMark commited on
Commit
039d674
·
verified ·
1 Parent(s): 02c2307

Update gradio_app.py

Browse files
Files changed (1) hide show
  1. gradio_app.py +54 -4
gradio_app.py CHANGED
@@ -60,6 +60,56 @@ def toggle_feedback(request_data, response_data, like_clicked, dislike_clicked):
60
  else:
61
  return "Feedback sent successfully!"
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  # Define the functions to handle the inputs and outputs
64
  def news_analysis(text):
65
  try:
@@ -75,8 +125,8 @@ def news_analysis(text):
75
  response.raise_for_status()
76
  # Prepare results for JSON output
77
  json_results = response.json()
78
- # Normalize the JSON data
79
- dataframe_results = pd.json_normalize(json_results, sep='_')
80
  return json_results, dataframe_results
81
  except Exception as e:
82
  results = {'error': str(e)}
@@ -96,8 +146,8 @@ def claim_verification(text):
96
  response.raise_for_status()
97
  # Prepare results for JSON output
98
  json_results = response.json()
99
- # Normalize the JSON data
100
- dataframe_results = pd.json_normalize(json_results, sep='_')
101
  return json_results, dataframe_results
102
  except Exception as e:
103
  results = {'error': str(e)}
 
60
  else:
61
  return "Feedback sent successfully!"
62
 
63
+ def preprocess_and_flatten(json_results, nested_fields, meta_fields=None):
64
+ # Ensure 'meta_fields' is a list or set default fields
65
+ if meta_fields is None:
66
+ meta_fields = ['doc_id', 'details', 'domain']
67
+
68
+ # Check if json_results is a valid dictionary
69
+ if not isinstance(json_results, dict):
70
+ print(f"Invalid JSON results: Expected a dictionary but got {type(json_results)}")
71
+ return pd.DataFrame() # Return an empty DataFrame if json_results is not a dictionary
72
+
73
+ # Preprocess each field in the nested_fields list
74
+ for field in nested_fields:
75
+ if field in json_results:
76
+ nested_field = json_results.get(field)
77
+ if isinstance(nested_field, dict):
78
+ json_results[field] = [nested_field] # Wrap dict in a list if it's a dict
79
+ elif not isinstance(nested_field, list):
80
+ json_results[field] = [] # Default to empty list if not a list or dict
81
+
82
+ # Flatten the JSON for each field in nested_fields
83
+ dataframes = []
84
+ for field in nested_fields:
85
+ # Check if the field exists in json_results
86
+ if field not in json_results or not isinstance(json_results[field], list):
87
+ print(f"Field '{field}' not found or is not a list in the JSON results.")
88
+ continue # Skip this field if it's missing or not a list
89
+
90
+ try:
91
+ # Flatten the JSON for the specified nested field
92
+ dataframe_results = pd.json_normalize(
93
+ json_results,
94
+ record_path=[field], # Nested array to flatten
95
+ meta=meta_fields, # Top-level fields to retain
96
+ sep='_', # Separator for nested fields
97
+ errors='ignore' # To handle cases with missing keys
98
+ )
99
+ if not dataframe_results.empty:
100
+ dataframes.append(dataframe_results) # Append the flattened DataFrame
101
+ except Exception as e:
102
+ print(f"Error while flattening field '{field}': {e}")
103
+ continue # Skip this field if an error occurs
104
+
105
+ # Concatenate all the DataFrames if there are any
106
+ if dataframes:
107
+ final_dataframe = pd.concat(dataframes, ignore_index=True)
108
+ else:
109
+ final_dataframe = pd.DataFrame() # Return an empty DataFrame if nothing was flattened
110
+
111
+ return final_dataframe
112
+
113
  # Define the functions to handle the inputs and outputs
114
  def news_analysis(text):
115
  try:
 
125
  response.raise_for_status()
126
  # Prepare results for JSON output
127
  json_results = response.json()
128
+ # Flatten 'claim_objects' field
129
+ dataframe_results = preprocess_and_flatten(json_results, ['claim_objects'], ['doc_id', 'details', 'domain'])
130
  return json_results, dataframe_results
131
  except Exception as e:
132
  results = {'error': str(e)}
 
146
  response.raise_for_status()
147
  # Prepare results for JSON output
148
  json_results = response.json()
149
+ # Call preprocess_and_flatten to flatten 'support', 'refute', and 'no_info' fields
150
+ dataframe_results = preprocess_and_flatten(json_results, ['support', 'refute', 'no_info'], ['doc_id', 'details'])
151
  return json_results, dataframe_results
152
  except Exception as e:
153
  results = {'error': str(e)}