Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,65 +3,222 @@ import openai
|
|
3 |
import os
|
4 |
import base64
|
5 |
import glob
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
from datetime import datetime
|
7 |
-
from dotenv import load_dotenv
|
8 |
from openai import ChatCompletion
|
9 |
-
|
10 |
-
|
|
|
|
|
11 |
|
12 |
openai.api_key = os.getenv('OPENAI_KEY')
|
|
|
13 |
|
14 |
-
|
15 |
-
|
|
|
16 |
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
19 |
|
|
|
|
|
|
|
|
|
|
|
20 |
response = openai.ChatCompletion.create(model=model, messages=conversation)
|
21 |
return response['choices'][0]['message']['content']
|
22 |
|
23 |
-
def
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
def create_file(filename, prompt, response):
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
def get_table_download_link(file_path):
|
33 |
with open(file_path, 'r') as file:
|
34 |
data = file.read()
|
35 |
b64 = base64.b64encode(data.encode()).decode()
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
return href
|
38 |
-
|
39 |
-
def main():
|
40 |
-
st.title("Chat with AI")
|
41 |
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
if
|
49 |
-
|
|
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
st.write('Response:')
|
55 |
st.write(response)
|
56 |
-
|
57 |
-
filename = generate_filename(user_prompt)
|
58 |
create_file(filename, user_prompt, response)
|
|
|
59 |
|
60 |
-
st.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
if __name__ == "__main__":
|
67 |
-
main()
|
|
|
3 |
import os
|
4 |
import base64
|
5 |
import glob
|
6 |
+
import json
|
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:
|
80 |
+
file.write(f"Prompt:\n{prompt}\nResponse:\n{response}")
|
81 |
+
elif filename.endswith(".htm"):
|
82 |
+
with open(filename, 'w') as file:
|
83 |
+
file.write(f"<h1>Prompt:</h1> <p>{prompt}</p> <h1>Response:</h1> <p>{response}</p>")
|
84 |
+
elif filename.endswith(".md"):
|
85 |
+
with open(filename, 'w') as file:
|
86 |
+
file.write(f"# Prompt:\n{prompt}\n# Response:\n{response}")
|
87 |
+
|
88 |
+
def truncate_document(document, length):
|
89 |
+
return document[:length]
|
90 |
+
|
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()
|
97 |
b64 = base64.b64encode(data.encode()).decode()
|
98 |
+
file_name = os.path.basename(file_path)
|
99 |
+
ext = os.path.splitext(file_name)[1] # get the file extension
|
100 |
+
if ext == '.txt':
|
101 |
+
mime_type = 'text/plain'
|
102 |
+
elif ext == '.htm':
|
103 |
+
mime_type = 'text/html'
|
104 |
+
elif ext == '.md':
|
105 |
+
mime_type = 'text/markdown'
|
106 |
+
else:
|
107 |
+
mime_type = 'application/octet-stream' # general binary data type
|
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()):
|
114 |
+
if isinstance(elem.tag, str) and 'Comment' in elem.tag:
|
115 |
+
elem.parent.remove(elem)
|
116 |
+
return ET.tostring(root, encoding='unicode', method="xml")
|
117 |
+
|
118 |
+
def read_file_content(file,max_length):
|
119 |
+
if file.type == "application/json":
|
120 |
+
content = json.load(file)
|
121 |
+
return str(content)
|
122 |
+
elif file.type == "text/html" or file.type == "text/htm":
|
123 |
+
content = BeautifulSoup(file, "html.parser")
|
124 |
+
return content.text
|
125 |
+
elif file.type == "application/xml" or file.type == "text/xml":
|
126 |
+
tree = ET.parse(file)
|
127 |
+
root = tree.getroot()
|
128 |
+
xml = CompressXML(ET.tostring(root, encoding='unicode'))
|
129 |
+
return xml
|
130 |
+
elif file.type == "text/markdown" or file.type == "text/md":
|
131 |
+
md = mistune.create_markdown()
|
132 |
+
content = md(file.read().decode())
|
133 |
+
return content
|
134 |
+
elif file.type == "text/plain":
|
135 |
+
return file.getvalue().decode()
|
136 |
+
else:
|
137 |
+
return ""
|
138 |
+
|
139 |
+
def transcribe_and_chat(openai_key, file_path, model):
|
140 |
+
transcription = transcribe_audio(openai_key, file_path, model)
|
141 |
+
if transcription is not None:
|
142 |
+
response = chat_with_model(transcription, '')
|
143 |
+
st.write('Chat Response:')
|
144 |
+
st.write(response)
|
145 |
+
return transcription, response
|
146 |
+
else:
|
147 |
+
return None, None
|
148 |
|
149 |
+
|
150 |
+
def main():
|
151 |
+
user_prompt = st.text_area("Enter prompts, instructions & questions:", '', height=100)
|
152 |
+
|
153 |
+
collength, colupload = st.columns([2,3]) # adjust the ratio as needed
|
154 |
+
with collength:
|
155 |
+
#max_length = 12000 - optimal for gpt35 turbo. 2x=24000 for gpt4. 8x=96000 for gpt4-32k.
|
156 |
+
max_length = st.slider("File section length for large files", min_value=1000, max_value=128000, value=12000, step=1000)
|
157 |
+
with colupload:
|
158 |
+
uploaded_file = st.file_uploader("Add a file for context:", type=["xml", "json", "html", "htm", "md", "txt"])
|
159 |
+
|
160 |
+
document_sections = deque()
|
161 |
+
document_responses = {}
|
162 |
|
163 |
+
if uploaded_file is not None:
|
164 |
+
file_content = read_file_content(uploaded_file, max_length)
|
165 |
+
document_sections.extend(divide_document(file_content, max_length))
|
166 |
|
167 |
+
|
168 |
+
if len(document_sections) > 0:
|
169 |
+
|
170 |
+
if st.button("👁️ View Upload"):
|
171 |
+
st.markdown("**Sections of the uploaded file:**")
|
172 |
+
for i, section in enumerate(list(document_sections)):
|
173 |
+
st.markdown(f"**Section {i+1}**\n{section}")
|
174 |
+
|
175 |
+
st.markdown("**Chat with the model:**")
|
176 |
+
for i, section in enumerate(list(document_sections)):
|
177 |
+
if i in document_responses:
|
178 |
+
st.markdown(f"**Section {i+1}**\n{document_responses[i]}")
|
179 |
+
else:
|
180 |
+
if st.button(f"Chat about Section {i+1}"):
|
181 |
+
st.write('Reasoning with your inputs...')
|
182 |
+
response = chat_with_model(user_prompt, section)
|
183 |
+
st.write('Response:')
|
184 |
+
st.write(response)
|
185 |
+
document_responses[i] = response
|
186 |
+
filename = generate_filename(f"{user_prompt}_section_{i+1}", choice)
|
187 |
+
create_file(filename, user_prompt, response)
|
188 |
+
st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
189 |
+
|
190 |
+
if st.button('💬 Chat'):
|
191 |
+
st.write('Reasoning with your inputs...')
|
192 |
+
response = chat_with_model(user_prompt, ''.join(list(document_sections)))
|
193 |
st.write('Response:')
|
194 |
st.write(response)
|
195 |
+
|
196 |
+
filename = generate_filename(user_prompt, choice)
|
197 |
create_file(filename, user_prompt, response)
|
198 |
+
st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
199 |
|
|
|
200 |
|
201 |
+
if filename is not None:
|
202 |
+
if st.button("Transcribe and Chat"):
|
203 |
+
transcription, response = transcribe_and_chat(openai.api_key, filename, "whisper-1")
|
204 |
+
if transcription is not None and response is not None:
|
205 |
+
filename = generate_filename(transcription, choice)
|
206 |
+
create_file(filename, transcription, response)
|
207 |
+
st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
208 |
|
209 |
+
|
210 |
+
all_files = glob.glob("*.*")
|
211 |
+
all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 20] # exclude files with short names
|
212 |
+
all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
|
213 |
+
|
214 |
+
for file in all_files:
|
215 |
+
col1, col3 = st.sidebar.columns([5,1]) # adjust the ratio as needed
|
216 |
+
with col1:
|
217 |
+
st.markdown(get_table_download_link(file), unsafe_allow_html=True)
|
218 |
+
with col3:
|
219 |
+
if st.button("🗑", key="delete_"+file):
|
220 |
+
os.remove(file)
|
221 |
+
st.experimental_rerun()
|
222 |
+
|
223 |
if __name__ == "__main__":
|
224 |
+
main()
|