awacke1 commited on
Commit
361fb06
β€’
1 Parent(s): 9e85e3e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -30
app.py CHANGED
@@ -16,13 +16,6 @@ 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")
@@ -33,10 +26,12 @@ 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 = {
@@ -47,7 +42,8 @@ def transcribe_audio(openai_key, file_path, model):
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)
@@ -67,27 +63,19 @@ def save_and_play_audio(audio_recorder):
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
 
@@ -99,6 +87,12 @@ def get_table_download_link(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':
@@ -136,6 +130,35 @@ def read_file_content(file,max_length):
136
  else:
137
  return ""
138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  def main():
140
  user_prompt = st.text_area("Enter prompts, instructions & questions:", '', height=100)
141
 
@@ -144,7 +167,7 @@ def main():
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 = {}
@@ -167,7 +190,7 @@ def main():
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
@@ -177,7 +200,7 @@ def main():
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)
183
 
@@ -188,16 +211,48 @@ def main():
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
-
 
16
  from collections import deque
17
  from audio_recorder_streamlit import audio_recorder
18
 
 
 
 
 
 
 
 
19
  def generate_filename(prompt, file_type):
20
  central = pytz.timezone('US/Central')
21
  safe_date_time = datetime.now(central).strftime("%m%d_%I%M")
 
26
  model = model_choice
27
  conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
28
  conversation.append({'role': 'user', 'content': prompt})
29
+ if len(document_section)>0:
30
+ conversation.append({'role': 'assistant', 'content': document_section})
31
  response = openai.ChatCompletion.create(model=model, messages=conversation)
32
+ #return response
33
  return response['choices'][0]['message']['content']
34
+
35
  def transcribe_audio(openai_key, file_path, model):
36
  OPENAI_API_URL = "https://api.openai.com/v1/audio/transcriptions"
37
  headers = {
 
42
  response = requests.post(OPENAI_API_URL, headers=headers, files=data, data={'model': model})
43
  if response.status_code == 200:
44
  st.write(response.json())
45
+
46
+ response2 = chat_with_model(response.json().get('text'), '') # *************************************
47
  st.write('Responses:')
48
  #st.write(response)
49
  st.write(response2)
 
63
  return filename
64
  return None
65
 
 
 
 
 
 
 
 
66
  def create_file(filename, prompt, response):
67
  if filename.endswith(".txt"):
68
  with open(filename, 'w') as file:
69
+ file.write(f"{prompt}\n{response}")
70
  elif filename.endswith(".htm"):
71
  with open(filename, 'w') as file:
72
+ file.write(f"{prompt} {response}")
73
  elif filename.endswith(".md"):
74
  with open(filename, 'w') as file:
75
+ file.write(f"{prompt}\n\n{response}")
76
+
77
  def truncate_document(document, length):
78
  return document[:length]
 
79
  def divide_document(document, max_length):
80
  return [document[i:i+max_length] for i in range(0, len(document), max_length)]
81
 
 
87
  ext = os.path.splitext(file_name)[1] # get the file extension
88
  if ext == '.txt':
89
  mime_type = 'text/plain'
90
+ elif ext == '.py':
91
+ mime_type = 'text/plain'
92
+ elif ext == '.xlsx':
93
+ mime_type = 'text/plain'
94
+ elif ext == '.csv':
95
+ mime_type = 'text/plain'
96
  elif ext == '.htm':
97
  mime_type = 'text/html'
98
  elif ext == '.md':
 
130
  else:
131
  return ""
132
 
133
+
134
+
135
+ def chat_with_file_contents(prompt, file_content):
136
+ conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
137
+ conversation.append({'role': 'user', 'content': prompt})
138
+ if len(file_content)>0:
139
+ conversation.append({'role': 'assistant', 'content': file_content})
140
+ response = openai.ChatCompletion.create(model=model_choice, messages=conversation)
141
+ return response['choices'][0]['message']['content']
142
+
143
+
144
+ # Sidebar and global
145
+ openai.api_key = os.getenv('OPENAI_KEY')
146
+ st.set_page_config(page_title="GPT Streamlit Document Reasoner",layout="wide")
147
+ menu = ["htm", "txt", "xlsx", "csv", "md", "py"] #619
148
+ choice = st.sidebar.selectbox("Output File Type:", menu)
149
+ model_choice = st.sidebar.radio("Select Model:", ('gpt-3.5-turbo', 'gpt-3.5-turbo-0301'))
150
+
151
+ # Audio, transcribe, GPT:
152
+ filename = save_and_play_audio(audio_recorder)
153
+ if filename is not None:
154
+ transcription = transcribe_audio(openai.api_key, filename, "whisper-1")
155
+ st.write(transcription)
156
+ gptOutput = chat_with_model(transcription, '') # *************************************
157
+ filename = generate_filename(transcription, choice)
158
+ create_file(filename, transcription, gptOutput)
159
+ st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
160
+
161
+
162
  def main():
163
  user_prompt = st.text_area("Enter prompts, instructions & questions:", '', height=100)
164
 
 
167
  #max_length = 12000 - optimal for gpt35 turbo. 2x=24000 for gpt4. 8x=96000 for gpt4-32k.
168
  max_length = st.slider("File section length for large files", min_value=1000, max_value=128000, value=12000, step=1000)
169
  with colupload:
170
+ uploaded_file = st.file_uploader("Add a file for context:", type=["xml", "json", "xlsx","csv","html", "htm", "md", "txt"])
171
 
172
  document_sections = deque()
173
  document_responses = {}
 
190
  else:
191
  if st.button(f"Chat about Section {i+1}"):
192
  st.write('Reasoning with your inputs...')
193
+ response = chat_with_model(user_prompt, section) # *************************************
194
  st.write('Response:')
195
  st.write(response)
196
  document_responses[i] = response
 
200
 
201
  if st.button('πŸ’¬ Chat'):
202
  st.write('Reasoning with your inputs...')
203
+ response = chat_with_model(user_prompt, ''.join(list(document_sections))) # *************************************
204
  st.write('Response:')
205
  st.write(response)
206
 
 
211
  all_files = glob.glob("*.*")
212
  all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 20] # exclude files with short names
