File size: 1,847 Bytes
cac2c45
 
29b48cb
 
 
 
 
 
 
 
 
 
 
cac2c45
 
 
 
 
 
 
 
 
29b48cb
cac2c45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29b48cb
 
cac2c45
29b48cb
 
cac2c45
 
29b48cb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import gradio as gr
import requests
from gtts import gTTS  # Import gTTS for Text-to-Speech
import tempfile

# Function to convert text to speech and transcribe
def text_to_speech_transcribe(text):
    # Convert text to speech
    tts = gTTS(text, lang='en')
    with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_audio:
        audio_path = tmp_audio.name
        tts.save(audio_path)
    
    # Read audio file in binary mode
    with open(audio_path, "rb") as audio_file:
        audio_data = audio_file.read()

    # Groq API endpoint for audio transcription
    groq_api_endpoint = "https://api.groq.com/openai/v1/audio/transcriptions"

    # Replace 'YOUR_GROQ_API_KEY' with your actual Groq API key
    headers = {
        "Authorization": "Bearer YOUR_GROQ_API_KEY",  # Replace with your Groq API key
    }

    # Prepare the files and data for the request
    files = {
        'file': ('audio.wav', audio_data, 'audio/wav'),
    }
    data = {
        'model': 'whisper-large-v3-turbo',  # Specify the model to use
        'response_format': 'json',          # Desired response format
        'language': 'en',                   # Language of the audio
    }

    # Send audio to Groq API
    response = requests.post(groq_api_endpoint, headers=headers, files=files, data=data)

    # Parse response
    if response.status_code == 200:
        result = response.json()
        return result.get("text", "No transcription available.")
    else:
        return f"Error: {response.status_code}, {response.text}"

# Gradio interface
iface = gr.Interface(
    fn=text_to_speech_transcribe,
    inputs="text",  # Input text to be converted to speech
    outputs="text",
    title="Text to Speech and Transcription",
    description="Enter text to convert it to audio, then transcribe it with the Groq API."
)

iface.launch()