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