import gradio as gr import json import os def parse_energy_plan(text_input): from engine import final_process_eme_technical_guided if not text_input or not text_input.strip(): return json.dumps({"error": "Input cannot be empty"}, indent=2) try: final_input = text_input try: # Thêm dấu ngoặc nếu thiếu stripped = text_input.strip() if not stripped.startswith("{") and not stripped.startswith("["): stripped = "{" + stripped + "}" clean_input = stripped.replace("None", "null").replace("True", "true").replace("False", "false") parsed_json = json.loads(clean_input) if isinstance(parsed_json, dict) and "instances" in parsed_json: final_input = parsed_json["instances"][0].get("raw_text", text_input) else: final_input = json.dumps(parsed_json) except json.JSONDecodeError: final_input = text_input print(f"=== final_input preview: {final_input[:200]}") result = final_process_eme_technical_guided(final_input) result_dict = result.model_dump() if hasattr(result, "model_dump") else result return json.dumps(result_dict, indent=2, ensure_ascii=False) except Exception as e: return json.dumps({"error": str(e)}, indent=2, ensure_ascii=False) demo = gr.Interface( fn=parse_energy_plan, inputs=gr.Textbox(label="Energy Plan Input", placeholder="Paste the energy plan JSON here...", lines=10), outputs=gr.Code(label="Extracted Schema", language="json"), title="Volta Energy Plan Parser", description="Energy Data Extraction Testing Interface" ) if __name__ == "__main__": demo.launch(server_name="0.0.0.0", server_port=8080, ssr_mode=False)