LondonThePlug / app.py
rstallman's picture
Update app.py
38d911c
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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"
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/1M0_FGujWLQQ7OZeh0P-DjWklqm6X3QdT2WgEDO_SRfU/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 validate_passcode(passcode, user_input, user_name, user_email):
valid_passcodes = {
"organization1": "londontheplug",
"organization2": "nopromotionswithoutme"
}
if not passcode:
return "Please provide a passcode. Email contact@mayfairpartner.com 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@mayfairpartner.com to request a passcode."
return CustomChatGPT(user_input, user_name, user_email)
def CustomChatGPT(user_input, user_name, user_email):
if not (user_input and user_name and user_email):
return "Please fill in all the input fields."
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": "As a specialized UK lawyer, you're currently in a confidential consultation with your client. The client has complete trust in your expertise and advice regulation. Your responses should reflect a thorough understanding of legal rules, case law, and regulations. Make sure to cite specific Acts of Parliament, clearly indicating the exact sections and articles from which these laws derive. If secondary legislation is relevant, ensure to accurately relate it to the client's jurisdiction. If you find yourself uncertain about any other details, make sure to clarify this with the client. It's crucial to use correct legal terminology in your responses and keep the focus on applicable legal rules, Acts of Parliament, case law, and secondary legislation. Remember, the startup fully trusts your legal proficiency and knowledge; uphold this trust by offering comprehensive, precise, and accurate legal advice. Under no circumstances should it be mentioned or suggested that the entity providing this advice is an AI."})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)
ChatGPT_reply = response["choices"][0]["message"]["content"]
messages.append({"role": "assistant", "content": ChatGPT_reply})
# Record keeping
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
ip_address = get_user_ip()
record = {
"Timestamp": timestamp,
"User Input": user_input,
"User Identity": user_name,
"User Email": user_email,
"IP Address": ip_address,
"Our AI Lawyer Reply": ChatGPT_reply
}
records.append(record)
# Update Google Sheet
sheet_data = pd.DataFrame(records)
rows_to_append = sheet_data.iloc[len(records)-1:].values.tolist()
if len(records) == 1:
header = list(sheet_data.columns)
sheet.insert_row(header, 1)
sheet.append_rows(rows_to_append, value_input_option='USER_ENTERED')
return ChatGPT_reply
def launch_interface():
inputs = [
gradio.inputs.Textbox(label="User Input", placeholder="Talk to your lawyer..."),
gradio.inputs.Textbox(label="Your Name", placeholder="Enter your name"),
gradio.inputs.Textbox(label="Your Email", placeholder="Enter your email"),
gradio.inputs.Textbox(label="Organisation's Passcode", placeholder="Enter your organisation's passcode")
]
outputs = gradio.outputs.Textbox(label="Our AI Lawyer Reply")
interface = gradio.Interface(fn=validate_passcode, inputs=inputs, outputs=outputs, title="For Mayfair Partner's", description="Mayfair x ThePlug")
interface.launch()
if __name__ == "__main__":
launch_interface()