Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,42 @@
|
|
| 1 |
import openai
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
return "Error: No file path provided."
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
try:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 41 |
-
fn=
|
| 42 |
-
inputs=gr.Audio(type="filepath"),
|
| 43 |
outputs="text",
|
| 44 |
-
title="Speech to Text",
|
| 45 |
-
description="
|
| 46 |
)
|
| 47 |
|
| 48 |
# Launch the interface
|
| 49 |
-
|
|
|
|
|
|
| 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()
|