From 60e4094e11e85a13c1656056f3d4bbd47587259d Mon Sep 17 00:00:00 2001 From: "shivani.aiindia@gmail.com" Date: Tue, 20 Oct 2026 16:57:09 +0530 Subject: [PATCH] speech-to-speech --- .gitignore | 2 + gemini_api.py | 87 ++++++++++++++++ medical_demo.py | 183 ++++++++++++++++++++++++++++++++++ medical_report.txt | 4 + property_data.json | 182 +++++++++++++++++++++++++++++++++ questions.json | 14 +++ requirements.txt | 4 + test.py | 244 +++++++++++++++++++++++++++++++++++++++++++++ test_voice.py | 53 ++++++++++ 9 files changed, 773 insertions(+) create mode 100644 .gitignore create mode 100644 gemini_api.py create mode 100644 medical_demo.py create mode 100644 medical_report.txt create mode 100644 property_data.json create mode 100644 questions.json create mode 100644 requirements.txt create mode 100644 test.py create mode 100644 test_voice.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9bec4dc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +venv/ \ No newline at end of file diff --git a/gemini_api.py b/gemini_api.py new file mode 100644 index 0000000..5ed58d4 --- /dev/null +++ b/gemini_api.py @@ -0,0 +1,87 @@ +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 diff --git a/medical_demo.py b/medical_demo.py new file mode 100644 index 0000000..f027b64 --- /dev/null +++ b/medical_demo.py @@ -0,0 +1,183 @@ +# import json + +# print("=" * 50) +# print("AI MEDICAL EXAMINER ASSISTANT") +# print("=" * 50) + +# # Simulated transcript +# transcript = input("\nEnter patient statement:\n> ") + +# text = transcript.lower() + +# detected_conditions = [] +# questions = [] + +# # Disease Question Bank +# question_bank = { +# "diabetes": [ +# "When were you diagnosed?", +# "What is your current HbA1c?", +# "Are you taking insulin?", +# "Any kidney disease?", +# "Any eye complications?" +# ], +# "hypertension": [ +# "Since when do you have hypertension?", +# "Current BP readings?", +# "Are you taking medication regularly?", +# "Any heart disease?", +# "Any stroke history?" +# ], +# "smoke": [ +# "How many cigarettes per day?", +# "For how many years?", +# "Current smoker or ex-smoker?", +# "Any chronic cough?", +# "Any COPD symptoms?" +# ] +# } + +# # Detect conditions +# if "diabetes" in text: +# detected_conditions.append("Diabetes") +# questions.extend(question_bank["diabetes"]) + +# if "hypertension" in text or "bp" in text: +# detected_conditions.append("Hypertension") +# questions.extend(question_bank["hypertension"]) + +# if "smoke" in text or "smoking" in text: +# detected_conditions.append("Smoking") +# questions.extend(question_bank["smoke"]) + +# print("\nDetected Conditions:") +# for condition in detected_conditions: +# print(f"āœ“ {condition}") + +# if not detected_conditions: +# print("No predefined condition detected.") +# exit() + +# print("\nSuggested Follow-Up Questions:") +# for i, q in enumerate(questions, start=1): +# print(f"{i}. {q}") + +# # Collect answers +# answers = {} + +# print("\nEnter Patient Answers:") +# for q in questions: +# answers[q] = input(f"{q} : ") + +# # Generate summary +# print("\n" + "=" * 50) +# print("FINAL MEDICAL SUMMARY") +# print("=" * 50) + +# print("\nPatient Statement:") +# print(transcript) + +# print("\nDetected Conditions:") +# for condition in detected_conditions: +# print(f"- {condition}") + +# print("\nResponses:") +# for question, answer in answers.items(): +# print(f"- {question}: {answer}") + +# print("\nMedical Completeness Score: 90%") +# print("Contradiction Score: Low") +# print("Additional Investigation: Recommended") + +# print("\nAreas Requiring Review:") +# for condition in detected_conditions: +# print(f"- {condition}") + +# print("\nReport Generated Successfully.") +# print("=" * 50) + +import os +from dotenv import load_dotenv +from elevenlabs.client import ElevenLabs + +load_dotenv() + +client = ElevenLabs( + api_key=os.getenv("ELEVENLABS_API_KEY") +) + +print("=" * 60) +print("AI MEDICAL EXAMINER ASSISTANT") +print("=" * 60) + +medical_questions = { + "diabetes": [ + "When were you diagnosed?", + "What medications are you taking?", + "Do you use insulin?", + "Any kidney disease?", + "Any eye complications?" + ], + "hypertension": [ + "Since when do you have hypertension?", + "What is your current BP reading?", + "Are you taking medications regularly?", + "Any heart disease?", + "Any stroke history?" + ], + "smoking": [ + "How many cigarettes per day?", + "For how many years have you smoked?", + "Current smoker or ex-smoker?", + "Any chronic cough?", + "Any COPD symptoms?" + ] +} + +answers = {} + +print("\nAI Doctor: Do you have diabetes? (yes/no)") +diabetes = input("Patient: ").strip().lower() + +if diabetes == "yes": + answers["Condition"] = "Diabetes" + + for question in medical_questions["diabetes"]: + print(f"\nAI Doctor: {question}") + response = input("Patient: ") + answers[question] = response + +print("\nAI Doctor: Do you have hypertension? (yes/no)") +hypertension = input("Patient: ").strip().lower() + +if hypertension == "yes": + answers["Hypertension"] = "Yes" + + for question in medical_questions["hypertension"]: + print(f"\nAI Doctor: {question}") + response = input("Patient: ") + answers[question] = response + +print("\nAI Doctor: Do you smoke? (yes/no)") +smoking = input("Patient: ").strip().lower() + +if smoking == "yes": + answers["Smoking"] = "Yes" + + for question in medical_questions["smoking"]: + print(f"\nAI Doctor: {question}") + response = input("Patient: ") + answers[question] = response + +print("\n" + "=" * 60) +print("FINAL MEDICAL REPORT") +print("=" * 60) + +for key, value in answers.items(): + print(f"{key}: {value}") + +print("\nMedical Completeness Score: 95%") +print("Contradiction Score: Low") +print("Additional Investigation Recommended: Yes") + +print("=" * 60) \ No newline at end of file diff --git a/medical_report.txt b/medical_report.txt new file mode 100644 index 0000000..dfd7ed5 --- /dev/null +++ b/medical_report.txt @@ -0,0 +1,4 @@ +FINAL MEDICAL REPORT +============================================================ + +Diabetes: diff --git a/property_data.json b/property_data.json new file mode 100644 index 0000000..b51fa5d --- /dev/null +++ b/property_data.json @@ -0,0 +1,182 @@ +[ + { + "name": "Sunrise Villas", + "location": "Pune", + "type": "Villa", + "price": "1.2 Cr" + }, + { + "name": "Maple Heights", + "location": "Mumbai", + "type": "2BHK Apartment", + "price": "85 Lakhs" + }, + { + "name": "Ocean Residency", + "location": "Goa", + "type": "Beach House", + "price": "2.5 Cr" + }, + { + "name": "Skyline Towers", + "location": "Bangalore", + "type": "3BHK Apartment", + "price": "1.05 Cr" + }, + { + "name": "Green Acres", + "location": "Hyderabad", + "type": "Independent House", + "price": "95 Lakhs" + }, + { + "name": "Palm Meadows", + "location": "Chennai", + "type": "Luxury Villa", + "price": "3 Cr" + }, + { + "name": "Cityscape Heights", + "location": "Delhi", + "type": "Studio Apartment", + "price": "45 Lakhs" + }, + { + "name": "Lakeview Residences", + "location": "Udaipur", + "type": "Lakefront Villa", + "price": "1.8 Cr" + }, + { + "name": "Tech Park Homes", + "location": "Noida", + "type": "2BHK Apartment", + "price": "70 Lakhs" + }, + { + "name": "Royal Heritage", + "location": "Jaipur", + "type": "Heritage Villa", + "price": "2.75 Cr" + }, + { + "name": "Emerald Enclave", + "location": "Ahmedabad", + "type": "3BHK Apartment", + "price": "1.2 Cr" + }, + { + "name": "The Urban Nest", + "location": "Kolkata", + "type": "2BHK Apartment", + "price": "60 Lakhs" + }, + { + "name": "Riverfront Meadows", + "location": "Surat", + "type": "Villa", + "price": "1.4 Cr" + }, + { + "name": "Hilltop Residency", + "location": "Shimla", + "type": "Cottage", + "price": "1.1 Cr" + }, + { + "name": "Blue Orchid Homes", + "location": "Nagpur", + "type": "4BHK Independent House", + "price": "88 Lakhs" + }, + { + "name": "Prestige Panorama", + "location": "Chandigarh", + "type": "Penthouse", + "price": "2 Cr" + }, + { + "name": "Golden Leaf Apartments", + "location": "Indore", + "type": "2BHK Apartment", + "price": "52 Lakhs" + }, + { + "name": "Whispering Woods", + "location": "Dehradun", + "type": "Cottage", + "price": "93 Lakhs" + }, + { + "name": "City View Heights", + "location": "Thane", + "type": "3BHK Apartment", + "price": "1.1 Cr" + }, + { + "name": "Silver Sands Residency", + "location": "Alibaug", + "type": "Beach Villa", + "price": "3.2 Cr" + }, + { + "name": "Eco Green Apartments", + "location": "Bhopal", + "type": "2BHK Apartment", + "price": "48 Lakhs" + }, + { + "name": "Royal Gardenia", + "location": "Mysore", + "type": "3BHK Independent House", + "price": "83 Lakhs" + }, + { + "name": "Infinity Towers", + "location": "Faridabad", + "type": "High-rise Apartment", + "price": "92 Lakhs" + }, + { + "name": "Casa Del Sol", + "location": "Pondicherry", + "type": "Beach House", + "price": "2.1 Cr" + }, + { + "name": "Galaxy Heights", + "location": "Lucknow", + "type": "3BHK Apartment", + "price": "74 Lakhs" + }, + { + "name": "Green Field Residences", + "location": "Rajkot", + "type": "Row House", + "price": "65 Lakhs" + }, + { + "name": "Pearl Regency", + "location": "Jodhpur", + "type": "Heritage Home", + "price": "2 Cr" + }, + { + "name": "Rainforest Retreat", + "location": "Wayanad", + "type": "Eco-Villa", + "price": "1.3 Cr" + }, + { + "name": "Sky High Residency", + "location": "Goa", + "type": "Penthouse", + "price": "3.5 Cr" + }, + { + "name": "Palm Avenue", + "location": "Goa", + "type": "Villa", + "price": "1.6 Cr" + } +] \ No newline at end of file diff --git a/questions.json b/questions.json new file mode 100644 index 0000000..2383997 --- /dev/null +++ b/questions.json @@ -0,0 +1,14 @@ +{ + "diabetes": [ + "When were you diagnosed?", + "What medications are you taking?", + "Do you use insulin?", + "Any kidney disease?", + "Any eye complications?" + ], + "hypertension": [ + "Since when do you have hypertension?", + "What is your current BP reading?", + "Are you taking medications regularly?" + ] +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c6ca895 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +speechrecognition +pyttsx3 +google-generativeai +pyaudio \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..8970088 --- /dev/null +++ b/test.py @@ -0,0 +1,244 @@ +# import json +# import speech_recognition as sr +# from elevenlabs.client import ElevenLabs +# from elevenlabs.play import play + +# # ----------------------- +# # ElevenLabs +# # ----------------------- + +# client = ElevenLabs( +# api_key="sk_a5001c59bb470cefd481dc0fbdc1f3b1e1b5e2dd057929ea" +# ) + +# # ----------------------- +# # Voice Functions +# # ----------------------- + +# def doctor_speak(text): +# print(f"\nDoctor: {text}") + +# audio = client.text_to_speech.convert( +# text=text, +# voice_id="JBFqnCBsd6RMkjVDRZzb", +# model_id="eleven_multilingual_v2", +# output_format="mp3_44100_128", +# ) + +# play(audio) + + +# def patient_listen(): +# recognizer = sr.Recognizer() + +# with sr.Microphone() as source: +# print("\nšŸŽ¤ Speak now...") +# recognizer.adjust_for_ambient_noise(source, duration=1) + +# audio = recognizer.listen(source) + +# try: +# text = recognizer.recognize_google(audio) + +# print(f"\nPatient: {text}") + +# return text + +# except Exception: +# print("\nCould not understand speech") +# return "" + + +# # ----------------------- +# # Load Questions +# # ----------------------- + +# with open("questions.json", "r") as f: +# questions = json.load(f) + +# report = {} + +# # ----------------------- +# # Start Examination +# # ----------------------- + +# doctor_speak( +# "Welcome to the AI Insurance Medical Examination." +# ) + +# doctor_speak( +# "Do you have diabetes? Please answer yes or no." +# ) + +# answer = patient_listen().lower() + +# report["Diabetes"] = answer + +# if "yes" in answer: + +# for question in questions["diabetes"]: + +# doctor_speak(question) + +# response = patient_listen() + +# report[question] = response + +# # ----------------------- +# # Final Summary +# # ----------------------- + +# doctor_speak( +# "Thank you. The examination is complete." +# ) + +# print("\n" + "=" * 60) +# print("FINAL MEDICAL REPORT") +# print("=" * 60) + +# for key, value in report.items(): +# print(f"{key}: {value}") + +# print("=" * 60) + +# with open("medical_report.txt", "w") as f: + +# f.write("FINAL MEDICAL REPORT\n") +# f.write("=" * 60 + "\n\n") + +# for key, value in report.items(): +# f.write(f"{key}: {value}\n") + +# print("\nReport saved as medical_report.txt") + +import json +import speech_recognition as sr +from elevenlabs.client import ElevenLabs +from elevenlabs.play import play + +# ElevenLabs +client = ElevenLabs( + api_key="sk_a5001c59bb470cefd481dc0fbdc1f3b1e1b5e2dd057929ea" +) + +# Load Questions +with open("questions.json", "r") as f: + questions = json.load(f) + +report = {} + +def doctor_speak(text): + print(f"\nšŸ‘Øā€āš•ļø Doctor: {text}") + + audio = client.text_to_speech.convert( + text=text, + voice_id="JBFqnCBsd6RMkjVDRZzb", + model_id="eleven_multilingual_v2", + output_format="mp3_44100_128", + ) + + play(audio) + + +def listen(): + r = sr.Recognizer() + + with sr.Microphone() as source: + + print("\nšŸŽ¤ Speak now...") + + audio = r.listen(source) + + try: + text = r.recognize_google(audio) + + print("šŸ§‘ Patient:", text) + + return text + + except: + return "" + + +# Start Interview + +doctor_speak( + "Welcome to the AI Insurance Medical Examination." +) + +doctor_speak( + "Do you have diabetes? Please answer yes or no." +) + +answer = listen().lower() + +report["Diabetes"] = answer + +if "yes" in answer: + + for q in questions["diabetes"]: + + doctor_speak(q) + + response = listen() + + report[q] = response + + +doctor_speak( + "Do you have hypertension? Please answer yes or no." +) + +answer = listen().lower() + +report["Hypertension"] = answer + +if "yes" in answer: + + for q in questions["hypertension"]: + + doctor_speak(q) + + response = listen() + + report[q] = response + + +doctor_speak( + "Do you smoke? Please answer yes or no." +) + +answer = listen().lower() + +report["Smoking"] = answer + +if "yes" in answer: + + for q in questions["smoking"]: + + doctor_speak(q) + + response = listen() + + report[q] = response + + +doctor_speak( + "Thank you. Examination completed." +) + +print("\n" + "=" * 60) +print("FINAL MEDICAL REPORT") +print("=" * 60) + +for k, v in report.items(): + print(f"{k}: {v}") + +with open("medical_report.txt", "w") as f: + + f.write("FINAL MEDICAL REPORT\n\n") + + for k, v in report.items(): + f.write(f"{k}: {v}\n") + +print("\nReport Saved -> medical_report.txt") \ No newline at end of file diff --git a/test_voice.py b/test_voice.py new file mode 100644 index 0000000..a4e46d4 --- /dev/null +++ b/test_voice.py @@ -0,0 +1,53 @@ +import json +from elevenlabs.client import ElevenLabs +from elevenlabs.play import play + +# Add your API key +client = ElevenLabs( + api_key="sk_a5001c59bb470cefd481dc0fbdc1f3b1e1b5e2dd057929ea" +) + +def doctor_speak(text): + print(f"\nDoctor: {text}") + + audio = client.text_to_speech.convert( + text=text, + voice_id="JBFqnCBsd6RMkjVDRZzb", + model_id="eleven_multilingual_v2", + output_format="mp3_44100_128", + ) + + play(audio) + +# Load questions +with open("questions.json", "r") as f: + questions = json.load(f) + +answers = {} + +doctor_speak("Welcome to the insurance medical examination.") + +doctor_speak("Do you have diabetes? Please answer yes or no.") + +response = input("Patient: ").strip().lower() + +answers["Diabetes"] = response + +if response == "yes": + + for question in questions["diabetes"]: + + doctor_speak(question) + + answer = input("Patient: ") + + answers[question] = answer + +doctor_speak("Thank you. The examination is complete.") + +print("\n" + "=" * 60) +print("FINAL MEDICAL REPORT") +print("=" * 60) + +for key, value in answers.items(): + print(f"{key}: {value}") \ No newline at end of file