Spaces:
Sleeping
Sleeping
Update gradio_app.py
Browse files- gradio_app.py +4 -46
gradio_app.py
CHANGED
@@ -60,33 +60,6 @@ def toggle_feedback(request_data, response_data, like_clicked, dislike_clicked):
|
|
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:
|
@@ -100,13 +73,10 @@ def news_analysis(text):
|
|
100 |
headers=HEADERS
|
101 |
)
|
102 |
response.raise_for_status()
|
103 |
-
|
104 |
# Prepare results for JSON output
|
105 |
json_results = response.json()
|
106 |
-
|
107 |
-
|
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)}
|
@@ -124,22 +94,10 @@ def claim_verification(text):
|
|
124 |
headers=HEADERS
|
125 |
)
|
126 |
response.raise_for_status()
|
127 |
-
|
128 |
# Prepare results for JSON output
|
129 |
json_results = response.json()
|
130 |
-
|
131 |
-
|
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)}
|
|
|
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:
|
|
|
73 |
headers=HEADERS
|
74 |
)
|
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)}
|
|
|
94 |
headers=HEADERS
|
95 |
)
|
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)}
|