Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,37 +7,73 @@ import json
|
|
7 |
import mistune
|
8 |
import pytz
|
9 |
import math
|
|
|
|
|
10 |
from datetime import datetime
|
11 |
from openai import ChatCompletion
|
12 |
from xml.etree import ElementTree as ET
|
13 |
from bs4 import BeautifulSoup
|
14 |
from collections import deque
|
|
|
15 |
|
16 |
openai.api_key = os.getenv('OPENAI_KEY')
|
17 |
-
st.set_page_config(
|
18 |
-
page_title="GPT Streamlit Document Reasoner",
|
19 |
-
layout="wide")
|
20 |
|
21 |
menu = ["txt", "htm", "md", "py"]
|
22 |
-
choice = st.sidebar.selectbox("Output
|
23 |
-
|
24 |
-
if choice == "txt":
|
25 |
-
st.sidebar.write(choicePrefix + "Text File.")
|
26 |
-
elif choice == "htm":
|
27 |
-
st.sidebar.write(choicePrefix + "HTML5.")
|
28 |
-
elif choice == "md":
|
29 |
-
st.sidebar.write(choicePrefix + "Markdown.")
|
30 |
-
elif choice == "py":
|
31 |
-
st.sidebar.write(choicePrefix + "Python Code.")
|
32 |
-
|
33 |
-
max_length = st.sidebar.slider("Max document length", min_value=1000, max_value=32000, value=2000, step=1000)
|
34 |
|
35 |
def generate_filename(prompt, file_type):
|
36 |
central = pytz.timezone('US/Central')
|
37 |
safe_date_time = datetime.now(central).strftime("%m%d_%I%M")
|
38 |
-
safe_prompt = "".join(x for x in prompt if x.isalnum())[:
|
39 |
return f"{safe_date_time}_{safe_prompt}.{file_type}"
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
def create_file(filename, prompt, response):
|
42 |
if filename.endswith(".txt"):
|
43 |
with open(filename, 'w') as file:
|
@@ -55,15 +91,6 @@ def truncate_document(document, length):
|
|
55 |
def divide_document(document, max_length):
|
56 |
return [document[i:i+max_length] for i in range(0, len(document), max_length)]
|
57 |
|
58 |
-
def chat_with_model(prompt, document_section):
|
59 |
-
model = "gpt-3.5-turbo"
|
60 |
-
conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
|
61 |
-
conversation.append({'role': 'user', 'content': prompt})
|
62 |
-
conversation.append({'role': 'assistant', 'content': document_section})
|
63 |
-
response = openai.ChatCompletion.create(model=model, messages=conversation)
|
64 |
-
return response['choices'][0]['message']['content']
|
65 |
-
|
66 |
-
|
67 |
def get_table_download_link(file_path):
|
68 |
with open(file_path, 'r') as file:
|
69 |
data = file.read()
|
@@ -81,7 +108,6 @@ def get_table_download_link(file_path):
|
|
81 |
href = f'<a href="data:{mime_type};base64,{b64}" target="_blank" download="{file_name}">{file_name}</a>'
|
82 |
return href
|
83 |
|
84 |
-
|
85 |
def CompressXML(xml_text):
|
86 |
root = ET.fromstring(xml_text)
|
87 |
for elem in list(root.iter()):
|
@@ -111,10 +137,15 @@ def read_file_content(file,max_length):
|
|
111 |
return ""
|
112 |
|
113 |
def main():
|
114 |
-
user_prompt = st.text_area("
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
118 |
document_sections = deque()
|
119 |
document_responses = {}
|
120 |
|
@@ -123,24 +154,29 @@ def main():
|
|
123 |
document_sections.extend(divide_document(file_content, max_length))
|
124 |
|
125 |
if len(document_sections) > 0:
|
126 |
-
|
127 |
-
|
128 |
-
st.markdown(
|
129 |
-
|
|
|
|
|
130 |
st.markdown("**Chat with the model:**")
|
131 |
for i, section in enumerate(list(document_sections)):
|
132 |
if i in document_responses:
|
133 |
st.markdown(f"**Section {i+1}**\n{document_responses[i]}")
|
134 |
else:
|
135 |
if st.button(f"Chat about Section {i+1}"):
|
136 |
-
st.write('
|
137 |
response = chat_with_model(user_prompt, section)
|
138 |
st.write('Response:')
|
139 |
st.write(response)
|
140 |
document_responses[i] = response
|
|
|
|
|
|
|
141 |
|
142 |
if st.button('π¬ Chat'):
|
143 |
-
st.write('
|
144 |
response = chat_with_model(user_prompt, ''.join(list(document_sections)))
|
145 |
st.write('Response:')
|
146 |
st.write(response)
|
@@ -149,15 +185,19 @@ def main():
|
|
149 |
create_file(filename, user_prompt, response)
|
150 |
st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
151 |
|
152 |
-
all_files = glob.glob("
|
|
|
|
|
|
|
153 |
for file in all_files:
|
154 |
-
col1,
|
155 |
with col1:
|
156 |
st.markdown(get_table_download_link(file), unsafe_allow_html=True)
|
157 |
-
with
|
158 |
-
if st.button("π", key=file):
|
159 |
os.remove(file)
|
160 |
st.experimental_rerun()
|
161 |
-
|
162 |
if __name__ == "__main__":
|
163 |
main()
|
|
|
|
7 |
import mistune
|
8 |
import pytz
|
9 |
import math
|
10 |
+
import requests
|
11 |
+
|
12 |
from datetime import datetime
|
13 |
from openai import ChatCompletion
|
14 |
from xml.etree import ElementTree as ET
|
15 |
from bs4 import BeautifulSoup
|
16 |
from collections import deque
|
17 |
+
from audio_recorder_streamlit import audio_recorder
|
18 |
|
19 |
openai.api_key = os.getenv('OPENAI_KEY')
|
20 |
+
st.set_page_config(page_title="GPT Streamlit Document Reasoner",layout="wide")
|
|
|
|
|
21 |
|
22 |
menu = ["txt", "htm", "md", "py"]
|
23 |
+
choice = st.sidebar.selectbox("Output File Type:", menu)
|
24 |
+
model_choice = st.sidebar.radio("Select Model:", ('gpt-3.5-turbo', 'gpt-3.5-turbo-0301'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
def generate_filename(prompt, file_type):
|
27 |
central = pytz.timezone('US/Central')
|
28 |
safe_date_time = datetime.now(central).strftime("%m%d_%I%M")
|
29 |
+
safe_prompt = "".join(x for x in prompt if x.isalnum())[:45]
|
30 |
return f"{safe_date_time}_{safe_prompt}.{file_type}"
|
31 |
|
32 |
+
def chat_with_model(prompt, document_section):
|
33 |
+
model = model_choice
|
34 |
+
conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
|
35 |
+
conversation.append({'role': 'user', 'content': prompt})
|
36 |
+
conversation.append({'role': 'assistant', 'content': document_section})
|
37 |
+
response = openai.ChatCompletion.create(model=model, messages=conversation)
|
38 |
+
return response['choices'][0]['message']['content']
|
39 |
+
|
40 |
+
def transcribe_audio(openai_key, file_path, model):
|
41 |
+
OPENAI_API_URL = "https://api.openai.com/v1/audio/transcriptions"
|
42 |
+
headers = {
|
43 |
+
"Authorization": f"Bearer {openai_key}",
|
44 |
+
}
|
45 |
+
with open(file_path, 'rb') as f:
|
46 |
+
data = {'file': f}
|
47 |
+
response = requests.post(OPENAI_API_URL, headers=headers, files=data, data={'model': model})
|
48 |
+
if response.status_code == 200:
|
49 |
+
st.write(response.json())
|
50 |
+
response2 = chat_with_model(response.json().get('text'), '')
|
51 |
+
st.write('Responses:')
|
52 |
+
#st.write(response)
|
53 |
+
st.write(response2)
|
54 |
+
return response.json().get('text')
|
55 |
+
else:
|
56 |
+
st.write(response.json())
|
57 |
+
st.error("Error in API call.")
|
58 |
+
return None
|
59 |
+
|
60 |
+
def save_and_play_audio(audio_recorder):
|
61 |
+
audio_bytes = audio_recorder()
|
62 |
+
if audio_bytes:
|
63 |
+
filename = generate_filename("Recording", "wav")
|
64 |
+
with open(filename, 'wb') as f:
|
65 |
+
f.write(audio_bytes)
|
66 |
+
st.audio(audio_bytes, format="audio/wav")
|
67 |
+
return filename
|
68 |
+
return None
|
69 |
+
|
70 |
+
filename = save_and_play_audio(audio_recorder)
|
71 |
+
if filename is not None:
|
72 |
+
if st.button("Transcribe"):
|
73 |
+
transcription = transcribe_audio(openai.api_key, filename, "whisper-1")
|
74 |
+
st.write(transcription)
|
75 |
+
chat_with_model(transcription, '') # push transcript through as prompt
|
76 |
+
|
77 |
def create_file(filename, prompt, response):
|
78 |
if filename.endswith(".txt"):
|
79 |
with open(filename, 'w') as file:
|
|
|
91 |
def divide_document(document, max_length):
|
92 |
return [document[i:i+max_length] for i in range(0, len(document), max_length)]
|
93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
def get_table_download_link(file_path):
|
95 |
with open(file_path, 'r') as file:
|
96 |
data = file.read()
|
|
|
108 |
href = f'<a href="data:{mime_type};base64,{b64}" target="_blank" download="{file_name}">{file_name}</a>'
|
109 |
return href
|
110 |
|
|
|
111 |
def CompressXML(xml_text):
|
112 |
root = ET.fromstring(xml_text)
|
113 |
for elem in list(root.iter()):
|
|
|
137 |
return ""
|
138 |
|
139 |
def main():
|
140 |
+
user_prompt = st.text_area("Enter prompts, instructions & questions:", '', height=100)
|
141 |
+
|
142 |
+
collength, colupload = st.columns([2,3]) # adjust the ratio as needed
|
143 |
+
with collength:
|
144 |
+
#max_length = 12000 - optimal for gpt35 turbo. 2x=24000 for gpt4. 8x=96000 for gpt4-32k.
|
145 |
+
max_length = st.slider("File section length for large files", min_value=1000, max_value=128000, value=12000, step=1000)
|
146 |
+
with colupload:
|
147 |
+
uploaded_file = st.file_uploader("Add a file for context:", type=["xml", "json", "html", "htm", "md", "txt"])
|
148 |
+
|
149 |
document_sections = deque()
|
150 |
document_responses = {}
|
151 |
|
|
|
154 |
document_sections.extend(divide_document(file_content, max_length))
|
155 |
|
156 |
if len(document_sections) > 0:
|
157 |
+
|
158 |
+
if st.button("ποΈ View Upload"):
|
159 |
+
st.markdown("**Sections of the uploaded file:**")
|
160 |
+
for i, section in enumerate(list(document_sections)):
|
161 |
+
st.markdown(f"**Section {i+1}**\n{section}")
|
162 |
+
|
163 |
st.markdown("**Chat with the model:**")
|
164 |
for i, section in enumerate(list(document_sections)):
|
165 |
if i in document_responses:
|
166 |
st.markdown(f"**Section {i+1}**\n{document_responses[i]}")
|
167 |
else:
|
168 |
if st.button(f"Chat about Section {i+1}"):
|
169 |
+
st.write('Reasoning with your inputs...')
|
170 |
response = chat_with_model(user_prompt, section)
|
171 |
st.write('Response:')
|
172 |
st.write(response)
|
173 |
document_responses[i] = response
|
174 |
+
filename = generate_filename(f"{user_prompt}_section_{i+1}", choice)
|
175 |
+
create_file(filename, user_prompt, response)
|
176 |
+
st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
177 |
|
178 |
if st.button('π¬ Chat'):
|
179 |
+
st.write('Reasoning with your inputs...')
|
180 |
response = chat_with_model(user_prompt, ''.join(list(document_sections)))
|
181 |
st.write('Response:')
|
182 |
st.write(response)
|
|
|
185 |
create_file(filename, user_prompt, response)
|
186 |
st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
187 |
|
188 |
+
all_files = glob.glob("*.*")
|
189 |
+
all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 20] # exclude files with short names
|
190 |
+
all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
|
191 |
+
|
192 |
for file in all_files:
|
193 |
+
col1, col3 = st.sidebar.columns([5,1]) # adjust the ratio as needed
|
194 |
with col1:
|
195 |
st.markdown(get_table_download_link(file), unsafe_allow_html=True)
|
196 |
+
with col3:
|
197 |
+
if st.button("π", key="delete_"+file):
|
198 |
os.remove(file)
|
199 |
st.experimental_rerun()
|
200 |
+
|
201 |
if __name__ == "__main__":
|
202 |
main()
|
203 |
+
|