Raje19112003/Invoice_Dispute_Resolution_Environment
0
1"""2Gradio UI for Invoice Dispute Resolution Environment3Run this locally: python ui.py4Or deploy to Hugging Face Spaces5"""6 7import gradio as gr8import requests9import json10 11# Base URL for API12BASE_URL = "http://localhost:7860"13 14# Global state15current_state = None16current_difficulty = "medium"17 18 19def reset_environment(difficulty):20 """Reset the environment with selected difficulty"""21 global current_state, current_difficulty22 current_difficulty = difficulty23 24 try:25 response = requests.post(26 f"{BASE_URL}/reset",27 json={"difficulty": difficulty},28 timeout=1029 )30 response.raise_for_status()31 obs = response.json()32 33 # Get initial state34 state_response = requests.get(f"{BASE_URL}/state", timeout=10)35 current_state = state_response.json()36 37 return json.dumps(obs, indent=2)38 except Exception as e:39 return json.dumps({"error": str(e)})40 41 42def submit_decision(decision, response_text, refund_amount):43 """Submit a decision to the environment"""44 try:45 action = {46 "decision": decision,47 "response_text": response_text,48 "refund_amount": float(refund_amount) if decision == "partial_refund" and refund_amount else None49 }50 51 response = requests.post(52 f"{BASE_URL}/step",53 json=action,54 timeout=1055 )56 response.raise_for_status()57 obs = response.json()58 59 return json.dumps(obs, indent=2)60 except Exception as e:61 return json.dumps({"error": str(e)})62 63 64def get_current_state():65 """Get current dispute state"""66 try:67 response = requests.get(f"{BASE_URL}/state", timeout=10)68 response.raise_for_status()69 state = response.json()70 return json.dumps(state, indent=2)71 except Exception as e:72 return json.dumps({"error": str(e)})73 74 75# Gradio Interface76with gr.Blocks(title="Invoice Dispute Resolution") as demo:77 gr.Markdown("# ๐ฆ Invoice Dispute Resolution Environment")78 gr.Markdown("An AI-powered system for resolving billing disputes using reinforcement learning.")79 80 with gr.Row():81 with gr.Column():82 gr.Markdown("## ๐ Episode Control")83 84 difficulty = gr.Radio(85 choices=["easy", "medium", "hard"],86 value="medium",87 label="Select Difficulty Level"88 )89 90 reset_btn = gr.Button("๐ Start New Episode", variant="primary")91 92 state_output = gr.Textbox(93 label="Current State",94 interactive=False,95 lines=1096 )97 98 with gr.Column():99 gr.Markdown("## ๐ฏ Make a Decision")100 101 decision = gr.Radio(102 choices=[103 ("โ
Full Refund", "full_refund"),104 ("๐ Partial Refund", "partial_refund"),105 ("โ Reject Dispute", "reject"),106 ("๐ Escalate to Manager", "escalate"),107 ("โ Request More Info", "request_info")108 ],109 label="Decision",110 value="full_refund"111 )112 113 response_text = gr.Textbox(114 label="Customer Response Message",115 placeholder="Draft your professional response...",116 lines=5117 )118 119 refund_amount = gr.Number(120 label="Refund Amount (if partial refund)",121 value=0,122 visible=True123 )124 125 submit_btn = gr.Button("๐ค Submit Decision", variant="primary")126 127 feedback_output = gr.Textbox(128 label="Feedback & Reward",129 interactive=False,130 lines=10131 )132 133 # Connect buttons134 reset_btn.click(135 fn=reset_environment,136 inputs=[difficulty],137 outputs=[state_output]138 )139 140 submit_btn.click(141 fn=submit_decision,142 inputs=[decision, response_text, refund_amount],143 outputs=[feedback_output]144 )145 146 # Load state on demand147 gr.Markdown("---")148 gr.Markdown("## ๐ Full API Response")149 150 load_state_btn = gr.Button("๐ Refresh State")151 full_state_output = gr.Textbox(152 label="Full Environment State",153 interactive=False,154 lines=15155 )156 157 load_state_btn.click(158 fn=get_current_state,159 outputs=[full_state_output]160 )161 162 163if __name__ == "__main__":164 demo.launch(share=True)165 