Spaces:
Sleeping
Sleeping
Commit
·
5f17ff4
1
Parent(s):
e21050f
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,93 @@
|
|
1 |
-
|
2 |
-
import os
|
3 |
-
os.environ["HUGGINGFACEHUB_API_TOKEN"]="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"
|
4 |
-
os.environ["OPENAI_API_KEY"]="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"
|
5 |
-
|
6 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
st.subheader("The Response is")
|
21 |
-
st.write(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
|
|
|
|
|
|
|
|
2 |
import streamlit as st
|
3 |
+
import os,time
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
load_dotenv()
|
6 |
+
st.set_page_config(page_title="Pak Case Law ChatBot")
|
7 |
+
st.header("Wellcome To Pakistani AI Lawyer!")
|
8 |
|
9 |
+
class OpenAIClient:
|
10 |
+
def __init__(self):
|
11 |
+
self.client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
12 |
+
self.thread_id = None
|
13 |
+
self.file_id = ["file-CwRdM2JgGKEVZP4rhoKSvgpD"]
|
14 |
+
self.thread = self.client.beta.threads.create()
|
15 |
+
|
16 |
+
def upload_file(self):
|
17 |
+
file=self.client.files.create(
|
18 |
+
file=open("1964_35_THE_WEST_PAKISTAN_FAMILY_COURTS_ACT_1964.pdf", "rb"),
|
19 |
+
purpose="assistants")
|
20 |
+
self.file_id.append(file.id)
|
21 |
+
print(file.id)
|
22 |
+
print("File Uploaded")
|
23 |
+
|
24 |
+
def main(self,user_message):
|
25 |
+
lawyer = self.client.beta.assistants.create(
|
26 |
+
instructions="""You are an Pakistani case law provider, and you have access to files to answer client questions about Pakistan case laws. Always response with info from either of the files.\
|
27 |
+
Please always make sure to follow these instructions that are delimited in triple backticks:\
|
28 |
+
```
|
29 |
+
1 - Behave like a Pakistani lawyer.\
|
30 |
+
2 - Always remember that you are also suitable for specific legal issues.\
|
31 |
+
3 - Providing helpful information, drafting documents, reviewing documents, or completing tasks based on user input.
|
32 |
+
""",
|
33 |
+
name="Pakistani Lawyer",
|
34 |
+
tools=[{"type": "retrieval"}],
|
35 |
+
model="gpt-3.5-turbo-1106",
|
36 |
+
file_ids=self.file_id)
|
37 |
|
38 |
+
message = self.client.beta.threads.messages.create(
|
39 |
+
thread_id= self.thread.id,
|
40 |
+
role="user",
|
41 |
+
content=user_message,
|
42 |
+
file_ids= self.file_id
|
43 |
+
)
|
44 |
+
_run = self.client.beta.threads.runs.create(
|
45 |
+
thread_id= self.thread.id,
|
46 |
+
assistant_id= lawyer.id
|
47 |
+
)
|
48 |
|
49 |
+
while True:
|
50 |
+
run = self.client.beta.threads.runs.retrieve(
|
51 |
+
thread_id=self.thread.id,
|
52 |
+
run_id=_run.id
|
53 |
+
).status
|
54 |
+
|
55 |
+
time.sleep(3)
|
56 |
+
if run == "completed":
|
57 |
+
print(run)
|
58 |
+
return self.get_response()
|
59 |
+
elif run == "queued":
|
60 |
+
continue
|
61 |
+
elif run == "failed" or run == "cancelled" or run == "expired":
|
62 |
+
print(run)
|
63 |
+
break
|
64 |
+
elif run == "in_progress":
|
65 |
+
continue
|
66 |
+
else:
|
67 |
+
break
|
68 |
+
|
69 |
+
def get_response(self):
|
70 |
+
messages = self.client.beta.threads.messages.list(
|
71 |
+
thread_id= self.thread.id
|
72 |
+
)
|
73 |
+
return messages.data[0].content[0].text.value
|
74 |
+
|
75 |
+
def file_handler(self):
|
76 |
+
return self.client.files.retrieve("file-CwRdM2JgGKEVZP4rhoKSvgpD")
|
77 |
+
def delete_file(self):
|
78 |
+
return self.client.files.list()
|
79 |
+
|
80 |
+
try:
|
81 |
+
client = OpenAIClient()
|
82 |
+
input = st.text_input("Input: ",key='input')
|
83 |
+
response=client.main(input)
|
84 |
+
submit = st.button("Ask the Question")
|
85 |
+
if submit:
|
86 |
st.subheader("The Response is")
|
87 |
+
st.write(response)
|
88 |
+
except openai.AuthenticationError:
|
89 |
+
st.write("Your API key or token was invalid, expired, or revoked.")
|
90 |
+
except openai.RateLimitError:
|
91 |
+
st.write("You have hit your assigned rate limit. Please try again in a few minutes.")
|
92 |
+
except Exception as e:
|
93 |
+
st.write(e)
|