dindizz commited on
Commit
71cfe0a
·
verified ·
1 Parent(s): 751709f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -36
app.py CHANGED
@@ -1,49 +1,42 @@
1
  import openai
2
  import gradio as gr
 
3
  import os
4
 
5
- # Set up your OpenAI API key from environment variables
6
- openai.api_key = os.getenv('API_KEY')
7
 
8
- def transcribe_audio(file_path):
9
- if file_path is None:
10
- return "Error: No file path provided."
11
 
12
- # Debugging: Print the file path
13
- print(f"Received file path: {file_path}")
14
-
15
- # Open the wav file
 
16
  try:
17
- audio_file = open(file_path, 'rb')
18
- response = openai.audio.transcriptions.create(
19
- model="whisper-1", # specify the appropriate model for transcription
20
- file=audio_file,
21
- language='ta' # specify 'ta' for Tamil language
22
- )
23
- audio_file.close()
 
 
 
24
  except Exception as e:
25
- return f"Error during transcription: {e}"
26
-
27
- # Extract the transcription text
28
- transcription_text = response['text']
29
- return transcription_text
30
-
31
- def transcribe_and_display(audio_path):
32
- # Debugging: Print the audio path
33
- print(f"Received audio path: {audio_path}")
34
-
35
- # Transcribe the audio file
36
- transcription = transcribe_audio(audio_path)
37
- return transcription
38
 
39
- # Gradio interface
40
- iface = gr.Interface(
41
- fn=transcribe_and_display,
42
- inputs=gr.Audio(type="filepath"),
43
  outputs="text",
44
- title="Speech to Text",
45
- description="Upload a WAV file to transcribe speech to Tamil text."
46
  )
47
 
48
  # Launch the interface
49
- iface.launch()
 
 
1
  import openai
2
  import gradio as gr
3
+ from dotenv import load_dotenv
4
  import os
5
 
6
+ # Load environment variables from .env file
7
+ load_dotenv()
8
 
9
+ # Set up OpenAI API key
10
+ openai.api_key = os.getenv("OPENAI_API_KEY")
 
11
 
12
+ def speech_to_text(audio):
13
+ # Check if the audio input is received correctly
14
+ if audio is None:
15
+ return "No audio file uploaded."
16
+
17
  try:
18
+ # Open the audio file
19
+ with open(audio, "rb") as audio_file:
20
+ # Transcribe the audio to text using OpenAI's Whisper API
21
+ response = openai.Audio.transcriptions.create(
22
+ file=audio_file,
23
+ model="whisper-1", # Use the appropriate model for transcription
24
+ language="ta" # Specify the language as Tamil
25
+ )
26
+ text = response['text']
27
+ return text
28
  except Exception as e:
29
+ return f"Error during transcription: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
+ # Set up the Gradio interface
32
+ interface = gr.Interface(
33
+ fn=speech_to_text,
34
+ inputs=gr.Audio(source="upload", type="filepath"),
35
  outputs="text",
36
+ title="Speech to Text Transcription",
37
+ description="Transcribe speech to Tamil text using OpenAI's API."
38
  )
39
 
40
  # Launch the interface
41
+ if __name__ == "__main__":
42
+ interface.launch()