Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +108 -0
- credentials.json +13 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import gradio
|
3 |
+
import pandas as pd
|
4 |
+
from datetime import datetime
|
5 |
+
import gspread
|
6 |
+
from google.oauth2.service_account import Credentials
|
7 |
+
import requests
|
8 |
+
import json
|
9 |
+
|
10 |
+
openai.api_key = "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"
|
11 |
+
|
12 |
+
records = []
|
13 |
+
credentials = Credentials.from_service_account_file("credentials.json", scopes=["https://www.googleapis.com/auth/spreadsheets"])
|
14 |
+
client = gspread.authorize(credentials)
|
15 |
+
sheet = client.open_by_url("https://docs.google.com/spreadsheets/d/1KsEoO3xVq4rkPk2T6XqPmOZ0mozlJ-cuS-Lg7SxTGjg/edit?usp=sharing").sheet1
|
16 |
+
|
17 |
+
def get_user_ip():
|
18 |
+
try:
|
19 |
+
response = requests.get("https://api.ipify.org?format=json")
|
20 |
+
data = json.loads(response.text)
|
21 |
+
return data["ip"]
|
22 |
+
except:
|
23 |
+
return None
|
24 |
+
|
25 |
+
def CustomChatGPT(user_input, user_name, user_email, user_location, user_agent):
|
26 |
+
messages = []
|
27 |
+
|
28 |
+
if not user_name:
|
29 |
+
return "Please enter your name."
|
30 |
+
|
31 |
+
user_message = f"{user_input} [USER_IDENTITY: {user_name}]"
|
32 |
+
messages.append({"role": "user", "content": user_message})
|
33 |
+
messages.append({"role": "system", "content": "As a specialized US attorney, you're currently in a confidential consultation with your client. The client has complete trust in your expertise and advice on regulations, as an attorney would do. 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 client 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."})
|
34 |
+
response = openai.ChatCompletion.create(
|
35 |
+
model="gpt-3.5-turbo",
|
36 |
+
messages=messages
|
37 |
+
)
|
38 |
+
|
39 |
+
ChatGPT_reply = response["choices"][0]["message"]["content"]
|
40 |
+
messages.append({"role": "assistant", "content": ChatGPT_reply})
|
41 |
+
|
42 |
+
# Record keeping
|
43 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
44 |
+
ip_address = get_user_ip()
|
45 |
+
device_type = get_device_type(user_agent)
|
46 |
+
session_duration = "" # Calculate session duration
|
47 |
+
features_used = "" # Track features used
|
48 |
+
num_queries = "" # Count number of queries
|
49 |
+
num_documents_created = "" # Count number of documents created
|
50 |
+
clickstream_data = "" # Track clickstream data
|
51 |
+
interaction_patterns = "" # Track user interface interaction patterns
|
52 |
+
success_of_advice = "" # Measure success of advice
|
53 |
+
user_satisfaction_rate = "" # Measure user satisfaction rate
|
54 |
+
industry_or_profession = "" # Capture user industry or profession
|
55 |
+
|
56 |
+
record = {
|
57 |
+
"Timestamp": timestamp,
|
58 |
+
"User Input": user_input,
|
59 |
+
"User Identity": user_name,
|
60 |
+
"User Email": user_email,
|
61 |
+
"IP Address": ip_address,
|
62 |
+
"Device Type": device_type,
|
63 |
+
"Session Duration": session_duration,
|
64 |
+
"Features Used": features_used,
|
65 |
+
"Number of Queries": num_queries,
|
66 |
+
"Number of Documents Created": num_documents_created,
|
67 |
+
"Clickstream Data": clickstream_data,
|
68 |
+
"Interaction Patterns": interaction_patterns,
|
69 |
+
"Success of Advice": success_of_advice,
|
70 |
+
"User Satisfaction Rate": user_satisfaction_rate,
|
71 |
+
"User Location": user_location,
|
72 |
+
"Industry or Profession": industry_or_profession,
|
73 |
+
"Our AI attorney Reply": ChatGPT_reply
|
74 |
+
}
|
75 |
+
records.append(record)
|
76 |
+
|
77 |
+
# Update Google Sheet
|
78 |
+
row_values = [
|
79 |
+
timestamp, user_input, user_name, user_email, ip_address, device_type, session_duration,
|
80 |
+
features_used, num_queries, num_documents_created, clickstream_data, interaction_patterns,
|
81 |
+
success_of_advice, user_satisfaction_rate, user_location, industry_or_profession, ChatGPT_reply
|
82 |
+
]
|
83 |
+
sheet.append_row(row_values)
|
84 |
+
|
85 |
+
return ChatGPT_reply
|
86 |
+
|
87 |
+
def get_device_type(user_agent):
|
88 |
+
if user_agent and "mobile" in user_agent.lower():
|
89 |
+
return "Mobile"
|
90 |
+
elif user_agent and "tablet" in user_agent.lower():
|
91 |
+
return "Tablet"
|
92 |
+
else:
|
93 |
+
return "Desktop"
|
94 |
+
|
95 |
+
def launch_interface():
|
96 |
+
inputs = [
|
97 |
+
gradio.inputs.Textbox(label="User Input", placeholder="Talk to your attorney..."),
|
98 |
+
gradio.inputs.Textbox(label="Your Name", placeholder="Enter your name"),
|
99 |
+
gradio.inputs.Textbox(label="Your Email", placeholder="Enter your email"),
|
100 |
+
gradio.inputs.Dropdown(label="Your Location (US State)", choices=["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"])
|
101 |
+
]
|
102 |
+
outputs = gradio.outputs.Textbox(label="Our AI attorney Reply")
|
103 |
+
|
104 |
+
interface = gradio.Interface(fn=CustomChatGPT, inputs=inputs, outputs=outputs, title="", description="")
|
105 |
+
interface.launch()
|
106 |
+
|
107 |
+
if __name__ == "__main__":
|
108 |
+
launch_interface()
|
credentials.json
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"type": "service_account",
|
3 |
+
"project_id": "ai-barrister",
|
4 |
+
"private_key_id": "79eb2f390afee34c1c51ea4ba945e4878c4e0794",
|
5 |
+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC4THaTlZt9UIa7\nLS2RzgFxU4POVFn0GFimDUEgQ+FKMh8+A6wEhFmzWYQyj6MqKYCobcFxCBXnh6s1\nLTYJHYJVAPSEYZE25tQTxONwEyObgltCKKif2OkHwCZLCS6lpH59WSMcEORJhFuS\nlxVN8ZGwdj1KD2gZkwEJMpn7RjVKTGt9Wl6S9NZ3WI3TWj9pzQXlrPp2c9pVgEfK\nOCsYi1uWGM3Z2x9zL0f8ekHikFTmqGyTIcOhyyNMOmbkrTKla9GK/Lr7TIzf4Lr9\nCpnbF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Rgfy3vNWZ5Aw7Hw/nhH7Xk+Q1vZVQTz1+0WbH4QBiW5YXDHn\noufb4AVPYuQXxvukTlm8kDLmX1fy0pAfpO1FMmc/AoGBAKQnhG8ym7rFLN1ebXG8\ng2gB02rEWRnBCXdKKalG33n7zeaF+93atUg4pJ0hofP7g/0yT31BTFh6R2Nt5uFf\nGccChHFSkpIQnNOLhQHisglfMttQP1KJ1WjfaRuQ+hqp7eGA6II0lFji5sgyBYoT\nbNrpwduYkIPY15gzHtMstVDC\n-----END PRIVATE KEY-----\n",
|
6 |
+
"client_email": "ai-barrister-log@ai-barrister.iam.gserviceaccount.com",
|
7 |
+
"client_id": "103630435709161336679",
|
8 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
9 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
10 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
11 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/ai-barrister-log%40ai-barrister.iam.gserviceaccount.com",
|
12 |
+
"universe_domain": "googleapis.com"
|
13 |
+
}
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
gradio
|
3 |
+
pandas
|
4 |
+
gspread
|
5 |
+
oauth2client
|