Spaces:
Sleeping
Sleeping
init
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
from typing import Dict, Any
|
4 |
+
|
5 |
+
def validate_json(json_text: str) -> Dict[str, Any]:
|
6 |
+
try:
|
7 |
+
parsed_json = json.loads(json_text)
|
8 |
+
return {"is_valid": True, "parsed_json": parsed_json}
|
9 |
+
except json.JSONDecodeError as e:
|
10 |
+
return {"is_valid": False, "error": str(e)}
|
11 |
+
|
12 |
+
def display_result(result: Dict[str, Any]) -> str:
|
13 |
+
if result["is_valid"]:
|
14 |
+
return f"JSON 有效。解析結果:\n{json.dumps(result['parsed_json'], indent=2, ensure_ascii=False)}"
|
15 |
+
else:
|
16 |
+
return f"JSON 無效。錯誤:{result['error']}"
|
17 |
+
|
18 |
+
def create_interface() -> gr.Blocks:
|
19 |
+
with gr.Blocks() as demo:
|
20 |
+
gr.Markdown("# JSON 驗證器")
|
21 |
+
|
22 |
+
with gr.Row():
|
23 |
+
json_input = gr.Textbox(label="請輸入 JSON 文字", lines=5)
|
24 |
+
output = gr.Textbox(label="驗證結果", lines=5)
|
25 |
+
|
26 |
+
validate_button = gr.Button("驗證 JSON")
|
27 |
+
validate_button.click(
|
28 |
+
fn=lambda x: display_result(validate_json(x)),
|
29 |
+
inputs=json_input,
|
30 |
+
outputs=output
|
31 |
+
)
|
32 |
+
|
33 |
+
return demo
|
34 |
+
|
35 |
+
if __name__ == "__main__":
|
36 |
+
interface = create_interface()
|
37 |
+
interface.launch()
|