import gradio as gr import requests import os # Set the Hugging Face API URL for your model API_URL = "https://api-inference.huggingface.co/models/nlpaueb/legal-bert-base-uncased" API_KEY = os.getenv("HF_API_KEY") # Securely fetch the API key from an environment variable # Function to send contract text to Hugging Face API and get analysis results def analyze_contract(contract_text): headers = { "Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json" } payload = {"inputs": contract_text} # Send the contract text to Hugging Face for analysis response = requests.post(API_URL, headers=headers, json=payload) if response.status_code == 200: try: result = response.json() # Print the raw response for debugging print("Raw Response from Hugging Face:", result) # Extracting the risk score, high-risk clauses, and the risk map URL # (The actual keys here may vary depending on how your model is trained) overall_risk_score = result.get('overall_score', 'No score available') high_risk_clauses = result.get('high_risk_clauses', 'No high-risk clauses detected') risk_map_url = result.get('risk_map_url', 'No risk map available') return overall_risk_score, high_risk_clauses, risk_map_url except Exception as e: return f"Error in processing response: {str(e)}", "", "" else: return f"Error: Unable to analyze the contract, Status Code: {response.status_code} - {response.text}", "", "" # Gradio UI setup def create_ui(): # Gradio interface with gr.Blocks() as demo: gr.Markdown("### Contract Risk Analyzer") with gr.Row(): contract_input = gr.Textbox(label="Contract Text", placeholder="Paste contract text here", lines=10) with gr.Row(): analyze_button = gr.Button("Analyze Contract") with gr.Row(): risk_score_output = gr.Textbox(label="Overall Risk Score", interactive=False) high_risk_clauses_output = gr.Textbox(label="High Risk Clauses", interactive=False) risk_map_url_output = gr.Textbox(label="Risk Map URL", interactive=False) analyze_button.click(fn=analyze_contract, inputs=contract_input, outputs=[risk_score_output, high_risk_clauses_output, risk_map_url_output]) demo.launch() if __name__ == "__main__": create_ui()