awacke1 commited on
Commit
702c71f
·
1 Parent(s): 50f3b7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -52
app.py CHANGED
@@ -23,8 +23,6 @@ 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
- uploaded_files = []
27
-
28
  def generate_filename(prompt, file_type):
29
  central = pytz.timezone('US/Central')
30
  safe_date_time = datetime.now(central).strftime("%m%d_%I%M")
@@ -65,62 +63,60 @@ def save_and_play_audio(audio_recorder):
65
  with open(filename, 'wb') as f:
66
  f.write(audio_bytes)
67
  st.audio(audio_bytes, format="audio/wav")
68
- uploaded_files.append(filename) # Add the new file name to the list
69
  return filename
70
  return None
71
 
72
- # ... (All your other function definitions)
73
-
74
- def main():
75
- user_prompt = st.text_area("Enter prompts, instructions & questions:", '', height=100)
76
-
77
- collength, colupload = st.columns([2,3]) # adjust the ratio as needed
78
- with collength:
79
- max_length = st.slider("File section length for large files", min_value=1000, max_value=128000, value=12000, step=1000)
80
- with colupload:
81
- uploaded_file = st.file_uploader("Add a file for context:", type=["xml", "json", "html", "htm", "md", "txt"])
82
-
83
- document_sections = deque()
84
- document_responses = {}
85
 
86
- if uploaded_file is not None:
87
- file_content = uploaded_file.getvalue().decode('utf-8')
88
- # Handle different file types here
89
- # ...
90
- document_sections.append(file_content)
91
-
92
- new_filename = save_and_play_audio(audio_recorder)
93
- if new_filename is not None:
94
- st.write(f'File {new_filename} uploaded.')
95
- if st.button("Transcribe"):
96
- transcription = transcribe_audio(openai.api_key, new_filename, "whisper-1")
97
- st.write(transcription)
98
- chat_with_model(transcription, '') # push transcript through as prompt
 
 
99
 
100
- if st.button('💬 Chat'):
101
- user_responses = document_responses.get(user_prompt, [])
102
- if len(user_responses) > 0:
103
- st.write(user_responses[-1])
104
- else:
105
- if document_sections:
106
- st.write('First document section:')
107
- st.write(document_sections[0])
108
- document_responses[user_prompt] = [chat_with_model(user_prompt, document_sections[0])]
109
- st.write(document_responses[user_prompt][-1])
110
- else:
111
- document_responses[user_prompt] = [chat_with_model(user_prompt, '')]
112
- st.write(document_responses[user_prompt][-1])
113
-
114
- if uploaded_files:
115
- st.write(f'Last uploaded file: {uploaded_files[-1]}')
116
 
117
- for filename in uploaded_files:
118
- if st.button(f"Transcribe and Chat for {filename}"):
119
- transcription, response = transcribe_and_chat(openai.api_key, filename, "whisper-1")
120
- if transcription is not None and response is not None:
121
- filename = generate_filename(transcription, choice)
122
- create_file(filename, transcription, response)
123
- st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
125
  if __name__ == "__main__":
126
  main()
 
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")
 
63
  with open(filename, 'wb') as f:
64
  f.write(audio_bytes)
65
  st.audio(audio_bytes, format="audio/wav")
 
66
  return filename
67
  return None
68
 
69
+ def transcribe_and_chat(openai_key, file_path, model):
70
+ transcription = transcribe_audio(openai_key, file_path, model)
71
+ if transcription is not None:
72
+ response = chat_with_model(transcription, '')
73
+ return transcription, response
74
+ else:
75
+ return None, None
 
 
 
 
 
 
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
+ elif filename.endswith(".py"):
88
+ with open(filename, 'w') as file:
89
+ file.write(f"# Prompt: \n'''{prompt}'''\n # Response: \n'''{response}'''")
90
+ else:
91
+ st.error("Unsupported file type!")
92
 
93
+ def main():
94
+ st.sidebar.header("Choose your Input Method:")
95
+ input_choice = st.sidebar.radio("",('Type it out','Record it'))
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
+ if input_choice == 'Type it out':
98
+ st.header("Type your query")
99
+ prompt = st.text_area("Input")
100
+ document_section = st.text_area("Document Section")
101
+ if st.button("Get Response"):
102
+ filename = generate_filename(prompt, choice)
103
+ response = chat_with_model(prompt, document_section)
104
+ st.write('Response:')
105
+ st.write(response)
106
+ create_file(filename, prompt, response)
107
+ elif input_choice == 'Record it':
108
+ st.header("Record your query")
109
+ st.write("Press record to start recording")
110
+ audio_recorder = audio_recorder()
111
+ filename = save_and_play_audio(audio_recorder)
112
+ if filename:
113
+ if st.button("Transcribe and Chat"):
114
+ transcription, response = transcribe_and_chat(openai.api_key, filename, "whisper-1")
115
+ if transcription and response:
116
+ st.write('Transcription:')
117
+ st.write(transcription)
118
+ st.write('Response:')
119
+ st.write(response)
120
 
121
  if __name__ == "__main__":
122
  main()