CSAle commited on
Commit
251f923
·
1 Parent(s): 08cc6ec

Adding App

Browse files
Files changed (3) hide show
  1. .env-sample +1 -0
  2. app.py +107 -0
  3. requirements.txt +4 -0
.env-sample ADDED
@@ -0,0 +1 @@
 
 
1
+ ANTHROPIC_API_KEY=""
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.chat_models import ChatAnthropic
2
+ from langchain import PromptTemplate, LLMChain
3
+ from langchain.prompts.chat import (
4
+ ChatPromptTemplate,
5
+ SystemMessagePromptTemplate,
6
+ AIMessagePromptTemplate,
7
+ HumanMessagePromptTemplate,
8
+ )
9
+ from langchain.schema import AIMessage, HumanMessage, SystemMessage
10
+ import streamlit as st
11
+ from dotenv import load_dotenv
12
+ import PyPDF2
13
+
14
+ load_dotenv()
15
+
16
+
17
+ class LegalExpert:
18
+ def __init__(self):
19
+ self.system_prompt = self.get_system_prompt()
20
+
21
+ self.user_prompt = HumanMessagePromptTemplate.from_template("{legal_question}")
22
+
23
+ full_prompt_template = ChatPromptTemplate.from_messages(
24
+ [self.system_prompt, self.user_prompt]
25
+ )
26
+
27
+ self.chat = ChatAnthropic()
28
+
29
+ self.chain = LLMChain(llm=self.chat, prompt=full_prompt_template)
30
+
31
+ def get_system_prompt(self):
32
+ system_prompt = """
33
+ You are a Canadian Legal Expert.
34
+
35
+ Under no circumstances do you give legal advice.
36
+
37
+ You are adept at explaining the law in laymans terms, and you are able to provide context to legal questions.
38
+
39
+ While you can add context outside of the provided context, please do not add any information that is not directly relevant to the question, or the provided context.
40
+
41
+ You speak {language}.
42
+
43
+ ### CONTEXT
44
+ {context}
45
+
46
+ ### END OF CONTEXT
47
+ """
48
+
49
+ return SystemMessagePromptTemplate.from_template(system_prompt)
50
+
51
+ def run_chain(self, language, context, question):
52
+ return self.chain.run(
53
+ language=language, context=context, legal_question=question
54
+ )
55
+
56
+
57
+ def retrieve_pdf_text(pdf_file):
58
+ pdf_reader = PyPDF2.PdfReader(pdf_file)
59
+ text = ""
60
+ for page in pdf_reader.pages:
61
+ text += page.extract_text()
62
+ return text
63
+
64
+
65
+ # create a streamlit app
66
+ st.title("Canadian Legal Explainer (that does not give advice)")
67
+
68
+ if "LegalExpert" not in st.session_state:
69
+ st.session_state.LegalExpert = LegalExpert()
70
+
71
+ # create a upload file widget for a pdf
72
+ pdf_file = st.file_uploader("Upload a PDF file", type=["pdf"])
73
+
74
+ # if a pdf file is uploaded
75
+ if pdf_file:
76
+ # retrieve the text from the pdf
77
+ if "context" not in st.session_state:
78
+ st.session_state.context = retrieve_pdf_text(pdf_file)
79
+
80
+ # create a button that clears the context
81
+ if st.button("Clear context"):
82
+ st.session_state.__delitem__("context")
83
+ st.session_state.__delitem__("legal_response")
84
+
85
+ # if there's context, proceed
86
+ if "context" in st.session_state:
87
+ # create a dropdown widget for the language
88
+ language = st.selectbox("Language", ["English", "Français"])
89
+ # create a text input widget for a question
90
+ question = st.text_input("Ask a question")
91
+
92
+ # create a button to run the model
93
+ if st.button("Run"):
94
+ # run the model
95
+ legal_response = st.session_state.LegalExpert.run_chain(
96
+ language=language, context=st.session_state.context, question=question
97
+ )
98
+
99
+ if "legal_response" not in st.session_state:
100
+ st.session_state.legal_response = legal_response
101
+
102
+ else:
103
+ st.session_state.legal_response = legal_response
104
+
105
+ # display the response
106
+ if "legal_response" in st.session_state:
107
+ st.write(st.session_state.legal_response)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ langchain
3
+ openai
4
+ python-dotenv