Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -23,32 +23,40 @@ 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 |
-
|
27 |
def generate_filename(prompt, file_type):
|
28 |
central = pytz.timezone('US/Central')
|
29 |
safe_date_time = datetime.now(central).strftime("%m%d_%I%M")
|
30 |
safe_prompt = "".join(x for x in prompt if x.isalnum())[:45]
|
31 |
return f"{safe_date_time}_{safe_prompt}.{file_type}"
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
def transcribe_audio(openai_key, file_path, model):
|
34 |
OPENAI_API_URL = "https://api.openai.com/v1/audio/transcriptions"
|
35 |
headers = {
|
36 |
"Authorization": f"Bearer {openai_key}",
|
37 |
}
|
38 |
-
|
39 |
with open(file_path, 'rb') as f:
|
40 |
data = {'file': f}
|
41 |
response = requests.post(OPENAI_API_URL, headers=headers, files=data, data={'model': model})
|
42 |
-
|
43 |
if response.status_code == 200:
|
44 |
st.write(response.json())
|
|
|
|
|
|
|
|
|
45 |
return response.json().get('text')
|
46 |
else:
|
47 |
st.write(response.json())
|
48 |
st.error("Error in API call.")
|
49 |
return None
|
50 |
|
51 |
-
|
52 |
def save_and_play_audio(audio_recorder):
|
53 |
audio_bytes = audio_recorder()
|
54 |
if audio_bytes:
|
@@ -64,14 +72,7 @@ if filename is not None:
|
|
64 |
if st.button("Transcribe"):
|
65 |
transcription = transcribe_audio(openai.api_key, filename, "whisper-1")
|
66 |
st.write(transcription)
|
67 |
-
|
68 |
-
def chat_with_model(prompt, document_section):
|
69 |
-
model = model_choice
|
70 |
-
conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
|
71 |
-
conversation.append({'role': 'user', 'content': prompt})
|
72 |
-
conversation.append({'role': 'assistant', 'content': document_section})
|
73 |
-
response = openai.ChatCompletion.create(model=model, messages=conversation)
|
74 |
-
return response['choices'][0]['message']['content']
|
75 |
|
76 |
def create_file(filename, prompt, response):
|
77 |
if filename.endswith(".txt"):
|
|
|
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:
|
|
|
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"):
|