File size: 2,409 Bytes
d8f254c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

# Import required modules
import whisper
import os
from groq import Groq
from gtts import gTTS
import gradio as gr

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

# Initialize Groq API client (replace with your actual API key)
GROQ_API_KEY = "your_groq_api_key_here"
client = Groq(api_key=GROQ_API_KEY)

# Function to transcribe audio using Whisper
def transcribe_audio(audio):
    # Transcribe the input audio file
    result = model.transcribe(audio)
    return result['text']

# Function to send the transcription to Groq API for UK Healthcare model response
def query_llm(text):
    # Adding a healthcare context to the prompt
    prompt = f"The user is asking about UK healthcare services. Answer accurately and provide information relevant to the UK's healthcare system, including NHS services. The question is: {text}"
    
    chat_completion = client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": prompt,
            }
        ],
        model="llama3-8b-8192",  # Specify the model here
    )
    return chat_completion.choices[0].message.content

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

# Gradio function that ties everything together for UK healthcare chatbot
def healthcare_voice_to_voice(audio):
    # Step 1: Transcribe the voice input
    transcription = transcribe_audio(audio)
    
    # Step 2: Send transcription to Groq API with UK healthcare context
    llm_response = query_llm(transcription)
    
    # Step 3: Convert LLM response to speech
    response_audio = text_to_speech(llm_response)
    
    return response_audio

# Define custom CSS for the background color and heading
custom_css = """
    body {
        background-color: grey;
    }
    h1 {
        text-align: center;
        color: white;
        font-size: 30px;
    }
"""

# Gradio interface for UK healthcare chatbot with a custom title and theme
gr.Interface(
    fn=healthcare_voice_to_voice,
    inputs=gr.Audio(type="filepath"),  # For audio input from file or microphone
    outputs=gr.Audio(type="filepath"),  # For audio output as a file
    title="UK Healthcare ChatBot",  # Add a title for the chatbot
    css=custom_css  # Apply the custom CSS for styling
).launch()