Spaces:
Sleeping
Sleeping
Samarth Goel
commited on
Commit
•
aa1b98f
1
Parent(s):
b9fb8a5
check secrets access
Browse files- .gitignore +1 -0
- app.py +114 -2
- requirements.txt +28 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.venv
|
app.py
CHANGED
@@ -1,4 +1,116 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import openai
|
3 |
+
import anthropic
|
4 |
+
import fitz
|
5 |
|
6 |
+
# Set up API clients
|
7 |
+
openai_api_key = st.secrets["OPENAI_API_KEY"]
|
8 |
+
openai_client = openai.OpenAI(api_key=openai_api_key)
|
9 |
+
|
10 |
+
anthropic_api_key = st.secrets["ANTHROPIC_API_KEY"]
|
11 |
+
anthropic_client = anthropic.Anthropic(api_key=anthropic_api_key)
|
12 |
+
|
13 |
+
|
14 |
+
# Password protection
|
15 |
+
def check_password():
|
16 |
+
if "password_correct" not in st.session_state:
|
17 |
+
st.session_state["password_correct"] = False
|
18 |
+
|
19 |
+
if not st.session_state["password_correct"]:
|
20 |
+
password = st.text_input("Enter the password", type="password")
|
21 |
+
if st.button("Submit"):
|
22 |
+
if password == st.secrets["PASSWORD"]:
|
23 |
+
st.session_state["password_correct"] = True
|
24 |
+
st.rerun()
|
25 |
+
else:
|
26 |
+
st.error("😕 Password incorrect")
|
27 |
+
return False
|
28 |
+
else:
|
29 |
+
return True
|
30 |
+
|
31 |
+
|
32 |
+
def extract_text_from_pdf(file):
|
33 |
+
try:
|
34 |
+
with fitz.open(stream=file.read(), filetype="pdf") as doc:
|
35 |
+
text = ""
|
36 |
+
for page in doc:
|
37 |
+
text += page.get_text()
|
38 |
+
return text
|
39 |
+
except Exception as e:
|
40 |
+
st.error(f"Error parsing PDF: {str(e)}")
|
41 |
+
return None
|
42 |
+
|
43 |
+
|
44 |
+
def main():
|
45 |
+
st.title("Harvey Legal Research Take-Home")
|
46 |
+
|
47 |
+
if not check_password():
|
48 |
+
return
|
49 |
+
|
50 |
+
# Model selector
|
51 |
+
model = st.selectbox("Select Model", ["GPT-4o", "Claude 3.5 Sonnet"])
|
52 |
+
|
53 |
+
# Prompt input
|
54 |
+
prompt = st.text_area("Enter your prompt:", height=200)
|
55 |
+
|
56 |
+
# Document upload
|
57 |
+
uploaded_file = st.file_uploader("Upload a document", type=["txt", "pdf"])
|
58 |
+
document_text = None
|
59 |
+
if uploaded_file is not None:
|
60 |
+
if uploaded_file.type == "text/plain":
|
61 |
+
document_text = uploaded_file.getvalue().decode()
|
62 |
+
elif uploaded_file.type == "application/pdf":
|
63 |
+
document_text = extract_text_from_pdf(uploaded_file)
|
64 |
+
else:
|
65 |
+
st.error("Unsupported file type. Please upload a text or PDF file.")
|
66 |
+
|
67 |
+
# Run button
|
68 |
+
if st.button("Run"):
|
69 |
+
if not prompt:
|
70 |
+
st.warning("Please enter a prompt.")
|
71 |
+
return
|
72 |
+
|
73 |
+
if not document_text:
|
74 |
+
st.warning("Please upload a document.")
|
75 |
+
return
|
76 |
+
|
77 |
+
# full_prompt = f"Document content:\n\n{document_text}\n\nPrompt:\n\n{prompt}"
|
78 |
+
full_prompt = prompt
|
79 |
+
|
80 |
+
if model == "GPT-4o":
|
81 |
+
with st.spinner("Processing with GPT-4o..."):
|
82 |
+
stream = openai_client.chat.completions.create(
|
83 |
+
model="gpt-4o",
|
84 |
+
messages=[{"role": "user", "content": full_prompt}],
|
85 |
+
stream=True,
|
86 |
+
)
|
87 |
+
|
88 |
+
full_response = ""
|
89 |
+
response_area = st.empty()
|
90 |
+
|
91 |
+
for chunk in stream:
|
92 |
+
if chunk.choices[0].delta.content is not None:
|
93 |
+
full_response += chunk.choices[0].delta.content
|
94 |
+
response_area.text_area(
|
95 |
+
"Response:", value=full_response, height=300
|
96 |
+
)
|
97 |
+
|
98 |
+
elif model == "Claude 3.5 Sonnet":
|
99 |
+
with st.spinner("Processing with Claude 3.5 Sonnet..."):
|
100 |
+
with anthropic_client.messages.stream(
|
101 |
+
model="claude-3-5-sonnet-20240620",
|
102 |
+
max_tokens=8192,
|
103 |
+
messages=[{"role": "user", "content": full_prompt}],
|
104 |
+
) as stream:
|
105 |
+
full_response = ""
|
106 |
+
response_area = st.empty()
|
107 |
+
|
108 |
+
for text in stream.text_stream:
|
109 |
+
full_response += text
|
110 |
+
response_area.text_area(
|
111 |
+
"Response:", value=full_response, height=300
|
112 |
+
)
|
113 |
+
|
114 |
+
|
115 |
+
if __name__ == "__main__":
|
116 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
annotated-types==0.7.0
|
2 |
+
anthropic==0.34.2
|
3 |
+
anyio==4.4.0
|
4 |
+
certifi==2024.8.30
|
5 |
+
charset-normalizer==3.3.2
|
6 |
+
distro==1.9.0
|
7 |
+
exceptiongroup==1.2.2
|
8 |
+
filelock==3.16.1
|
9 |
+
fsspec==2024.9.0
|
10 |
+
h11==0.14.0
|
11 |
+
httpcore==1.0.5
|
12 |
+
httpx==0.27.2
|
13 |
+
huggingface-hub==0.25.0
|
14 |
+
idna==3.10
|
15 |
+
jiter==0.5.0
|
16 |
+
openai==1.46.0
|
17 |
+
packaging==24.1
|
18 |
+
pydantic==2.9.2
|
19 |
+
pydantic_core==2.23.4
|
20 |
+
PyMuPDF==1.24.10
|
21 |
+
PyMuPDFb==1.24.10
|
22 |
+
PyYAML==6.0.2
|
23 |
+
requests==2.32.3
|
24 |
+
sniffio==1.3.1
|
25 |
+
tokenizers==0.20.0
|
26 |
+
tqdm==4.66.5
|
27 |
+
typing_extensions==4.12.2
|
28 |
+
urllib3==2.2.3
|