Spaces:
Sleeping
Sleeping
Commit
·
6925a37
1
Parent(s):
f30b474
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import streamlit as st
|
3 |
+
import os
|
4 |
+
import time
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
load_dotenv()
|
7 |
+
st.set_page_config(page_title="Pak Case Law ChatBot")
|
8 |
+
st.header("Welcome To Pakistani AI Lawyer!")
|
9 |
+
st.markdown("Currently the ChatBot is taking content only from this [file.](http://kpcode.kp.gov.pk/uploads/1964_35_THE_WEST_PAKISTAN_FAMILY_COURTS_ACT_1964.pdf)")
|
10 |
+
|
11 |
+
class OpenAIClient:
|
12 |
+
def __init__(self):
|
13 |
+
self.client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
14 |
+
self.thread_id = None
|
15 |
+
self.file_id = ["file-Anvyhz2k0puV0NCjWH8mGsuk"]
|
16 |
+
self.assistant_id = "asst_Z7pj5QGUjiAlVpJzqsX2SJkg"
|
17 |
+
self.run_id = "run_A5wq14ZMQXBpaDnULFPky5Bb"
|
18 |
+
|
19 |
+
def create_thread_id(self):
|
20 |
+
if "thread_id" not in st.session_state:
|
21 |
+
_id = self.client.beta.threads.create().id
|
22 |
+
st.session_state["thread_id"] = _id
|
23 |
+
|
24 |
+
def main(self, user_message):
|
25 |
+
message = self.client.beta.threads.messages.create(
|
26 |
+
st.session_state.get("thread_id"),
|
27 |
+
role="user",
|
28 |
+
content=user_message,
|
29 |
+
)
|
30 |
+
_run = self.client.beta.threads.runs.create(thread_id=st.session_state.get("thread_id"), assistant_id=self.assistant_id)
|
31 |
+
while True:
|
32 |
+
run = self.client.beta.threads.runs.retrieve(thread_id=st.session_state.get("thread_id"),
|
33 |
+
run_id=_run.id).status
|
34 |
+
|
35 |
+
time.sleep(3)
|
36 |
+
if run == "completed":
|
37 |
+
return self.get_response()
|
38 |
+
elif run == "queued":
|
39 |
+
continue
|
40 |
+
elif run == "failed" or run == "cancelled" or run == "expired":
|
41 |
+
return run
|
42 |
+
elif run == "in_progress":
|
43 |
+
continue
|
44 |
+
else:
|
45 |
+
break
|
46 |
+
|
47 |
+
def get_response(self):
|
48 |
+
messages = self.client.beta.threads.messages.list(
|
49 |
+
thread_id=st.session_state.get("thread_id")
|
50 |
+
)
|
51 |
+
return messages.data[0].content[0].text.value
|
52 |
+
|
53 |
+
try:
|
54 |
+
client = OpenAIClient()
|
55 |
+
client.create_thread_id()
|
56 |
+
user_input = st.text_input("Input: ", key='input')
|
57 |
+
submit = st.button("Ask the Question")
|
58 |
+
if submit:
|
59 |
+
st.subheader("The Response is")
|
60 |
+
st.write(client.main(user_input))
|
61 |
+
except openai.AuthenticationError:
|
62 |
+
st.write("Your API key or token was invalid, expired, or revoked.")
|
63 |
+
except openai.RateLimitError:
|
64 |
+
st.write("You have hit your assigned rate limit. Please try again in a few minutes.")
|
65 |
+
except Exception as e:
|
66 |
+
st.write(e)
|