import gradio as gr import json from engine import final_process_eme_technical_guided def parse_energy_plan_ui(text_input): """Hàm xử lý logic trích xuất dữ liệu""" if not text_input or not text_input.strip(): return json.dumps({"error": "Input không được để trống"}, indent=2, ensure_ascii=False) try: # Xử lý input đầu vào final_input = text_input.strip() # Gọi engine xử lý (đảm bảo engine.py không chứa code nặng ở global) result = final_process_eme_technical_guided(final_input) # Chuyển đổi kết quả sang dict 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) # Tạo giao diện with gr.Blocks(theme=gr.themes.Soft(primary_hue="yellow", secondary_hue="amber"), title="Volta Parser") as demo: gr.HTML("""

Volta Energy Plan Parser

Dán JSON hợp đồng điện để trích xuất dữ liệu chi tiết

""") with gr.Row(): # Cột Input with gr.Column(scale=1): text_input = gr.Textbox( label="Energy Plan Input (JSON)", placeholder="Paste the energy plan JSON here...", lines=15 ) # Nút Submit nằm gọn trong cột btn_submit = gr.Button("Submit", variant="primary") # Cột Output with gr.Column(scale=1): json_output = gr.Code( label="Extracted Schema (JSON Output)", language="json", lines=17 ) # Sự kiện click btn_submit.click(fn=parse_energy_plan_ui, inputs=text_input, outputs=json_output) # Chạy ứng dụng if __name__ == "__main__": # Server port 7860 là mặc định của Hugging Face Spaces demo.launch(server_name="0.0.0.0", server_port=7860)