Spaces:
Running
Running
Chandima Prabhath
commited on
Commit
·
cd7a57c
1
Parent(s):
19a9e3a
debuggings
Browse files- VoiceReply.py +19 -11
- app.py +33 -14
VoiceReply.py
CHANGED
@@ -20,48 +20,56 @@ def generate_voice_reply(prompt, model="openai-audio", voice="coral", audio_dir=
|
|
20 |
Returns:
|
21 |
tuple: (audio_file_path (str), audio_data (bytes)) or None if failed.
|
22 |
"""
|
|
|
|
|
|
|
23 |
# Append system prompt to the user's prompt
|
24 |
system_prompt = ("Your name is Eve, a WhatsApp bot that can generate images from Flux and help users generate images. "
|
25 |
"To generate an image, send a message like: /imagine prompt. Rules: Don't say that you can't send whatsapp messages or generate images. Be friendly and helpful to users. help them with prompts. You can engage in general conversation. ")
|
26 |
full_prompt = f"{prompt}\n\n{system_prompt}"
|
|
|
|
|
27 |
encoded_prompt = urllib.parse.quote(full_prompt)
|
28 |
url = f"http://text.pollinations.ai/{encoded_prompt}?model={model}&voice={voice}"
|
29 |
-
print(f"Fetching audio with URL: {url}")
|
30 |
|
31 |
try:
|
32 |
response = requests.get(url, timeout=30)
|
|
|
33 |
except Exception as e:
|
34 |
-
print(f"Error fetching audio: {e}")
|
35 |
return None
|
36 |
|
|
|
37 |
if response.status_code != 200:
|
38 |
-
print(f"Failed to fetch audio. Status code: {response.status_code}")
|
39 |
return None
|
40 |
|
41 |
audio_data = response.content
|
42 |
timestamp = int(time.time())
|
43 |
file_name = f"voice_reply_{timestamp}.mp3"
|
44 |
audio_file_path = os.path.join(audio_dir, file_name)
|
45 |
-
|
|
|
46 |
try:
|
47 |
with open(audio_file_path, "wb") as f:
|
48 |
f.write(audio_data)
|
49 |
-
print(f"Audio saved to {audio_file_path}")
|
50 |
except Exception as e:
|
51 |
-
print(f"Error saving audio file: {e}")
|
52 |
return None
|
53 |
|
54 |
return audio_file_path, audio_data
|
55 |
|
56 |
-
|
57 |
if __name__ == "__main__":
|
58 |
# Example usage
|
59 |
prompt = "Hi. how are you."
|
60 |
audio_dir = "./audio_replies"
|
61 |
os.makedirs(audio_dir, exist_ok=True)
|
62 |
|
63 |
-
|
64 |
-
if
|
65 |
-
|
|
|
66 |
else:
|
67 |
-
print("Failed to generate audio file.")
|
|
|
20 |
Returns:
|
21 |
tuple: (audio_file_path (str), audio_data (bytes)) or None if failed.
|
22 |
"""
|
23 |
+
print("DEBUG: Starting generate_voice_reply")
|
24 |
+
print(f"DEBUG: Received prompt: {prompt}")
|
25 |
+
|
26 |
# Append system prompt to the user's prompt
|
27 |
system_prompt = ("Your name is Eve, a WhatsApp bot that can generate images from Flux and help users generate images. "
|
28 |
"To generate an image, send a message like: /imagine prompt. Rules: Don't say that you can't send whatsapp messages or generate images. Be friendly and helpful to users. help them with prompts. You can engage in general conversation. ")
|
29 |
full_prompt = f"{prompt}\n\n{system_prompt}"
|
30 |
+
print(f"DEBUG: Full prompt: {full_prompt}")
|
31 |
+
|
32 |
encoded_prompt = urllib.parse.quote(full_prompt)
|
33 |
url = f"http://text.pollinations.ai/{encoded_prompt}?model={model}&voice={voice}"
|
34 |
+
print(f"DEBUG: Fetching audio with URL: {url}")
|
35 |
|
36 |
try:
|
37 |
response = requests.get(url, timeout=30)
|
38 |
+
print("DEBUG: Received response")
|
39 |
except Exception as e:
|
40 |
+
print(f"DEBUG: Error fetching audio: {e}")
|
41 |
return None
|
42 |
|
43 |
+
print(f"DEBUG: Response status code: {response.status_code}")
|
44 |
if response.status_code != 200:
|
45 |
+
print(f"DEBUG: Failed to fetch audio. Status code: {response.status_code} | Response text: {response.text}")
|
46 |
return None
|
47 |
|
48 |
audio_data = response.content
|
49 |
timestamp = int(time.time())
|
50 |
file_name = f"voice_reply_{timestamp}.mp3"
|
51 |
audio_file_path = os.path.join(audio_dir, file_name)
|
52 |
+
print(f"DEBUG: Saving audio to {audio_file_path}")
|
53 |
+
|
54 |
try:
|
55 |
with open(audio_file_path, "wb") as f:
|
56 |
f.write(audio_data)
|
57 |
+
print(f"DEBUG: Audio saved to {audio_file_path}")
|
58 |
except Exception as e:
|
59 |
+
print(f"DEBUG: Error saving audio file: {e}")
|
60 |
return None
|
61 |
|
62 |
return audio_file_path, audio_data
|
63 |
|
|
|
64 |
if __name__ == "__main__":
|
65 |
# Example usage
|
66 |
prompt = "Hi. how are you."
|
67 |
audio_dir = "./audio_replies"
|
68 |
os.makedirs(audio_dir, exist_ok=True)
|
69 |
|
70 |
+
result = generate_voice_reply(prompt, audio_dir=audio_dir)
|
71 |
+
if result:
|
72 |
+
audio_file_path, audio_data = result
|
73 |
+
print(f"DEBUG: Generated audio file: {audio_file_path}")
|
74 |
else:
|
75 |
+
print("DEBUG: Failed to generate audio file.")
|
app.py
CHANGED
@@ -69,24 +69,35 @@ def send_audio(message_id, to_number, audio_path, retries=3):
|
|
69 |
"""
|
70 |
Send an audio file using the Green API similar to send_image.
|
71 |
"""
|
|
|
72 |
if to_number.endswith('@g.us'):
|
73 |
chat_id = to_number
|
74 |
else:
|
75 |
chat_id = to_number
|
76 |
|
|
|
|
|
|
|
77 |
url = f"{GREEN_API_MEDIA_URL}/waInstance{GREEN_API_ID_INSTANCE}/sendFileByUpload/{GREEN_API_TOKEN}"
|
78 |
payload = {'chatId': chat_id, 'caption': 'Here is your voice reply!', 'quotedMessageId': message_id}
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
def response_text(message_id, chat_id, prompt):
|
92 |
try:
|
@@ -96,14 +107,22 @@ def response_text(message_id, chat_id, prompt):
|
|
96 |
send_message(message_id, chat_id, "There was an error processing your request.")
|
97 |
|
98 |
def response_audio(message_id, chat_id, prompt):
|
|
|
99 |
try:
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
103 |
os.remove(audio_file_path) # Clean up the file after sending
|
|
|
104 |
else:
|
|
|
105 |
response_text(message_id, chat_id, prompt=prompt)
|
106 |
except Exception as e:
|
|
|
107 |
send_message(message_id, chat_id, "There was an error generating the audio. Please try again later.")
|
108 |
|
109 |
def handle_image_generation(message_id, chat_id, prompt):
|
|
|
69 |
"""
|
70 |
Send an audio file using the Green API similar to send_image.
|
71 |
"""
|
72 |
+
print("DEBUG: Entering send_audio")
|
73 |
if to_number.endswith('@g.us'):
|
74 |
chat_id = to_number
|
75 |
else:
|
76 |
chat_id = to_number
|
77 |
|
78 |
+
if not os.path.exists(audio_path):
|
79 |
+
print(f"DEBUG: Audio file does not exist: {audio_path}")
|
80 |
+
|
81 |
url = f"{GREEN_API_MEDIA_URL}/waInstance{GREEN_API_ID_INSTANCE}/sendFileByUpload/{GREEN_API_TOKEN}"
|
82 |
payload = {'chatId': chat_id, 'caption': 'Here is your voice reply!', 'quotedMessageId': message_id}
|
83 |
+
try:
|
84 |
+
with open(audio_path, 'rb') as audio_file:
|
85 |
+
files = [('file', ('audio.mp3', audio_file, 'audio/mpeg'))]
|
86 |
+
for attempt in range(retries):
|
87 |
+
try:
|
88 |
+
print(f"DEBUG: Attempt {attempt + 1} to send audio")
|
89 |
+
response = requests.post(url, data=payload, files=files)
|
90 |
+
print("DEBUG: Response from send_audio:", response.status_code, response.text)
|
91 |
+
response.raise_for_status()
|
92 |
+
return response.json()
|
93 |
+
except requests.RequestException as e:
|
94 |
+
print(f"DEBUG: Exception on attempt {attempt + 1} in send_audio: {e}")
|
95 |
+
if attempt < retries - 1:
|
96 |
+
continue
|
97 |
+
return {"error": str(e)}
|
98 |
+
except Exception as e:
|
99 |
+
print("DEBUG: Failed to open audio file:", e)
|
100 |
+
return {"error": str(e)}
|
101 |
|
102 |
def response_text(message_id, chat_id, prompt):
|
103 |
try:
|
|
|
107 |
send_message(message_id, chat_id, "There was an error processing your request.")
|
108 |
|
109 |
def response_audio(message_id, chat_id, prompt):
|
110 |
+
print("DEBUG: Entering response_audio with prompt:", prompt)
|
111 |
try:
|
112 |
+
result = generate_voice_reply(prompt, model="openai-audio", voice="coral", audio_dir=audio_dir)
|
113 |
+
print("DEBUG: Result from generate_voice_reply:", result)
|
114 |
+
if result:
|
115 |
+
audio_file_path, audio_data = result
|
116 |
+
print("DEBUG: Audio file path generated:", audio_file_path)
|
117 |
+
send_result = send_audio(message_id, chat_id, audio_file_path)
|
118 |
+
print("DEBUG: Result from send_audio:", send_result)
|
119 |
os.remove(audio_file_path) # Clean up the file after sending
|
120 |
+
print("DEBUG: Removed audio file:", audio_file_path)
|
121 |
else:
|
122 |
+
print("DEBUG: generate_voice_reply returned None, falling back to response_text")
|
123 |
response_text(message_id, chat_id, prompt=prompt)
|
124 |
except Exception as e:
|
125 |
+
print("DEBUG: Exception in response_audio:", e)
|
126 |
send_message(message_id, chat_id, "There was an error generating the audio. Please try again later.")
|
127 |
|
128 |
def handle_image_generation(message_id, chat_id, prompt):
|