Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,34 @@
|
|
|
|
|
| 1 |
import requests
|
| 2 |
-
import
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
API_URL = "https://api-inference.huggingface.co/models/bert
|
| 6 |
-
API_KEY = "
|
| 7 |
|
| 8 |
-
# Function to send contract
|
| 9 |
def analyze_contract(contract_text):
|
| 10 |
headers = {
|
| 11 |
"Authorization": f"Bearer {API_KEY}",
|
| 12 |
"Content-Type": "application/json"
|
| 13 |
}
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 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 |
-
#
|
| 32 |
-
#
|
| 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 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|