213
  all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
214
+
215
+ # sidebar of files
216
+ file_contents=''
217
+ next_action=''
218
  for file in all_files:
219
+ col1, col2, col3, col4, col5 = st.sidebar.columns([1,6,1,1,1]) # adjust the ratio as needed
220
  with col1:
221
+ if st.button("🌐", key="md_"+file): # md emoji button
222
+ with open(file, 'r') as f:
223
+ file_contents = f.read()
224
+ next_action='md'
225
+ with col2:
226
  st.markdown(get_table_download_link(file), unsafe_allow_html=True)
227
  with col3:
228
+ if st.button("πŸ“‚", key="open_"+file): # open emoji button
229
+ with open(file, 'r') as f:
230
+ file_contents = f.read()
231
+ next_action='open'
232
+ with col4:
233
+ if st.button("πŸ”", key="read_"+file): # search emoji button
234
+ with open(file, 'r') as f:
235
+ file_contents = f.read()
236
+ next_action='search'
237
+ with col5:
238
  if st.button("πŸ—‘", key="delete_"+file):
239
  os.remove(file)
240
  st.experimental_rerun()
241
+
242
+ if len(file_contents) > 0:
243
+ if next_action=='open':
244
+ file_content_area = st.text_area("File Contents:", file_contents, height=500)
245
+ if next_action=='md':
246
+ st.markdown(file_contents)
247
+ if next_action=='search':
248
+ file_content_area = st.text_area("File Contents:", file_contents, height=500)
249
+ st.write('Reasoning with your inputs...')
250
+ response = chat_with_file_contents(user_prompt, file_contents)
251
+ st.write('Response:')
252
+ st.write(response)
253
+ filename = generate_filename(file_content_area, choice)
254
+ create_file(filename, file_content_area, response)
255
+ st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
256
+
257
  if __name__ == "__main__":
258
+ main()