ABDALLAH31 commited on
Commit
e884a72
·
verified ·
1 Parent(s): ffc07e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -31
app.py CHANGED
@@ -1,37 +1,34 @@
 
1
  import requests
2
- import json
3
 
4
- # Replace with your Hugging Face API Key and Model URL
5
- API_URL = "https://api-inference.huggingface.co/models/bert-base-uncased" # Change this URL to your model
6
- API_KEY = "hf_YOUR_API_KEY" # Replace this with your actual Hugging Face API key
7
 
8
- # Function to send contract text to Hugging Face API and get analysis results
9
  def analyze_contract(contract_text):
10
  headers = {
11
  "Authorization": f"Bearer {API_KEY}",
12
  "Content-Type": "application/json"
13
  }
14
-
15
- # Prepare the payload with the contract text
16
- payload = {
17
- "inputs": contract_text
18
- }
19
-
20
- # Send the request to Hugging Face
21
  response = requests.post(API_URL, headers=headers, json=payload)
22
 
23
  if response.status_code == 200:
24
  try:
25
- # Parse the response JSON
26
  result = response.json()
27
 
28
  # Print the raw response for debugging
29
  print("Raw Response from Hugging Face:", result)
30
 
31
- # Example: Extracting fields from the response (based on your expected output structure)
32
- # If the model returns 'overall_score', 'high_risk_clauses', and 'risk_map_url', you can parse it here
33
  overall_risk_score = result.get('overall_score', 'No score available')
34
- high_risk_clauses = result.get('high_risk_clauses', [])
35
  risk_map_url = result.get('risk_map_url', 'No risk map available')
36
 
37
  return overall_risk_score, high_risk_clauses, risk_map_url
@@ -39,20 +36,25 @@ def analyze_contract(contract_text):
39
  except Exception as e:
40
  return f"Error in processing response: {str(e)}", "", ""
41
  else:
42
- # Handle the error if API request fails
43
  return f"Error: Unable to analyze the contract, Status Code: {response.status_code} - {response.text}", "", ""
44
 
45
- # Example contract text for testing
46
- contract_text = """
47
- This Agreement is made between Party A (Seller) and Party B (Buyer).
48
- 1. Termination Clause: The agreement can be terminated with a 30-day written notice.
49
- 2. Indemnity Clause: Party B agrees to indemnify Party A for any losses.
50
- """
51
-
52
- # Run the analysis
53
- risk_score, high_risk_clauses, risk_map_url = analyze_contract(contract_text)
54
-
55
- # Print the results
56
- print("Overall Risk Score:", risk_score)
57
- print("High Risk Clauses:", high_risk_clauses)
58
- print("Risk Map URL:", risk_map_url)
 
 
 
 
 
 
 
1
+ import gradio as gr
2
  import requests
3
+ import os
4
 
5
+ # Hugging Face API URL for LegalBERT model (replace with your model URL)
6
+ API_URL = "https://api-inference.huggingface.co/models/joeddav/legal-bert" # LegalBERT example URL
7
+ API_KEY = os.getenv("HF_API_KEY") # Fetch API key securely from environment variable
8
 
9
+ # Function to send contract to Hugging Face API and get analysis results
10
  def analyze_contract(contract_text):
11
  headers = {
12
  "Authorization": f"Bearer {API_KEY}",
13
  "Content-Type": "application/json"
14
  }
15
+
16
+ payload = {"inputs": contract_text}
17
+
18
+ # Send the contract text to Hugging Face for analysis
 
 
 
19
  response = requests.post(API_URL, headers=headers, json=payload)
20
 
21
  if response.status_code == 200:
22
  try:
 
23
  result = response.json()
24
 
25
  # Print the raw response for debugging
26
  print("Raw Response from Hugging Face:", result)
27
 
28
+ # Extracting the risk score, high-risk clauses, and the risk map URL
29
+ # (The actual keys here may vary depending on how your model is trained)
30
  overall_risk_score = result.get('overall_score', 'No score available')
31
+ high_risk_clauses = result.get('high_risk_clauses', 'No high-risk clauses detected')
32
  risk_map_url = result.get('risk_map_url', 'No risk map available')
33
 
34
  return overall_risk_score, high_risk_clauses, risk_map_url
 
36
  except Exception as e:
37
  return f"Error in processing response: {str(e)}", "", ""
38
  else:
 
39
  return f"Error: Unable to analyze the contract, Status Code: {response.status_code} - {response.text}", "", ""
40
 
41
+ # Gradio UI setup
42
+ def create_ui():
43
+ # Gradio interface
44
+ with gr.Blocks() as demo:
45
+ gr.Markdown("### Contract Risk Analyzer")
46
+ with gr.Row():
47
+ contract_input = gr.Textbox(label="Contract Text", placeholder="Paste contract text here", lines=10)
48
+ with gr.Row():
49
+ analyze_button = gr.Button("Analyze Contract")
50
+ with gr.Row():
51
+ risk_score_output = gr.Textbox(label="Overall Risk Score", interactive=False)
52
+ high_risk_clauses_output = gr.Textbox(label="High Risk Clauses", interactive=False)
53
+ risk_map_url_output = gr.Textbox(label="Risk Map URL", interactive=False)
54
+
55
+ analyze_button.click(fn=analyze_contract, inputs=contract_input, outputs=[risk_score_output, high_risk_clauses_output, risk_map_url_output])
56
+
57
+ demo.launch()
58
+
59
+ if __name__ == "__main__":
60
+ create_ui()