Advertisement
Steadyspaghetti

Untitled

Feb 1st, 2025
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | Software | 0 0
  1. #!/usr/bin/env python3
  2. import os
  3. import json
  4. import time
  5. from openai import OpenAI
  6. import sounddevice as sd
  7. import soundfile as sf
  8.  
  9. # Create LM Studio client (using the externally reachable address)
  10. client = OpenAI(base_url="http://26.133.56.123:1234/v1", api_key="lm-studio")
  11. model = "lmstudio-community/qwen2.5-7B-Instruct-Q4_K_M.gguf"
  12.  
  13. # Create a separate client for your TTS server (covas-next-aiserver)
  14. tts_client = OpenAI(base_url="http://192.168.40.218:8080", api_key="-")
  15.  
  16. def generate_speech(text: str) -> str:
  17. """
  18. Generate speech from text using the TTS server.
  19. This calls the /audio/speech endpoint of the covas-next-aiserver.
  20. The generated audio is saved to 'output.wav'.
  21. """
  22. print(f"[generate_speech] Sending text to TTS server: {text}")
  23. try:
  24. response = tts_client.audio.speech.create(
  25. model="-", # Use default or configured model on your TTS server
  26. input=text, # The text to be converted into speech.
  27. voice="nova", # Specify the voice; adjust if needed.
  28. speed=1.0, # Speech speed.
  29. response_format="wav" # Request a WAV audio response.
  30. )
  31. if response and hasattr(response, "content") and isinstance(response.content, bytes):
  32. with open("output.wav", "wb") as f:
  33. f.write(response.content)
  34. result = "Speech generated and saved to output.wav"
  35. print(f"[generate_speech] {result}")
  36. return result
  37. else:
  38. result = "TTS server returned no content."
  39. print(f"[generate_speech] {result}")
  40. return result
  41. except Exception as e:
  42. result = f"TTS server error: {str(e)}"
  43. print(f"[generate_speech] {result}")
  44. return result
  45.  
  46. def play_audio(filename: str):
  47. """
  48. Play the WAV file internally using sounddevice and soundfile.
  49. """
  50. try:
  51. data, samplerate = sf.read(filename, dtype='float32')
  52. print(f"[play_audio] Playing audio from {filename} at {samplerate} Hz")
  53. sd.play(data, samplerate)
  54. sd.wait() # Wait until playback is finished
  55. print("[play_audio] Audio playback completed.")
  56. except Exception as e:
  57. print(f"[play_audio] Error during audio playback: {e}")
  58.  
  59. def chat_loop():
  60. """
  61. Interactive chat loop where the assistant is instructed to output in JSON format.
  62. The script extracts the 'response' field and uses that text for TTS playback.
  63. """
  64. messages = [
  65. {
  66. "role": "system",
  67. "content": (
  68. "You are GLaDOS, the sarcastic, dry, and menacing AI from Portal. "
  69. "When responding, output your answer in JSON format according to the following schema:\n\n"
  70. "{\n"
  71. ' "response": "Your response here, written with sarcastic wit.",\n'
  72. ' "mood": "One of: sarcastic, menacing, dry, mocking",\n'
  73. ' "additional_comment": "Any extra commentary, if needed."\n'
  74. "}\n\n"
  75. "Do not include any additional keys or text outside this JSON object."
  76. )
  77. }
  78. ]
  79.  
  80. print("GLaDOS mode activated. Type 'quit' to exit.")
  81.  
  82. while True:
  83. user_input = input("\nYou: ").strip()
  84. if user_input.lower() == "quit":
  85. break
  86.  
  87. messages.append({"role": "user", "content": user_input})
  88.  
  89. try:
  90. response = client.chat.completions.create(
  91. model=model,
  92. messages=messages
  93. )
  94. except Exception as e:
  95. print(f"Error communicating with LM Studio: {str(e)}")
  96. continue
  97.  
  98. if response.choices[0].message.content:
  99. raw_output = response.choices[0].message.content
  100. try:
  101. glados_output = json.loads(raw_output)
  102. assistant_text = glados_output.get("response", "")
  103. mood = glados_output.get("mood", "")
  104. additional_comment = glados_output.get("additional_comment", "")
  105.  
  106. print("\nGLaDOS:")
  107. print("Response:", assistant_text)
  108. print("Mood:", mood)
  109. if additional_comment:
  110. print("Additional Comment:", additional_comment)
  111. except json.JSONDecodeError as je:
  112. print("Error decoding JSON:", je)
  113. print("Raw response:", raw_output)
  114. assistant_text = raw_output # Fallback to raw text
  115.  
  116. messages.append({"role": "assistant", "content": raw_output})
  117.  
  118. # Use only the extracted assistant text for TTS.
  119. tts_result = generate_speech(assistant_text)
  120. print(f"[TTS] {tts_result}")
  121. play_audio("output.wav")
  122. else:
  123. print("GLaDOS returned no content.")
  124.  
  125. time.sleep(0.5)
  126.  
  127. if __name__ == "__main__":
  128. chat_loop()
  129.  
Tags: ai
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement