Spaces:
Running
Running
File size: 2,924 Bytes
b9724da 4beb4cc 7e58c6f b9724da cd7a57c b920265 e371810 3470178 4beb4cc cd7a57c b9724da cd7a57c b9724da cd7a57c b9724da cd7a57c b9724da cd7a57c b9724da cd7a57c b9724da cd7a57c b9724da cd7a57c b9724da 0ffceca b920265 b9724da 0ffceca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
import requests
import os
import time
import urllib.parse
import random
from utils import read_config
_config = read_config()["llm"]
_SYSTEM_TEMPLATE = _config.get("system_prompt", "")
_CHAR = _config.get("char", "Eve")
def _build_system_prompt() -> str:
"""
Substitute {char} into the system prompt template.
"""
return _SYSTEM_TEMPLATE.replace("{char}", _CHAR)
def generate_voice_reply(prompt, model="openai-audio", voice="coral", audio_dir="."):
"""
Generate an audio file (MP3) using the Pollinations text-to-speech API.
The prompt is appended with a system message explaining that this is Eve,
a WhatsApp bot that can generate images from Flux and help users generate images.
To generate an image, the user should send a message like: /imagine {prompt}
Parameters:
prompt (str): The text prompt for audio generation.
model (str): Model identifier (default: openai-audio).
voice (str): Voice parameter (default: coral).
audio_dir (str): Directory where the audio file will be saved.
Returns:
tuple: (audio_file_path (str), audio_data (bytes)) or None if failed.
"""
print("DEBUG: Starting generate_voice_reply")
print(f"DEBUG: Received prompt: {prompt}")
os.makedirs(audio_dir, exist_ok=True)
randomSeed = random.randint(0, 9999999)
encoded_prompt = urllib.parse.quote(prompt)
url = f"http://text.pollinations.ai/{encoded_prompt}?model={model}&voice={voice}&seed={randomSeed}"
print(f"DEBUG: Fetching audio with URL: {url}")
try:
response = requests.get(url, timeout=30)
print("DEBUG: Received response")
except Exception as e:
print(f"DEBUG: Error fetching audio: {e}")
return None
print(f"DEBUG: Response status code: {response.status_code}")
if response.status_code != 200:
print(f"DEBUG: Failed to fetch audio. Status code: {response.status_code} | Response text: {response.text}")
return None
audio_data = response.content
timestamp = int(time.time())
file_name = f"voice_reply_{timestamp}.mp3"
audio_file_path = os.path.join(audio_dir, file_name)
print(f"DEBUG: Saving audio to {audio_file_path}")
try:
with open(audio_file_path, "wb") as f:
f.write(audio_data)
print(f"DEBUG: Audio saved to {audio_file_path}")
except Exception as e:
print(f"DEBUG: Error saving audio file: {e}")
return None
return audio_file_path, audio_data
# if __name__ == "__main__":
# # Example usage
# prompt = "Hi. how are you."
# audio_dir = "./audio_replies"
# result = generate_voice_reply(prompt, audio_dir=audio_dir)
# if result:
# audio_file_path, audio_data = result
# print(f"DEBUG: Generated audio file: {audio_file_path}")
# else:
# print("DEBUG: Failed to generate audio file.") |