awacke1 commited on
Commit
d8e0155
·
1 Parent(s): 110a8a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -27
app.py CHANGED
@@ -4,13 +4,6 @@ import streamlit as st
4
  from datetime import datetime
5
  from audio_recorder_streamlit import audio_recorder
6
 
7
- API_URL = "https://tonpixzfvq3791u9.us-east-1.aws.endpoints.huggingface.cloud"
8
- key = 'test-public-anonymous-T4-Whisper-Small-En'
9
- headers = {
10
- "Authorization": "Bearer {key}",
11
- "Content-Type": "audio/wav"
12
- }
13
-
14
  def generate_filename(prompt, file_type):
15
  central = pytz.timezone('US/Central')
16
  safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
@@ -18,39 +11,56 @@ def generate_filename(prompt, file_type):
18
  safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:90]
19
  return f"{safe_date_time}_{safe_prompt}.{file_type}"
20
 
21
- def query(filename):
22
- with open(filename, "rb") as f:
23
- data = f.read()
24
- response = requests.post(API_URL, headers=headers, data=data)
25
- return response.json()
26
-
27
  def save_and_play_audio(audio_recorder):
28
- audio_bytes = audio_recorder.get_audio()
29
  if audio_bytes:
30
  filename = generate_filename("Recording", "wav")
31
  with open(filename, 'wb') as f:
32
  f.write(audio_bytes)
33
  st.audio(audio_bytes, format="audio/wav")
34
  return filename
35
- return None
36
 
 
37
  def transcribe_audio(file_path):
38
- API_URL = "https://tonpixzfvq3791u9.us-east-1.aws.endpoints.huggingface.cloud"
39
  headers = {
40
- "Authorization": f"Bearer {openai_key}",
 
41
  }
42
  with open(file_path, 'rb') as f:
43
  data = {'file': f}
44
- response = requests.post(API_URL, headers=headers, files=data)
 
 
45
  if response.status_code == 200:
46
- st.write(response.json())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
- st.title("Speech to Text")
49
- st.write("Record your speech and get the text.")
50
 
51
- # Audio, transcribe, GPT:
52
- filename = save_and_play_audio(audio_recorder)
53
- if filename is not None:
54
- transcription = transcribe_audio(filename)
55
- #st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
56
- filename = None
 
4
  from datetime import datetime
5
  from audio_recorder_streamlit import audio_recorder
6
 
 
 
 
 
 
 
 
7
  def generate_filename(prompt, file_type):
8
  central = pytz.timezone('US/Central')
9
  safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
 
11
  safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:90]
12
  return f"{safe_date_time}_{safe_prompt}.{file_type}"
13
 
14
+ # 10. Audio recorder to Wav file:
 
 
 
 
 
15
  def save_and_play_audio(audio_recorder):
16
+ audio_bytes = audio_recorder()
17
  if audio_bytes:
18
  filename = generate_filename("Recording", "wav")
19
  with open(filename, 'wb') as f:
20
  f.write(audio_bytes)
21
  st.audio(audio_bytes, format="audio/wav")
22
  return filename
 
23
 
24
+ # 9B. Speech transcription to file output - OPENAI Whisper
25
  def transcribe_audio(file_path):
26
+ key=os.getenv('IE_KEY')
27
  headers = {
28
+ "Authorization": f"Bearer {key}",
29
+ "Content-Type": "audio/wav"
30
  }
31
  with open(file_path, 'rb') as f:
32
  data = {'file': f}
33
+ API_URL = "https://tonpixzfvq3791u9.us-east-1.aws.endpoints.huggingface.cloud"
34
+ response = requests.post(API_URL, headers=headers, data=data)
35
+
36
  if response.status_code == 200:
37
+ st.write('Reasoning with your transcription..')
38
+ try:
39
+ transcript=response.json().get('text')
40
+ except:
41
+ transcript=response
42
+
43
+ st.write(transcript)
44
+ #gptResponse = chat_with_model(transcript, systemPrompt=user_prompt_System, model=MODELCHOICE) # send transcript to ChatGPT - prompts, systemPrompt=SYSTEM_PROMPT, model="Gpt-4-32k"
45
+ #filename = generate_filename(transcript, choice)
46
+ #create_file(filename, transcript, gptResponse) # write output file
47
+ #return gptResponse
48
+ else:
49
+ #st.write(response.json())
50
+ #st.error("Error in API call.")
51
+ return None
52
+
53
+ def main():
54
+ st.title("Speech to Text")
55
+ st.write("Record your speech and get the text.")
56
+
57
+ # Audio, transcribe, GPT:
58
+ filename = save_and_play_audio(audio_recorder)
59
+ if filename is not None:
60
+ transcription = transcribe_audio(filename)
61
+ #st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
62
+ filename = None
63
 
64
+ if __name__ == "__main__":
65
+ main()
66