88 lines
2.3 KiB
Python
88 lines
2.3 KiB
Python
import speech_recognition as sr
|
|
import pyttsx3
|
|
import google.generativeai as genai
|
|
import json
|
|
|
|
# 🔐 Configure Gemini API key
|
|
genai.configure(api_key="AQ.Ab8RN6Ja_fjsA6xNxJLGPxCZJXfqmTdPzp-ATB5pAUveK7aoQw")
|
|
model = genai.GenerativeModel("gemini-2.5-flash")
|
|
|
|
|
|
# Loaded JSON File
|
|
with open("property_data.json", "r") as f:
|
|
property_data = json.load(f)
|
|
|
|
# Initialize TTS engine
|
|
engine = pyttsx3.init()
|
|
|
|
# Voice choice
|
|
print("Choose a voice:\n1. Male (David)\n2. Female (Zira)")
|
|
choice = input("Enter 1 or 2: ")
|
|
voices = engine.getProperty("voices")
|
|
if choice == "1":
|
|
for voice in voices:
|
|
if "David" in voice.name:
|
|
engine.setProperty("voice", voice.id)
|
|
break
|
|
elif choice == "2":
|
|
for voice in voices:
|
|
if "Zira" in voice.name:
|
|
engine.setProperty("voice", voice.id)
|
|
break
|
|
|
|
|
|
# Speak Function
|
|
def speak(text):
|
|
engine.say(text)
|
|
engine.runAndWait()
|
|
|
|
|
|
# Listen Function
|
|
def listen():
|
|
r = sr.Recognizer()
|
|
with sr.Microphone() as source:
|
|
print("🎤 Speak now:")
|
|
audio = r.listen(source)
|
|
try:
|
|
text = r.recognize_google(audio)
|
|
print("🧑 You:", text)
|
|
return text
|
|
except sr.UnknownValueError:
|
|
print("❌ Could not understand.")
|
|
speak("Sorry, I didn't catch that.")
|
|
return None
|
|
|
|
|
|
# Ask Gemini with Property Data
|
|
def ask_gemini_with_data(user_query, data):
|
|
context = "You are a real estate assistant. Here are the property listings:\n"
|
|
for p in data:
|
|
context += (
|
|
f"- {p['name']} in {p['location']} is a {p['type']} priced at {p['price']}"
|
|
)
|
|
prompt = context + f"\nUser question: {user_query}"
|
|
|
|
try:
|
|
response = model.generate_content(prompt)
|
|
return response.text
|
|
except Exception as e:
|
|
print("⚠️ Gemini Error:", e)
|
|
return "Sorry, I couldn't process that request."
|
|
|
|
|
|
# Loop
|
|
while True:
|
|
try:
|
|
question = listen()
|
|
if question:
|
|
if "exit" in question.lower() or "stop" in question.lower():
|
|
speak("Okay, goodbye!")
|
|
break
|
|
answer = ask_gemini_with_data(question, property_data)
|
|
print("🤖 Gemini:", answer)
|
|
speak(answer)
|
|
except KeyboardInterrupt:
|
|
print("Stopped.")
|
|
speak("Goodbye!")
|
|
break
|