testing.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import pygame
  2. import time
  3. from openai import OpenAI
  4. # Initialize OpenAI API key
  5. api_key = 'sk-proj-wwWaxim1Qt13uq5645dfghvbnzSS0xjT3BlbkFJK0rZvx78AJiWG3Ot7d3S'
  6. client = OpenAI(api_key=api_key)
  7. def user_input():
  8. while True:
  9. try:
  10. num = int(input("Enter a number from 1 to 3: "))
  11. if 1 <= num <= 3:
  12. return num
  13. else:
  14. print("Invalid input. Please enter a number between 1 and 3.")
  15. except ValueError:
  16. print("Invalid input. Please enter a valid integer.")
  17. def play_audio(num):
  18. audio_files = {
  19. 1: "Gigabee.mp3",
  20. 2: "hydrosense.mp3",
  21. 3: "Pushtotalk.mp3",
  22. }
  23. text_files = {
  24. 1: "Gigabeeprotect.txt",
  25. 2: "Hydrosense.txt",
  26. 3: "Pushtotalk.txt",
  27. }
  28. audio_file = audio_files.get(num)
  29. text_file = text_files.get(num)
  30. if audio_file and text_file:
  31. pygame.mixer.init()
  32. pygame.mixer.music.load(audio_file)
  33. pygame.mixer.music.play()
  34. while pygame.mixer.music.get_busy():
  35. time.sleep(1)
  36. # Prompt for Q&A mode
  37. user_choice = input("Would you like to enter Q&A mode? (yes/no): ").strip().lower()
  38. if user_choice == 'yes':
  39. with open(text_file, 'r') as file:
  40. context = file.read()
  41. start_qa_mode(context)
  42. def start_qa_mode(context):
  43. while True:
  44. question = input("Ask a question (or type 'exit' to quit): ").strip()
  45. if question.lower() == 'exit':
  46. break
  47. response = client.chat.completions.create(
  48. engine="text-davinci-003", # You can choose another engine if needed
  49. prompt=f"{context}\n\nQuestion: {question}\nAnswer:",
  50. max_tokens=150
  51. )
  52. answer = (response.choices[0].message.content)
  53. print(f"Answer: {answer}")
  54. if __name__ == "__main__":
  55. while True:
  56. number = user_input()
  57. play_audio(number)
  58. # Loop continues to ask for new user input after Q&A mode ends or if no Q&A mode is entered