File size: 2,819 Bytes
b85123b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
# coding: utf-8

# In[ ]:


import google.generativeai as genai
import speech_recognition as sr
import pyttsx3
import streamlit as st
import tempfile
import os

# Configure the Gemini API
genai.configure(api_key="AIzaSyCiaN2sysFQdEvucNdTawb0LgjdDSwcVN4")

# Initialize Text-to-Speech
engine = pyttsx3.init()

# Function to make the assistant speak
def speak(text):
    engine.say(text)
    engine.runAndWait()

# Define the assistant's prompt
prompt = f"""You are a Humanoid Assistant named Kiki, designed to assist humans in their daily tasks through intuitive commands and routines.

Be sure to respond in a complete sentence, being comprehensive, including all relevant background information.

However, you are talking to a non-technical audience, so be sure to break down complicated concepts and

strike a friendly and conversational tone. If the passage is irrelevant to the answer, you may ignore it."""

# Function to interact with Gemini
def ask_gemini(query):
    try:
        full_query = prompt + "\n\nUser: " + query + "\nAssistant:"
        model = genai.GenerativeModel("gemini-1.5-flash-latest")
        response = model.generate_content(full_query)
        return response.text.strip()
    except Exception as e:
        return f"Error: {e}"

# Function for speech-to-text
def listen_to_speech(audio_file):
    recognizer = sr.Recognizer()
    with sr.AudioFile(audio_file) as source:
        audio = recognizer.record(source)
    try:
        text = recognizer.recognize_google(audio)
        return text
    except sr.UnknownValueError:
        return "Could not understand the audio"
    except sr.RequestError:
        return "Error connecting to Google Speech API"

# Streamlit UI
st.title("πŸŽ™οΈ AI Voice Assistant - Kiki")

st.write("Click the button below to record your voice and get a response.")

# Upload audio file (for testing)
uploaded_file = st.file_uploader("Upload a WAV file for testing", type=["wav"])

if uploaded_file:
    with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
        temp_file.write(uploaded_file.read())
        temp_file_path = temp_file.name
    user_text = listen_to_speech(temp_file_path)
    os.remove(temp_file_path)  # Delete temp file after processing
    st.write("πŸ“ You said:", user_text)

    if user_text.lower() in ["exit", "stop", "quit", "i am done"]:
        st.write("πŸ‘‹ Exiting chatbot.")
    else:
        response = ask_gemini(user_text)
        st.write("πŸ€– Gemini:", response)
        speak(response)

# Button to use microphone input
if st.button("🎀 Record Voice"):
    st.write("Recording feature needs to be implemented for Streamlit.")

st.write("πŸ”Ή Developed with Streamlit & Gemini AI.")