Update gradio_app.py
Browse files- gradio_app.py +41 -47
gradio_app.py
CHANGED
@@ -60,6 +60,33 @@ 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:
|
@@ -77,23 +104,9 @@ def news_analysis(text):
|
|
77 |
# Prepare results for JSON output
|
78 |
json_results = response.json()
|
79 |
|
80 |
-
#
|
81 |
-
|
82 |
-
|
83 |
-
claim_objects = result.get('claim_objects')
|
84 |
-
if isinstance(claim_objects, dict):
|
85 |
-
result['claim_objects'] = list(claim_objects.values())
|
86 |
-
elif not isinstance(claim_objects, list):
|
87 |
-
result['claim_objects'] = [] # Default to empty list if it's not a list or dict
|
88 |
-
|
89 |
-
# Flatten the JSON for 'claim_objects'
|
90 |
-
dataframe_results = pd.json_normalize(
|
91 |
-
json_results,
|
92 |
-
record_path=['claim_objects'], # Nested array to flatten
|
93 |
-
meta=['doc_id', 'details', 'domain'], # Top-level fields to retain
|
94 |
-
sep='_', # Separator for nested fields
|
95 |
-
errors='ignore' # To handle cases with missing keys
|
96 |
-
)
|
97 |
return json_results, dataframe_results
|
98 |
except Exception as e:
|
99 |
results = {'error': str(e)}
|
@@ -115,37 +128,18 @@ def claim_verification(text):
|
|
115 |
# Prepare results for JSON output
|
116 |
json_results = response.json()
|
117 |
|
118 |
-
#
|
119 |
-
|
120 |
-
# Ensure 'support' is a list, or if it's a dict, convert it to a list of its values, else leave it as an empty list
|
121 |
-
support = result.get('support')
|
122 |
-
if isinstance(support, dict):
|
123 |
-
result['support'] = list(support.values())
|
124 |
-
elif not isinstance(support, list):
|
125 |
-
result['support'] = [] # Default to empty list if it's not a list or dict
|
126 |
-
|
127 |
-
# Ensure 'refute' is a list, or if it's a dict, convert it to a list of its values, else leave it as an empty list
|
128 |
-
refute = result.get('refute')
|
129 |
-
if isinstance(refute, dict):
|
130 |
-
result['refute'] = list(refute.values())
|
131 |
-
elif not isinstance(refute, list):
|
132 |
-
result['refute'] = [] # Default to empty list if it's not a list or dict
|
133 |
-
|
134 |
-
# Ensure 'no_info' is a list, or if it's a dict, convert it to a list of its values, else leave it as an empty list
|
135 |
-
no_info = result.get('no_info')
|
136 |
-
if isinstance(no_info, dict):
|
137 |
-
result['no_info'] = list(no_info.values())
|
138 |
-
elif not isinstance(no_info, list):
|
139 |
-
result['no_info'] = [] # Default to empty list if it's not a list or dict
|
140 |
|
141 |
-
# Flatten
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
)
|
|
|
149 |
return json_results, dataframe_results
|
150 |
except Exception as e:
|
151 |
results = {'error': str(e)}
|
|
|
60 |
else:
|
61 |
return "Feedback sent successfully!"
|
62 |
|
63 |
+
# Define the function to preprocess and flatten a specified field in the JSON results
|
64 |
+
def preprocess_and_flatten(json_results, nested_field_name, meta_fields=None):
|
65 |
+
# Ensure 'meta_fields' is a list or set default fields
|
66 |
+
if meta_fields is None:
|
67 |
+
meta_fields = ['doc_id', 'details', 'domain']
|
68 |
+
|
69 |
+
# Preprocess to convert the specified nested field to a list
|
70 |
+
for result in json_results:
|
71 |
+
nested_field = result.get(nested_field_name, None) # Handle missing field
|
72 |
+
if nested_field is None:
|
73 |
+
result[nested_field_name] = [] # Default to empty list if the field is missing
|
74 |
+
elif isinstance(nested_field, dict):
|
75 |
+
result[nested_field_name] = list(nested_field.values())
|
76 |
+
elif not isinstance(nested_field, list):
|
77 |
+
result[nested_field_name] = [] # Default to empty list if it's not a list or dict
|
78 |
+
|
79 |
+
# Flatten the JSON for the specified nested field
|
80 |
+
dataframe_results = pd.json_normalize(
|
81 |
+
json_results,
|
82 |
+
record_path=[nested_field_name], # Nested array to flatten
|
83 |
+
meta=meta_fields, # Top-level fields to retain
|
84 |
+
sep='_', # Separator for nested fields
|
85 |
+
errors='ignore' # To handle cases with missing keys
|
86 |
+
)
|
87 |
+
|
88 |
+
return dataframe_results
|
89 |
+
|
90 |
# Define the functions to handle the inputs and outputs
|
91 |
def news_analysis(text):
|
92 |
try:
|
|
|
104 |
# Prepare results for JSON output
|
105 |
json_results = response.json()
|
106 |
|
107 |
+
# Flatten 'claim_objects' field
|
108 |
+
dataframe_results = preprocess_and_flatten(json_results, 'claim_objects', ['doc_id', 'details', 'domain'])
|
109 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
return json_results, dataframe_results
|
111 |
except Exception as e:
|
112 |
results = {'error': str(e)}
|
|
|
128 |
# Prepare results for JSON output
|
129 |
json_results = response.json()
|
130 |
|
131 |
+
# Flatten 'support' field
|
132 |
+
support_results = preprocess_and_flatten(json_results, 'support', ['doc_id', 'details'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
+
# Flatten 'refute' field
|
135 |
+
refute_results = preprocess_and_flatten(json_results, 'refute', ['doc_id', 'details'])
|
136 |
+
|
137 |
+
# Flatten 'no_info' field
|
138 |
+
no_info_results = preprocess_and_flatten(json_results, 'no_info', ['doc_id', 'details'])
|
139 |
+
|
140 |
+
# concatenate the results if needed
|
141 |
+
dataframe_results = pd.concat([support_results, refute_results, no_info_results], ignore_index=True)
|
142 |
+
|
143 |
return json_results, dataframe_results
|
144 |
except Exception as e:
|
145 |
results = {'error': str(e)}
|