Files
speech-to-speech/test.py
T
2026-10-20 16:57:09 +05:30

244 lines
4.4 KiB
Python

# 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")