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