File size: 3,821 Bytes
179252b
 
 
 
 
 
 
 
 
 
a36b97a
7018776
179252b
 
 
 
 
 
 
 
 
 
 
 
 
955c09c
179252b
 
a36b97a
 
86e20ec
179252b
 
7018776
179252b
 
 
 
 
83ffbb5
2eac875
179252b
 
a36b97a
179252b
 
 
 
 
 
 
 
 
 
 
955c09c
7018776
179252b
 
 
 
a36b97a
7018776
 
 
 
 
 
179252b
 
 
 
 
 
 
 
 
 
 
 
7018776
83ffbb5
 
 
 
 
 
 
 
7018776
 
 
 
9173059
179252b
734ed8a
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
95
96
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,
        "Contract Draft": ContractGPT_reply
    }
    records.append(record)

    # Update Google Sheet
    df = pd.DataFrame(records)
    df.to_csv("contracts.csv", index=False)

    # Clear the sheet and append the updated data
    sheet.clear()
    sheet.insert_rows(df.values.tolist(), row=1)

    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()