Full.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import pygame
  2. import speech_recognition as sr
  3. from openai import OpenAI
  4. from pathlib import Path
  5. import time
  6. # Initialize OpenAI API key
  7. api_key = 'sk-proj-wwWaxim1Qt13uqzSS0xjT3BlbkFJKtfftgh0rZvx78AJiWG3Ot7d3S'
  8. client = OpenAI(api_key=api_key)
  9. def create_messages(question, file_content):
  10. return [
  11. {"role": "system", "content": "You are a helpful assistant who explains and answers about IoT use cases in Vodafone."},
  12. {"role": "user", "content": f"{file_content}\n\nQ: {question}\nA:"}
  13. ]
  14. def user_input():
  15. while True:
  16. try:
  17. num = int(input("Enter a number from 1 to 3: "))
  18. if 1 <= num <= 3:
  19. return num
  20. else:
  21. print("Invalid input. Please enter a number between 1 and 3.")
  22. except ValueError:
  23. print("Invalid input. Please enter a valid integer.")
  24. def play_audio(num):
  25. audio_files = {
  26. 1: "Gigabee.mp3",
  27. 2: "hydrosense.mp3",
  28. 3: "Pushtotalk.mp3",
  29. }
  30. audio_file = audio_files.get(num)
  31. if audio_file:
  32. pygame.mixer.init()
  33. pygame.mixer.music.load(audio_file)
  34. pygame.mixer.music.play()
  35. while pygame.mixer.music.get_busy():
  36. time.sleep(1)
  37. def recognize_speech():
  38. recognizer = sr.Recognizer()
  39. with sr.Microphone() as source:
  40. print("Listening...")
  41. audio = recognizer.listen(source)
  42. try:
  43. print("Recognizing...")
  44. text = recognizer.recognize_google(audio, language='en-US')
  45. print(f"You said: {text}")
  46. return text
  47. except sr.UnknownValueError:
  48. print("Sorry, I did not understand that.")
  49. return None
  50. except sr.RequestError:
  51. print("Sorry, there was an error with the speech recognition service.")
  52. return None
  53. def get_response_from_openai(messages):
  54. response = client.chat.completions.create(
  55. model="gpt-3.5-turbo",
  56. messages=messages,
  57. max_tokens=150,
  58. temperature=0.5,
  59. )
  60. return (response.choices[0].message.content)
  61. def read_text_file(file_path):
  62. with open(file_path, 'r') as file:
  63. return file.read()
  64. def generate_speech(text, file_path):
  65. speech_file_path = Path(file_path).parent / "speech.mp3"
  66. with client.audio.speech.with_streaming_response.create(
  67. model="tts-1", # Replace with your actual model ID
  68. input=text,
  69. voice="alloy" # Replace with a valid voice for your chosen model
  70. ) as response:
  71. response.stream_to_file(str(speech_file_path))
  72. return str(speech_file_path)
  73. def start_qa_mode(file_content):
  74. while True:
  75. print("Please ask your question:")
  76. question = recognize_speech()
  77. if question:
  78. messages = create_messages(question, file_content)
  79. answer = get_response_from_openai(messages)
  80. print(f"Answer: {answer}")
  81. speech_file_path = generate_speech(answer, "speech.mp3")
  82. pygame.mixer.music.load(speech_file_path)
  83. pygame.mixer.music.play()
  84. while pygame.mixer.music.get_busy():
  85. time.sleep(1)
  86. print("Do you want to ask another question? (Yes/No)")
  87. user_choice = recognize_speech()
  88. if user_choice and user_choice.lower() == "no":
  89. break
  90. else:
  91. print("Sorry, I didn't get that. Please ask again.")
  92. def main():
  93. while True:
  94. num = user_input()
  95. play_audio(num)
  96. text_files = {
  97. 1: "Gigabeeprotect.txt",
  98. 2: "Hydrosense.txt",
  99. 3: "Pushtotalk.txt",
  100. }
  101. text_file = text_files.get(num)
  102. if text_file:
  103. file_content = read_text_file(text_file)
  104. start_qa_mode(file_content)
  105. if __name__ == "__main__":
  106. main()