File size: 3,666 Bytes
5ac6755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import speech_recognition as sr
import pyttsx3
import requests

# Set your Mistral API key
MISTRAL_API_KEY = "PdUU4XtVP6juMrwpHtBYC4tbv49hjCJg"
MISTRAL_API_URL = "https://api.mistral.ai/v1/chat/completions"

# Predefined responses
predefined_responses = {
    "What should we know about your life story in a few sentences": 
        "I have a background in Computer Science with a passion for AI and Machine Learning. I've worked on projects in NLP and Computer Vision, and I'm constantly learning and growing in this space.",
    "What’s your number one superpower": 
        "My #1 superpower is adaptability. I can quickly learn new concepts, adapt to different work environments, and solve complex problems effectively.",
    "What are the top three areas you would like to grow in": 
        "I’d like to improve my expertise in large-scale AI model deployment, learn more about reinforcement learning, and enhance my leadership skills in AI teams.",
    "What misconception do your coworkers have about you": 
        "A common misconception is that I prefer working alone. In reality, I enjoy teamwork and collaborating to solve problems efficiently.",
    "How do you push your boundaries and limits": 
        "I challenge myself by taking on new, difficult projects and setting ambitious learning goals. I also stay updated with the latest advancements in AI and actively contribute to open-source projects."
}

def speech_to_text():
    """Convert speech to text using SpeechRecognition"""
    recognizer = sr.Recognizer()
    with sr.Microphone() as source:
        st.info("Listening...")
        recognizer.adjust_for_ambient_noise(source)
        audio = recognizer.listen(source)

    try:
        text = recognizer.recognize_google(audio)
        st.success(f"You said: {text}")
        return text
    except sr.UnknownValueError:
        st.error("Sorry, I couldn't understand the audio.")
        return None
    except sr.RequestError:
        st.error("Could not request results, please check your internet connection.")
        return None

def get_response(user_input):
    """Get bot response from predefined answers or Mistral API"""
    user_input = user_input.lower().strip()

    for question, response in predefined_responses.items():
        if question.lower().strip() == user_input:
            return response

    headers = {"Authorization": f"Bearer {MISTRAL_API_KEY}", "Content-Type": "application/json"}
    data = {"model": "mistral-tiny", "messages": [{"role": "user", "content": user_input}]}
    response = requests.post(MISTRAL_API_URL, headers=headers, json=data)

    if response.status_code == 200:
        return response.json()["choices"][0]["message"]["content"]
    else:
        return "Error: Unable to fetch response from Mistral API."

def text_to_speech(response_text):
    """Speak the response synchronously"""
    engine = pyttsx3.init()
    engine.setProperty('rate', 150)
    engine.say(response_text)
    engine.runAndWait()

# Streamlit UI
st.title("🎤 AI Voice Bot")

st.write("Click the button below and speak.")

if "conversation" not in st.session_state:
    st.session_state.conversation = []

if st.button("Speak Now"):
    user_input = speech_to_text()
    if user_input:
        bot_response = get_response(user_input)
        
        st.session_state.conversation.append(("You", user_input))
        st.session_state.conversation.append(("Bot", bot_response))

        for speaker, message in st.session_state.conversation:
            st.subheader(f"**{speaker}:**")
            st.write(message)

        # Speak the response
        text_to_speech(bot_response)