#!/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.")