File size: 2,126 Bytes
81c0724
2bc7f1c
 
 
51f2933
81c0724
2bc7f1c
 
81c0724
51f2933
 
 
 
 
81c0724
 
2bc7f1c
 
 
 
 
 
 
 
 
 
81c0724
 
2bc7f1c
 
 
 
 
81c0724
 
2bc7f1c
 
 
 
 
 
81c0724
 
51f2933
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c56de2b
51f2933
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
import gradio as gr
import whisper
from groq import Groq
from gtts import gTTS
import os

# Initialize Whisper model for transcription
model = whisper.load_model("base")

# Get API key from Hugging Face Spaces secrets
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
client = Groq(api_key=GROQ_API_KEY)




# Function to query the LLM using Groq API  (ye fx input receive krega or llm se trancribe krega)
def get_llm_response(input_text):
    chat_completion = client.chat.completions.create(
        messages=[{
            "role": "user",
            "content": input_text,
        }],
        model="llama3-8b-8192",
    )
    return chat_completion.choices[0].message.content


# Function to convert text to speech using gTTS
def text_to_speech(text,output_audio="output_audio.mp3"):
    tts = gTTS(text)
    tts.save(output_audio)
    return output_audio


def chatbot(audio):
  result=model.transcribe(audio)
  user_text=result['text']
  response_text=get_llm_response(user_text)
  output_audio=text_to_speech(response_text)
  return response_text,output_audio


# Create the chatbot interface
with gr.Blocks() as iface:
    # Title & Description
    gr.Markdown("# πŸŽ™οΈ AI Voice Chatbot")
    gr.Markdown("Speak into the microphone, and the AI will transcribe, process, and respond with both text and voice.")

    # Input Section
    with gr.Row():
        audio_input = gr.Audio(type="filepath", label="🎀 Speak", interactive=True, elem_id="box-style")

    # Output Section
    with gr.Row():
        text_output = gr.Textbox(label="πŸ’¬ AI Response", interactive=False, elem_id="box-style")

    with gr.Row():
        audio_output = gr.Audio(type="filepath", label="πŸ”Š AI Voice Response", elem_id="box-style")

    # Create a button for interaction
    submit_btn = gr.Button("πŸš€ Start Chat")
    submit_btn.click(fn=chatbot, inputs=audio_input, outputs=[text_output, audio_output])

    # Footer Section
    gr.Markdown("<hr>")  # Adds a line separator
    gr.Markdown("<p style='text-align: center; font-size: 14px;'>🌟 Developed by <b>Sheema Masood</b> | Built with <b>Gradio</b> 🌟</p>")


iface.launch()