|
@@ -1,127 +1,127 @@
|
|
|
-import pygame
|
|
|
-import speech_recognition as sr
|
|
|
-from openai import OpenAI
|
|
|
-from pathlib import Path
|
|
|
-import time
|
|
|
-import os
|
|
|
-
|
|
|
-# Initialize OpenAI API key
|
|
|
-api_key = 'sk-proj-wwWaxim1Qt13uqzSS0xjT3BlbkFJK0rZvx78AJiWG3Ot7d3S'
|
|
|
-client = OpenAI(api_key=api_key)
|
|
|
-
|
|
|
-def create_messages(question, file_content):
|
|
|
- return [
|
|
|
- {"role": "system", "content": "You are a tour guide who explains and answers about IoT use cases in Vodafone."},
|
|
|
- {"role": "user", "content": f"{file_content}\n\nQ: {question}\nA:"}
|
|
|
- ]
|
|
|
-
|
|
|
-def user_input():
|
|
|
- while True:
|
|
|
- try:
|
|
|
- num = int(input("Enter a number from 1 to 3: "))
|
|
|
- if 1 <= num <= 3:
|
|
|
- return num
|
|
|
- else:
|
|
|
- print("Invalid input. Please enter a number between 1 and 3.")
|
|
|
- except ValueError:
|
|
|
- print("Invalid input. Please enter a valid integer.")
|
|
|
-
|
|
|
-def play_audio(num):
|
|
|
- audio_files = {
|
|
|
- 1: "speech1.mp3",
|
|
|
- 2: "hydrosense.mp3",
|
|
|
- 3: "Pushtotalk.mp3",
|
|
|
- }
|
|
|
-
|
|
|
- audio_file = audio_files.get(num)
|
|
|
-
|
|
|
- if audio_file:
|
|
|
- pygame.mixer.init()
|
|
|
- pygame.mixer.music.load(audio_file)
|
|
|
- pygame.mixer.music.play()
|
|
|
- while pygame.mixer.music.get_busy():
|
|
|
- time.sleep(1)
|
|
|
-
|
|
|
-def recognize_speech():
|
|
|
- recognizer = sr.Recognizer()
|
|
|
- with sr.Microphone() as source:
|
|
|
- print("Listening...")
|
|
|
- audio = recognizer.listen(source)
|
|
|
- try:
|
|
|
- print("Recognizing...")
|
|
|
- text = recognizer.recognize_google(audio, language='en-US')
|
|
|
- print(f"You said: {text}")
|
|
|
- return text
|
|
|
- except sr.UnknownValueError:
|
|
|
- print("Sorry, I did not understand that.")
|
|
|
- return None
|
|
|
- except sr.RequestError:
|
|
|
- print("Sorry, there was an error with the speech recognition service.")
|
|
|
- return None
|
|
|
-
|
|
|
-def get_response_from_openai(messages):
|
|
|
- response = client.chat.completions.create(
|
|
|
- model="gpt-4o",
|
|
|
- messages=messages,
|
|
|
- max_tokens=75,
|
|
|
- temperature=0.5,
|
|
|
- )
|
|
|
- return (response.choices[0].message.content)
|
|
|
-
|
|
|
-def read_text_file(file_path):
|
|
|
- with open(file_path, 'r') as file:
|
|
|
- return file.read()
|
|
|
-
|
|
|
-def generate_speech(text, file_path):
|
|
|
- speech_file_path = Path(file_path).parent / "speech.mp3"
|
|
|
- with client.audio.speech.with_streaming_response.create(
|
|
|
- model="tts-1", # Replace with your actual model ID
|
|
|
- input=text,
|
|
|
- voice="alloy" # Replace with a valid voice for your chosen model
|
|
|
- ) as response:
|
|
|
- response.stream_to_file(str(speech_file_path))
|
|
|
- return str(speech_file_path)
|
|
|
-
|
|
|
-def start_qa_mode(file_content):
|
|
|
- while True:
|
|
|
- print("Please ask your question:")
|
|
|
- question = recognize_speech()
|
|
|
- if question and question.lower() in ["no", "go to next showcase", "exit"]:
|
|
|
- break
|
|
|
- if question:
|
|
|
- messages = create_messages(question, file_content)
|
|
|
- answer = get_response_from_openai(messages)
|
|
|
- print(f"Answer: {answer}")
|
|
|
- speech_file_path = generate_speech(answer, "speech.mp3")
|
|
|
- pygame.mixer.init()
|
|
|
- pygame.mixer.music.load(speech_file_path)
|
|
|
- pygame.mixer.music.play()
|
|
|
- while pygame.mixer.music.get_busy():
|
|
|
- pygame.time.Clock().tick(10) # Adjust as needed
|
|
|
- pygame.mixer.music.stop() # Ensure music playback stops
|
|
|
- pygame.mixer.quit() # Release resources
|
|
|
- os.remove(speech_file_path)
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- else:
|
|
|
- print("Sorry, I didn't get that. Please ask again.")
|
|
|
-
|
|
|
-def main():
|
|
|
- while True:
|
|
|
- num = user_input()
|
|
|
- play_audio(num)
|
|
|
-
|
|
|
- text_files = {
|
|
|
- 1: "Gigabeeprotect.txt",
|
|
|
- 2: "Hydrosense.txt",
|
|
|
- 3: "Pushtotalk.txt",
|
|
|
- }
|
|
|
-
|
|
|
- text_file = text_files.get(num)
|
|
|
- if text_file:
|
|
|
- file_content = read_text_file(text_file)
|
|
|
- start_qa_mode(file_content)
|
|
|
-
|
|
|
-if __name__ == "__main__":
|
|
|
- main()
|
|
|
+import pygame
|
|
|
+import speech_recognition as sr
|
|
|
+from openai import OpenAI
|
|
|
+from pathlib import Path
|
|
|
+import time
|
|
|
+import os
|
|
|
+
|
|
|
+# Initialize OpenAI API key
|
|
|
+api_key = 'sk-proj-wwWaxim1Qt13uq45e45rtfgzSS0xjT3BlbkFJK0rZvx78AJiWG3Ot7d3S'
|
|
|
+client = OpenAI(api_key=api_key)
|
|
|
+
|
|
|
+def create_messages(question, file_content):
|
|
|
+ return [
|
|
|
+ {"role": "system", "content": "You are a tour guide who explains and answers about IoT use cases in Vodafone."},
|
|
|
+ {"role": "user", "content": f"{file_content}\n\nQ: {question}\nA:"}
|
|
|
+ ]
|
|
|
+
|
|
|
+def user_input():
|
|
|
+ while True:
|
|
|
+ try:
|
|
|
+ num = int(input("Enter a number from 1 to 3: "))
|
|
|
+ if 1 <= num <= 3:
|
|
|
+ return num
|
|
|
+ else:
|
|
|
+ print("Invalid input. Please enter a number between 1 and 3.")
|
|
|
+ except ValueError:
|
|
|
+ print("Invalid input. Please enter a valid integer.")
|
|
|
+
|
|
|
+def play_audio(num):
|
|
|
+ audio_files = {
|
|
|
+ 1: "speech1.mp3",
|
|
|
+ 2: "hydrosense.mp3",
|
|
|
+ 3: "Pushtotalk.mp3",
|
|
|
+ }
|
|
|
+
|
|
|
+ audio_file = audio_files.get(num)
|
|
|
+
|
|
|
+ if audio_file:
|
|
|
+ pygame.mixer.init()
|
|
|
+ pygame.mixer.music.load(audio_file)
|
|
|
+ pygame.mixer.music.play()
|
|
|
+ while pygame.mixer.music.get_busy():
|
|
|
+ time.sleep(1)
|
|
|
+
|
|
|
+def recognize_speech():
|
|
|
+ recognizer = sr.Recognizer()
|
|
|
+ with sr.Microphone() as source:
|
|
|
+ print("Listening...")
|
|
|
+ audio = recognizer.listen(source)
|
|
|
+ try:
|
|
|
+ print("Recognizing...")
|
|
|
+ text = recognizer.recognize_google(audio, language='en-US')
|
|
|
+ print(f"You said: {text}")
|
|
|
+ return text
|
|
|
+ except sr.UnknownValueError:
|
|
|
+ print("Sorry, I did not understand that.")
|
|
|
+ return None
|
|
|
+ except sr.RequestError:
|
|
|
+ print("Sorry, there was an error with the speech recognition service.")
|
|
|
+ return None
|
|
|
+
|
|
|
+def get_response_from_openai(messages):
|
|
|
+ response = client.chat.completions.create(
|
|
|
+ model="gpt-4o",
|
|
|
+ messages=messages,
|
|
|
+ max_tokens=75,
|
|
|
+ temperature=0.5,
|
|
|
+ )
|
|
|
+ return (response.choices[0].message.content)
|
|
|
+
|
|
|
+def read_text_file(file_path):
|
|
|
+ with open(file_path, 'r') as file:
|
|
|
+ return file.read()
|
|
|
+
|
|
|
+def generate_speech(text, file_path):
|
|
|
+ speech_file_path = Path(file_path).parent / "speech.mp3"
|
|
|
+ with client.audio.speech.with_streaming_response.create(
|
|
|
+ model="tts-1", # Replace with your actual model ID
|
|
|
+ input=text,
|
|
|
+ voice="alloy" # Replace with a valid voice for your chosen model
|
|
|
+ ) as response:
|
|
|
+ response.stream_to_file(str(speech_file_path))
|
|
|
+ return str(speech_file_path)
|
|
|
+
|
|
|
+def start_qa_mode(file_content):
|
|
|
+ while True:
|
|
|
+ print("Please ask your question:")
|
|
|
+ question = recognize_speech()
|
|
|
+ if question and question.lower() in ["no", "go to next showcase", "exit"]:
|
|
|
+ break
|
|
|
+ if question:
|
|
|
+ messages = create_messages(question, file_content)
|
|
|
+ answer = get_response_from_openai(messages)
|
|
|
+ print(f"Answer: {answer}")
|
|
|
+ speech_file_path = generate_speech(answer, "speech.mp3")
|
|
|
+ pygame.mixer.init()
|
|
|
+ pygame.mixer.music.load(speech_file_path)
|
|
|
+ pygame.mixer.music.play()
|
|
|
+ while pygame.mixer.music.get_busy():
|
|
|
+ pygame.time.Clock().tick(10) # Adjust as needed
|
|
|
+ pygame.mixer.music.stop() # Ensure music playback stops
|
|
|
+ pygame.mixer.quit() # Release resources
|
|
|
+ os.remove(speech_file_path)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ else:
|
|
|
+ print("Sorry, I didn't get that. Please ask again.")
|
|
|
+
|
|
|
+def main():
|
|
|
+ while True:
|
|
|
+ num = user_input()
|
|
|
+ play_audio(num)
|
|
|
+
|
|
|
+ text_files = {
|
|
|
+ 1: "Gigabeeprotect.txt",
|
|
|
+ 2: "Hydrosense.txt",
|
|
|
+ 3: "Pushtotalk.txt",
|
|
|
+ }
|
|
|
+
|
|
|
+ text_file = text_files.get(num)
|
|
|
+ if text_file:
|
|
|
+ file_content = read_text_file(text_file)
|
|
|
+ start_qa_mode(file_content)
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ main()
|