Spaces:
Running
Running
async test
Browse files
app.py
CHANGED
@@ -4,27 +4,26 @@ import time
|
|
4 |
import traceback
|
5 |
from validation import validate_json, validate_croissant, validate_records
|
6 |
import requests
|
7 |
-
import aiohttp
|
8 |
|
9 |
-
|
10 |
results = []
|
11 |
|
12 |
# Check 1: JSON validation
|
13 |
-
json_valid, json_message, json_data =
|
14 |
results.append(("JSON Format Validation", json_valid, json_message))
|
15 |
|
16 |
if not json_valid:
|
17 |
return results
|
18 |
|
19 |
# Check 2: Croissant validation
|
20 |
-
croissant_valid, croissant_message =
|
21 |
results.append(("Croissant Schema Validation", croissant_valid, croissant_message))
|
22 |
|
23 |
if not croissant_valid:
|
24 |
return results
|
25 |
|
26 |
# Check 3: Records validation
|
27 |
-
records_valid, records_message =
|
28 |
results.append(("Records Generation Test", records_valid, records_message))
|
29 |
|
30 |
return results
|
@@ -182,16 +181,15 @@ def create_ui():
|
|
182 |
|
183 |
return """<div class="progress-status">β
File uploaded successfully</div>""", gr.update(visible=False)
|
184 |
|
185 |
-
|
186 |
if not url:
|
187 |
return """<div class="progress-status">Please enter a URL</div>""", gr.update(visible=False)
|
188 |
|
189 |
try:
|
190 |
# Fetch JSON from URL
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
json_data = await response.json()
|
195 |
|
196 |
# Show success message
|
197 |
progress_html = """<div class="progress-status">β
JSON fetched successfully from URL</div>"""
|
@@ -200,18 +198,18 @@ def create_ui():
|
|
200 |
results = []
|
201 |
results.append(("JSON Format Validation", True, "β
The URL returned valid JSON."))
|
202 |
|
203 |
-
croissant_valid, croissant_message =
|
204 |
results.append(("Croissant Schema Validation", croissant_valid, croissant_message))
|
205 |
|
206 |
if not croissant_valid:
|
207 |
return progress_html, build_results_html(results)
|
208 |
|
209 |
-
records_valid, records_message =
|
210 |
results.append(("Records Generation Test", records_valid, records_message))
|
211 |
|
212 |
return progress_html, build_results_html(results)
|
213 |
|
214 |
-
except
|
215 |
error_message = f"β Error fetching URL: {str(e)}"
|
216 |
return f"""<div class="progress-status">{error_message}</div>""", gr.update(visible=False)
|
217 |
except json.JSONDecodeError as e:
|
@@ -256,19 +254,19 @@ def create_ui():
|
|
256 |
html += '</div>'
|
257 |
return gr.update(value=html, visible=True)
|
258 |
|
259 |
-
|
260 |
if file is None:
|
261 |
return gr.update(visible=False)
|
262 |
|
263 |
# Process the file and get results
|
264 |
-
results =
|
265 |
return build_results_html(results)
|
266 |
|
267 |
# Connect UI events to functions
|
268 |
tabs.select(on_tab_change, None, [active_tab, upload_progress, validation_results])
|
269 |
file_input.change(on_file_upload, inputs=file_input, outputs=[upload_progress, validation_results])
|
270 |
-
validate_btn.click(
|
271 |
-
fetch_btn.click(
|
272 |
|
273 |
# Footer
|
274 |
gr.HTML("""
|
|
|
4 |
import traceback
|
5 |
from validation import validate_json, validate_croissant, validate_records
|
6 |
import requests
|
|
|
7 |
|
8 |
+
def process_file(file):
|
9 |
results = []
|
10 |
|
11 |
# Check 1: JSON validation
|
12 |
+
json_valid, json_message, json_data = validate_json(file.name)
|
13 |
results.append(("JSON Format Validation", json_valid, json_message))
|
14 |
|
15 |
if not json_valid:
|
16 |
return results
|
17 |
|
18 |
# Check 2: Croissant validation
|
19 |
+
croissant_valid, croissant_message = validate_croissant(json_data)
|
20 |
results.append(("Croissant Schema Validation", croissant_valid, croissant_message))
|
21 |
|
22 |
if not croissant_valid:
|
23 |
return results
|
24 |
|
25 |
# Check 3: Records validation
|
26 |
+
records_valid, records_message = validate_records(json_data)
|
27 |
results.append(("Records Generation Test", records_valid, records_message))
|
28 |
|
29 |
return results
|
|
|
181 |
|
182 |
return """<div class="progress-status">β
File uploaded successfully</div>""", gr.update(visible=False)
|
183 |
|
184 |
+
def fetch_from_url(url):
|
185 |
if not url:
|
186 |
return """<div class="progress-status">Please enter a URL</div>""", gr.update(visible=False)
|
187 |
|
188 |
try:
|
189 |
# Fetch JSON from URL
|
190 |
+
response = requests.get(url, timeout=10)
|
191 |
+
response.raise_for_status()
|
192 |
+
json_data = response.json()
|
|
|
193 |
|
194 |
# Show success message
|
195 |
progress_html = """<div class="progress-status">β
JSON fetched successfully from URL</div>"""
|
|
|
198 |
results = []
|
199 |
results.append(("JSON Format Validation", True, "β
The URL returned valid JSON."))
|
200 |
|
201 |
+
croissant_valid, croissant_message = validate_croissant(json_data)
|
202 |
results.append(("Croissant Schema Validation", croissant_valid, croissant_message))
|
203 |
|
204 |
if not croissant_valid:
|
205 |
return progress_html, build_results_html(results)
|
206 |
|
207 |
+
records_valid, records_message = validate_records(json_data)
|
208 |
results.append(("Records Generation Test", records_valid, records_message))
|
209 |
|
210 |
return progress_html, build_results_html(results)
|
211 |
|
212 |
+
except requests.exceptions.RequestException as e:
|
213 |
error_message = f"β Error fetching URL: {str(e)}"
|
214 |
return f"""<div class="progress-status">{error_message}</div>""", gr.update(visible=False)
|
215 |
except json.JSONDecodeError as e:
|
|
|
254 |
html += '</div>'
|
255 |
return gr.update(value=html, visible=True)
|
256 |
|
257 |
+
def on_validate(file):
|
258 |
if file is None:
|
259 |
return gr.update(visible=False)
|
260 |
|
261 |
# Process the file and get results
|
262 |
+
results = process_file(file)
|
263 |
return build_results_html(results)
|
264 |
|
265 |
# Connect UI events to functions
|
266 |
tabs.select(on_tab_change, None, [active_tab, upload_progress, validation_results])
|
267 |
file_input.change(on_file_upload, inputs=file_input, outputs=[upload_progress, validation_results])
|
268 |
+
validate_btn.click(on_validate, inputs=file_input, outputs=validation_results)
|
269 |
+
fetch_btn.click(fetch_from_url, inputs=url_input, outputs=[upload_progress, validation_results])
|
270 |
|
271 |
# Footer
|
272 |
gr.HTML("""
|