File size: 1,615 Bytes
cb78863
 
b8bbb80
cb78863
b8bbb80
cb78863
 
b8bbb80
 
cb78863
b8bbb80
cb78863
b8bbb80
 
cb78863
 
b8bbb80
 
 
 
cb78863
 
 
 
 
 
 
 
 
 
 
b8bbb80
 
 
 
cb78863
 
b8bbb80
cb78863
b8bbb80
cb78863
b8bbb80
cb78863
b8bbb80
cb78863
 
 
 
b8bbb80
 
cb78863
 
b8bbb80
cb78863
 
 
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 os
import gradio as gr
import numpy as np
from gtts import gTTS
import torch
import whisper  # Correct import from openai-whisper package
from groq import Groq
import io
import tempfile  # To handle temporary audio file saving

# Initialize Groq API client
client = Groq(api_key="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")
# Load Whisper model
whisper_model = whisper.load_model("base")  # Use 'whisper' directly

def transcribe_audio(audio_file):
    # Load audio
    audio, sr = sf.read(audio_file)
    # Transcribe audio using Whisper
    result = whisper_model.transcribe(audio, language="en")
    return result['text']

def get_response(prompt):
    chat_completion = client.chat.completions.create(
        messages=[{"role": "user", "content": prompt}],
        model="llama3-8b-8192",
    )
    return chat_completion.choices[0].message.content

def text_to_speech(text):
    tts = gTTS(text)
    audio_buffer = io.BytesIO()
    tts.save(audio_buffer)
    audio_buffer.seek(0)
    return audio_buffer

def chatbot(audio_file):
    # Transcribe audio to text
    user_input = transcribe_audio(audio_file)
    # Get response from Llama 8B
    response = get_response(user_input)
    # Convert response to speech
    audio_output = text_to_speech(response)
    return audio_output

# Gradio interface
iface = gr.Interface(
    fn=chatbot,
    inputs=gr.Audio(type="filepath"),  # Remove 'source' argument
    outputs=gr.Audio(type="filepath"),
    live=True,
    title="Voice to Voice Chatbot",
    description="Speak into the microphone, and the chatbot will respond!"
)

iface.launch()