Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[ ]:
|
5 |
+
|
6 |
+
|
7 |
+
import google.generativeai as genai
|
8 |
+
import speech_recognition as sr
|
9 |
+
import pyttsx3
|
10 |
+
import streamlit as st
|
11 |
+
import tempfile
|
12 |
+
import os
|
13 |
+
|
14 |
+
# Configure the Gemini API
|
15 |
+
genai.configure(api_key="AIzaSyCiaN2sysFQdEvucNdTawb0LgjdDSwcVN4")
|
16 |
+
|
17 |
+
# Initialize Text-to-Speech
|
18 |
+
engine = pyttsx3.init()
|
19 |
+
|
20 |
+
# Function to make the assistant speak
|
21 |
+
def speak(text):
|
22 |
+
engine.say(text)
|
23 |
+
engine.runAndWait()
|
24 |
+
|
25 |
+
# Define the assistant's prompt
|
26 |
+
prompt = f"""You are a Humanoid Assistant named Kiki, designed to assist humans in their daily tasks through intuitive commands and routines.
|
27 |
+
Be sure to respond in a complete sentence, being comprehensive, including all relevant background information.
|
28 |
+
However, you are talking to a non-technical audience, so be sure to break down complicated concepts and
|
29 |
+
strike a friendly and conversational tone. If the passage is irrelevant to the answer, you may ignore it."""
|
30 |
+
|
31 |
+
# Function to interact with Gemini
|
32 |
+
def ask_gemini(query):
|
33 |
+
try:
|
34 |
+
full_query = prompt + "\n\nUser: " + query + "\nAssistant:"
|
35 |
+
model = genai.GenerativeModel("gemini-1.5-flash-latest")
|
36 |
+
response = model.generate_content(full_query)
|
37 |
+
return response.text.strip()
|
38 |
+
except Exception as e:
|
39 |
+
return f"Error: {e}"
|
40 |
+
|
41 |
+
# Function for speech-to-text
|
42 |
+
def listen_to_speech(audio_file):
|
43 |
+
recognizer = sr.Recognizer()
|
44 |
+
with sr.AudioFile(audio_file) as source:
|
45 |
+
audio = recognizer.record(source)
|
46 |
+
try:
|
47 |
+
text = recognizer.recognize_google(audio)
|
48 |
+
return text
|
49 |
+
except sr.UnknownValueError:
|
50 |
+
return "Could not understand the audio"
|
51 |
+
except sr.RequestError:
|
52 |
+
return "Error connecting to Google Speech API"
|
53 |
+
|
54 |
+
# Streamlit UI
|
55 |
+
st.title("ποΈ AI Voice Assistant - Kiki")
|
56 |
+
|
57 |
+
st.write("Click the button below to record your voice and get a response.")
|
58 |
+
|
59 |
+
# Upload audio file (for testing)
|
60 |
+
uploaded_file = st.file_uploader("Upload a WAV file for testing", type=["wav"])
|
61 |
+
|
62 |
+
if uploaded_file:
|
63 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
|
64 |
+
temp_file.write(uploaded_file.read())
|
65 |
+
temp_file_path = temp_file.name
|
66 |
+
user_text = listen_to_speech(temp_file_path)
|
67 |
+
os.remove(temp_file_path) # Delete temp file after processing
|
68 |
+
st.write("π You said:", user_text)
|
69 |
+
|
70 |
+
if user_text.lower() in ["exit", "stop", "quit", "i am done"]:
|
71 |
+
st.write("π Exiting chatbot.")
|
72 |
+
else:
|
73 |
+
response = ask_gemini(user_text)
|
74 |
+
st.write("π€ Gemini:", response)
|
75 |
+
speak(response)
|
76 |
+
|
77 |
+
# Button to use microphone input
|
78 |
+
if st.button("π€ Record Voice"):
|
79 |
+
st.write("Recording feature needs to be implemented for Streamlit.")
|
80 |
+
|
81 |
+
st.write("πΉ Developed with Streamlit & Gemini AI.")
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
+
|