ABDALLAH31 commited on
Commit
e846a12
·
verified ·
1 Parent(s): c05eef1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -27
app.py CHANGED
@@ -1,11 +1,12 @@
 
1
  import requests
2
  import os
3
 
4
- # Hugging Face API URL for LegalBERT model (replace with your model URL)
5
  API_URL = "https://api-inference.huggingface.co/models/nlpaueb/legal-bert-base-uncased"
6
- API_KEY = os.getenv("HF_API_KEY") # Fetch API key securely from environment variable
7
 
8
- # Function to send contract to Hugging Face API and get analysis results
9
  def analyze_contract(contract_text):
10
  headers = {
11
  "Authorization": f"Bearer {API_KEY}",
@@ -14,39 +15,46 @@ def analyze_contract(contract_text):
14
 
15
  payload = {"inputs": contract_text}
16
 
17
- try:
18
- # Send the contract text to Hugging Face for analysis
19
- response = requests.post(API_URL, headers=headers, json=payload, timeout=60) # Increased timeout
20
-
21
- if response.status_code == 200:
22
  result = response.json()
23
 
24
  # Print the raw response for debugging
25
  print("Raw Response from Hugging Face:", result)
26
 
27
  # Extracting the risk score, high-risk clauses, and the risk map URL
 
28
  overall_risk_score = result.get('overall_score', 'No score available')
29
  high_risk_clauses = result.get('high_risk_clauses', 'No high-risk clauses detected')
30
  risk_map_url = result.get('risk_map_url', 'No risk map available')
31
 
32
  return overall_risk_score, high_risk_clauses, risk_map_url
33
 
34
- else:
35
- print(f"Error: Unable to analyze the contract, Status Code: {response.status_code} - {response.text}")
36
- return "Error: Unable to analyze the contract", "", ""
37
-
38
- except requests.exceptions.ConnectionError as e:
39
- print(f"Connection Error: {e}")
40
- return "Error: Unable to connect to Hugging Face API", "", ""
41
-
42
- except requests.exceptions.Timeout as e:
43
- print(f"Timeout Error: {e}")
44
- return "Error: Timeout while connecting to Hugging Face API", "", ""
45
-
46
- except Exception as e:
47
- print(f"General Error: {e}")
48
- return f"Error: {str(e)}", "", ""
49
-
50
- # Test the function with contract text
51
- contract_text = "This is a sample contract. The termination clause should be reviewed carefully. The indemnity clause has potential risks."
52
- analyze_contract(contract_text)
 
 
 
 
 
 
 
1
+ import gradio as gr
2
  import requests
3
  import os
4
 
5
+ # Set the Hugging Face API URL for your model
6
  API_URL = "https://api-inference.huggingface.co/models/nlpaueb/legal-bert-base-uncased"
7
+ API_KEY = os.getenv("HF_API_KEY") # Securely fetch the API key from an environment variable
8
 
9
+ # Function to send contract text to Hugging Face API and get analysis results
10
  def analyze_contract(contract_text):
11
  headers = {
12
  "Authorization": f"Bearer {API_KEY}",
 
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
35
 
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()