File size: 2,902 Bytes
228930f
669e9dd
228930f
669e9dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
691cb51
 
228930f
 
3bf7e40
 
691cb51
3bf7e40
691cb51
 
 
 
0a7ae60
228930f
691cb51
 
6a73b14
 
 
 
 
691cb51
 
 
6a73b14
 
 
691cb51
 
6a73b14
 
 
 
 
 
228930f
691cb51
 
 
 
 
 
228930f
691cb51
228930f
691cb51
 
 
 
 
 
 
 
228930f
691cb51
 
228930f
691cb51
228930f
 
073411f
691cb51
 
 
228930f
 
 
691cb51
228930f
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os
import sys

# Function to check and install required libraries
def install_libraries():
    try:
        import whisper
        import gtts
        import gradio as gr
        from groq import Groq
    except ImportError:
        print("Required libraries not found. Installing now...")
        os.system("pip install git+https://github.com/openai/whisper.git gtts gradio groq")
        # Try to import again after installation
        try:
            import whisper
            import gtts
            import gradio as gr
            from groq import Groq
        except ImportError:
            print("Failed to install required libraries. Please install them manually.")
            sys.exit(1)

# Call the install function
install_libraries()

# Now we can safely import the libraries
import whisper
import gtts
import gradio as gr
from groq import Groq

# Load Whisper model
model = whisper.load_model("base")

# Hard-code the Groq API key
Groq_api_key = "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"

# Check if the API key is set (even though it's hard-coded, we can keep this check)
if Groq_api_key is None:
    raise ValueError("API key for Groq not found. Please set the 'GROQ_API_KEY' environment variable.")

# Initialize the Groq client
client = Groq(api_key=Groq_api_key)

# Function to transcribe audio
def transcribe_audio(audio_path):
    try:
        result = model.transcribe(audio_path)
        return result["text"]
    except Exception as e:
        return f"Error transcribing audio: {str(e)}"

# Function to get response from Groq's API
def get_groq_response(transcribed_text):
    try:
        chat_completion = client.chat.completions.create(
            messages=[{
                "role": "user",
                "content": transcribed_text,
            }],
            model="llama3-8b-8192",
        )
        return chat_completion.choices[0].message.content
    except Exception as e:
        return f"Error getting response from Groq: {str(e)}"

# Function to convert text to speech
def text_to_speech(text):
    tts = gtts.gTTS(text=text, lang='en')
    audio_path = "response.mp3"
    tts.save(audio_path)
    return audio_path

# Gradio chatbot function
def chatbot(audio):
    # Step 1: Transcribe the audio
    transcribed_text = transcribe_audio(audio)
    
    # Step 2: Get LLM response from Groq API
    response_text = get_groq_response(transcribed_text)
    
    # Step 3: Convert response text to speech
    response_audio = text_to_speech(response_text)
    
    # Return the response audio
    return response_audio

# Create a Gradio interface
iface = gr.Interface(
    fn=chatbot,
    inputs=gr.Audio(type="filepath"),  # Updated to remove 'source'
    outputs="audio",
    title="Voice-to-Voice Chatbot",
    description="Speak to the chatbot and listen to the response!",
    live=True
)

# Launch the interface
iface.launch()