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