Chandima Prabhath commited on
Commit
4beb4cc
·
1 Parent(s): 7995625

Add random seed to voice reply generation and enhance image sending function with customizable caption

Browse files
Files changed (2) hide show
  1. VoiceReply.py +3 -1
  2. app.py +13 -7
VoiceReply.py CHANGED
@@ -2,6 +2,7 @@ import requests
2
  import os
3
  import time
4
  import urllib.parse
 
5
 
6
  def generate_voice_reply(prompt, model="openai-audio", voice="coral", audio_dir="."):
7
  """
@@ -23,6 +24,7 @@ def generate_voice_reply(prompt, model="openai-audio", voice="coral", audio_dir=
23
  print("DEBUG: Starting generate_voice_reply")
24
  print(f"DEBUG: Received prompt: {prompt}")
25
  os.makedirs(audio_dir, exist_ok=True)
 
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. ")
@@ -30,7 +32,7 @@ def generate_voice_reply(prompt, model="openai-audio", voice="coral", audio_dir=
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:
 
2
  import os
3
  import time
4
  import urllib.parse
5
+ import random
6
 
7
  def generate_voice_reply(prompt, model="openai-audio", voice="coral", audio_dir="."):
8
  """
 
24
  print("DEBUG: Starting generate_voice_reply")
25
  print(f"DEBUG: Received prompt: {prompt}")
26
  os.makedirs(audio_dir, exist_ok=True)
27
+ randomSeed = random.randint(0, 999999)
28
  # Append system prompt to the user's prompt
29
  system_prompt = ("Your name is Eve, a WhatsApp bot that can generate images from Flux and help users generate images. "
30
  "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. ")
 
32
  print(f"DEBUG: Full prompt: {full_prompt}")
33
 
34
  encoded_prompt = urllib.parse.quote(full_prompt)
35
+ url = f"http://text.pollinations.ai/{encoded_prompt}?model={model}&voice={voice}&seed={randomSeed}"
36
  print(f"DEBUG: Fetching audio with URL: {url}")
37
 
38
  try:
app.py CHANGED
@@ -66,10 +66,10 @@ def send_message(message_id, to_number, message, retries=3):
66
  if i == retries-1:
67
  return {"error": str(e)}
68
 
69
- def send_image(message_id, to_number, image_path, retries=3):
70
  chat_id = to_number if to_number.endswith("@g.us") else to_number
71
  url = f"{GREEN_API_MEDIA_URL}/waInstance{GREEN_API_ID_INSTANCE}/sendFileByUpload/{GREEN_API_TOKEN}"
72
- payload = {"chatId": chat_id, "caption": "Here you go!", "quotedMessageId": message_id}
73
  files = [("file", ("image.jpg", open(image_path, "rb"), "image/jpeg"))]
74
  for i in range(retries):
75
  try:
@@ -128,14 +128,20 @@ def handle_image_generation(message_id, chat_id, prompt):
128
  try:
129
  img, path, ret_prompt, url = generate_image(prompt, message_id, message_id, image_dir)
130
  if img:
131
- send_image(message_id, chat_id, path)
132
- send_message(
133
- message_id, chat_id,
134
- f"✅ Image ready: {url}\n>{chr(8203)} _{ret_prompt}_"
 
 
 
 
 
135
  )
136
  else:
137
  send_message(message_id, chat_id, "Image generation failed.")
138
- except:
 
139
  send_message(message_id, chat_id, "Error generating image.")
140
 
141
  # --- webhook ---
 
66
  if i == retries-1:
67
  return {"error": str(e)}
68
 
69
+ def send_image(message_id, to_number, image_path, caption = "Here you go!", retries=3):
70
  chat_id = to_number if to_number.endswith("@g.us") else to_number
71
  url = f"{GREEN_API_MEDIA_URL}/waInstance{GREEN_API_ID_INSTANCE}/sendFileByUpload/{GREEN_API_TOKEN}"
72
+ payload = {"chatId": chat_id, "caption": caption, "quotedMessageId": message_id}
73
  files = [("file", ("image.jpg", open(image_path, "rb"), "image/jpeg"))]
74
  for i in range(retries):
75
  try:
 
128
  try:
129
  img, path, ret_prompt, url = generate_image(prompt, message_id, message_id, image_dir)
130
  if img:
131
+ # Split the ret_prompt into paragraphs and wrap each in underscores for italics.
132
+ formatted_ret_prompt = "\n\n".join(
133
+ f"_{paragraph.strip()}_" for paragraph in ret_prompt.split("\n\n") if paragraph.strip()
134
+ )
135
+ send_image(
136
+ message_id,
137
+ chat_id,
138
+ path,
139
+ caption=f"✨ Image ready: {url}\n>{chr(8203)} {formatted_ret_prompt}"
140
  )
141
  else:
142
  send_message(message_id, chat_id, "Image generation failed.")
143
+ except Exception as e:
144
+ logging.error("Error in handle_image_generation: %s", e)
145
  send_message(message_id, chat_id, "Error generating image.")
146
 
147
  # --- webhook ---