File size: 3,812 Bytes
60f8f6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f324cd7
60f8f6d
 
 
 
 
 
 
f324cd7
60f8f6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8542db
60f8f6d
 
 
 
 
7b7383f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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(user_input, user_name, user_email, user_agent, is_fintech_startup, region, profession):
    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})

    # Record keeping
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    ip_address = get_user_ip()
    device_type = get_device_type(user_agent)

    record = {
        "Timestamp": timestamp,
        "User Input": user_input,
        "User Identity": user_name,
        "User Email": user_email,
        "IP Address": ip_address,
        "Device Type": device_type,
        "Region": region,
        "Profession": profession,
        "Fintech": "Yes" if is_fintech_startup == "Yes" else "No",
        "Contract Draft": ContractGPT_reply
    }
    records.append(record)

    # Update Google Sheet
    df = pd.DataFrame(records)
    sheet.clear()
    sheet.update([df.columns.values.tolist()] + df.values.tolist())

    return ContractGPT_reply

def get_device_type(user_agent):
    if user_agent and "mobile" in user_agent.lower():
        return "Mobile"
    elif user_agent and "tablet" in user_agent.lower():
        return "Tablet"
    else:
        return "Desktop"

def launch_interface():
    inputs = [
        gradio.inputs.Textbox(label="User Input", placeholder="Provide details for contract draft..."),
        gradio.inputs.Textbox(label="Your Name", placeholder="Enter your name"),
        gradio.inputs.Textbox(label="Your Email", placeholder="Enter your email"),
        gradio.inputs.Textbox(label="User Agent", placeholder="Enter your user agent"),
        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")

    interface = gradio.Interface(fn=ContractDraftGPT, inputs=inputs, outputs=outputs, title="Contract Drafting GPT", description="Generate contract drafts using OpenAI's GPT-3.5 model.")
    interface.launch()

if __name__ == "__main__":
    launch_interface()