Spaces:
Runtime error
Runtime error
| import openai | |
| import gradio | |
| import pandas as pd | |
| from datetime import datetime | |
| import gspread | |
| from google.oauth2.service_account import Credentials | |
| import requests | |
| import json | |
| openai.api_key = "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" | |
| # Global variables | |
| records = [] | |
| credentials = Credentials.from_service_account_file("credentials.json", scopes=["https://www.googleapis.com/auth/spreadsheets"]) | |
| client = gspread.authorize(credentials) | |
| sheet = client.open_by_url("https://docs.google.com/spreadsheets/d/1aZibKvwrvOB-xx_PSp2YFyuaycHyVkJZW_unC21VUbA/edit?usp=sharing").sheet1 | |
| def get_user_ip(): | |
| try: | |
| response = requests.get("https://api.ipify.org?format=json") | |
| data = json.loads(response.text) | |
| return data["ip"] | |
| except: | |
| return None | |
| def ContractDraftGPT(passcode, user_input, user_name, user_email, is_fintech_startup, region, profession): | |
| if not (user_input and user_name and user_email and is_fintech_startup and region and profession): | |
| return "Please fill in all the input fields." | |
| ip_address = get_user_ip() | |
| # Check if passcode is required based on IP address usage count | |
| if ip_address and any(record["IP Address"] == ip_address for record in records): | |
| usage_count = sum(record["IP Address"] == ip_address for record in records) | |
| if usage_count > 3 and not passcode: | |
| return "A passcode is required for subsequent uses. Email contact@westminster.ai to request a passcode." | |
| messages = [] | |
| if not user_name: | |
| return "Please enter your name." | |
| user_message = f"{user_input} [USER_IDENTITY: {user_name}]" | |
| messages.append({"role": "user", "content": user_message}) | |
| messages.append({"role": "system", "content": "You are a professional and experienced UK Lawyer who is drafting a legal document, a contract for your client based on his requirements. Make sure to mention and point precise legal rules, Acts of Parliament (please insert which section of which article of which law, be precise when you refer to Acts of Parliament), case law, and any pieces of secondary legislation. UK legislation."}) | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", | |
| messages=messages | |
| ) | |
| ContractGPT_reply = response["choices"][0]["message"]["content"].strip() | |
| messages.append({"role": "assistant", "content": ContractGPT_reply}) | |
| timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
| record = { | |
| "Passcode": passcode, | |
| "Timestamp": timestamp, | |
| "User Input": user_input, | |
| "User Identity": user_name, | |
| "User Email": user_email, | |
| "IP Address": ip_address, | |
| "Region": region, | |
| "Profession": profession, | |
| "Fintech": "Yes" if is_fintech_startup == "Yes" else "No", | |
| "Contract Draft": ContractGPT_reply | |
| } | |
| records.append(record) | |
| sheet_data = pd.DataFrame(records) | |
| rows_to_append = sheet_data.iloc[len(records) - 1:][["Passcode", "Timestamp", "User Input", "User Identity", "User Email", "IP Address", "Region", "Profession", "Fintech", "Contract Draft"]].values.tolist() | |
| if len(records) == 1: | |
| header = ["Passcode", "Timestamp", "User Input", "User Identity", "User Email", "IP Address", "Region", "Profession", "Fintech", "Contract Draft"] | |
| sheet.insert_row(header, 1) | |
| sheet.append_rows(rows_to_append, value_input_option='USER_ENTERED') | |
| return ContractGPT_reply | |
| def launch_interface(): | |
| inputs = [ | |
| gradio.inputs.Textbox(label="Organisation's Passcode (Optional)", placeholder="Enter your organisation's passcode"), | |
| gradio.inputs.Textbox(label="Your Contract Draft Request", placeholder="Provide details for our AI lawyer to draft your contract..."), | |
| gradio.inputs.Textbox(label="Your Name", placeholder="Enter your name"), | |
| gradio.inputs.Textbox(label="Your Email", placeholder="Enter your email"), | |
| gradio.inputs.Radio(label="Are you a fintech startup?", choices=["Yes", "No"]), | |
| gradio.inputs.Dropdown(label="Select your region:", choices=["England", "Scotland", "Wales", "Northern Ireland"]), | |
| gradio.inputs.Textbox(label="Profession", placeholder="Enter your profession") | |
| ] | |
| outputs = gradio.outputs.Textbox(label="Contract Draft") | |
| def validate_passcode(passcode, user_input, user_name, user_email, is_fintech_startup, region, profession): | |
| valid_passcodes = { | |
| "organization1": "risebybarclays", | |
| "organization2": "launchlabrocks", | |
| "organization3": "fintechalliance", | |
| "organization4": "cisi-fintech", | |
| "organization5": "city-bayes-alumni", | |
| "organization6": "bar-council", | |
| "organization7": "vcinnovations", | |
| "organization8": "remi-slama", | |
| "organization9": "dalton-latymer", | |
| "organization10": "barrister", | |
| "organization11": "r-muttukrishnan", | |
| "organization12": "zhero" | |
| } | |
| if not passcode: | |
| return "Please provide a passcode. Email contact@westminster.ai to request a passcode." | |
| passcode = passcode.lower() # Convert the passcode to lowercase for case-insensitive comparison | |
| if passcode not in valid_passcodes.values(): | |
| return "Incorrect passcode. Access denied. Email contact@westminster.ai to request a passcode." | |
| return ContractDraftGPT(passcode, user_input, user_name, user_email, is_fintech_startup, region, profession) | |
| interface = gradio.Interface(fn=validate_passcode, inputs=inputs, outputs=outputs, title="", description="") | |
| interface.launch() | |
| if __name__ == "__main__": | |
| launch_interface() | |