awacke1 commited on
Commit
4a28b51
·
1 Parent(s): b43823f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -27
app.py CHANGED
@@ -30,31 +30,24 @@ def generate_filename(prompt, file_type):
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
-
34
- # Audio record and transcribe:
35
- def transcribe_audio_ui(openai_key, file_path):
36
  OPENAI_API_URL = "https://api.openai.com/v1/audio/transcriptions"
37
  headers = {
38
  "Authorization": f"Bearer {openai_key}",
39
- "Content-Type": "multipart/form-data",
40
  }
41
- def transcribe_audio(file_path, model):
42
- data = {
43
- 'file': open(file_path, 'rb'),
44
- 'model': model,
45
- }
46
- response = requests.post(OPENAI_API_URL, headers=headers, files=data)
47
- if response.status_code == 200:
48
- st.write(response.json())
49
- return response.json().get('text')
50
- else:
51
- st.write(response.json())
52
- st.error("Error in API call.")
53
- return None
54
- if file_path is not None:
55
- if st.button("Transcribe"):
56
- transcription = transcribe_audio(file_path, "whisper-1")
57
- st.write(transcription)
58
 
59
  def save_and_play_audio(audio_recorder):
60
  audio_bytes = audio_recorder()
@@ -67,12 +60,10 @@ def save_and_play_audio(audio_recorder):
67
  return None
68
 
69
  filename = save_and_play_audio(audio_recorder)
70
- if filename:
71
- st.write(f"Audio file has been saved as {filename}")
72
- transcribe_audio_ui(openai.api_key, filename)
73
- else:
74
- st.write("No audio data was recorded")
75
-
76
 
77
  def chat_with_model(prompt, document_section):
78
  model = model_choice
 
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()
 
60
  return None
61
 
62
  filename = save_and_play_audio(audio_recorder)
63
+ if filename is not None:
64
+ if st.button("Transcribe"):
65
+ transcription = transcribe_audio(openai_key, filename, "whisper-1")
66
+ st.write(transcription)
 
 
67
 
68
  def chat_with_model(prompt, document_section):
69
  model = model_choice