Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
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 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
99 |
|
100 |
-
|
101 |
-
|
102 |
-
|
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 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|