Spaces:
Running
Running
Chandima Prabhath
commited on
Commit
Β·
6e6c1ff
1
Parent(s):
48499fe
Add support for sending multiple images at once and update image generation handling
Browse files
app.py
CHANGED
@@ -113,6 +113,32 @@ def send_image(message_id, to_number, image_path, caption="Here you go!", retrie
|
|
113 |
if i == retries - 1:
|
114 |
return {"error": str(e)}
|
115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
def send_audio(message_id, to_number, audio_path, retries=3):
|
117 |
logging.debug("send_audio")
|
118 |
chat_id = to_number if to_number.endswith("@g.us") else to_number
|
@@ -159,22 +185,26 @@ def response_audio(message_id, chat_id, prompt):
|
|
159 |
|
160 |
def handle_image_generation(message_id, chat_id, prompt):
|
161 |
try:
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
|
|
|
|
|
|
|
|
175 |
except Exception as e:
|
176 |
logging.error("Error in handle_image_generation: %s", e)
|
177 |
-
send_message(message_id, chat_id, "Error generating
|
178 |
|
179 |
# --- Startup Message ---
|
180 |
def send_startup_message():
|
@@ -187,22 +217,22 @@ def send_startup_message():
|
|
187 |
logging.warning("BOT_STATUS_CHAT is not set; startup message not sent.")
|
188 |
|
189 |
help_text = (
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
|
207 |
# --- Webhook ---
|
208 |
@app.post("/whatsapp")
|
@@ -321,12 +351,10 @@ async def whatsapp_webhook(request: Request):
|
|
321 |
|
322 |
# ANSWER: Accept any message starting with /answer. If additional text is provided, check it.
|
323 |
if low.startswith("/answer"):
|
324 |
-
# Remove command and any extra spaces
|
325 |
user_response = body[len("/answer"):].strip()
|
326 |
if chat in trivia_store:
|
327 |
correct_answer = trivia_store[chat]["answer"]
|
328 |
question = trivia_store[chat]["question"]
|
329 |
-
# If user provided an answer, evaluate it via LLM; otherwise, just reveal the answer.
|
330 |
if user_response:
|
331 |
eval_prompt = (
|
332 |
f"Question: {question}\n"
|
@@ -431,7 +459,6 @@ def index():
|
|
431 |
return "Server is running!"
|
432 |
|
433 |
if __name__ == "__main__":
|
434 |
-
# Send startup message on launch
|
435 |
def send_startup_message():
|
436 |
if BOT_STATUS_CHAT:
|
437 |
startup_msg = "π Hi! I'm Eve, your friendly AI assistant. I'm now live and ready to help with images, voice replies, and more!"
|
|
|
113 |
if i == retries - 1:
|
114 |
return {"error": str(e)}
|
115 |
|
116 |
+
def send_images(message_id, to_number, image_paths, caption="Here are your images!", retries=3):
|
117 |
+
"""
|
118 |
+
Uploads multiple images at once.
|
119 |
+
"""
|
120 |
+
chat_id = to_number if to_number.endswith("@g.us") else to_number
|
121 |
+
url = f"{GREEN_API_MEDIA_URL}/waInstance{GREEN_API_ID_INSTANCE}/sendFileByUpload/{GREEN_API_TOKEN}"
|
122 |
+
payload = {"chatId": chat_id, "caption": caption, "quotedMessageId": message_id}
|
123 |
+
files = []
|
124 |
+
file_handles = []
|
125 |
+
try:
|
126 |
+
for idx, image_path in enumerate(image_paths):
|
127 |
+
f = open(image_path, "rb")
|
128 |
+
file_handles.append(f)
|
129 |
+
files.append(("file", (f"image_{idx}.jpg", f, "image/jpeg")))
|
130 |
+
for i in range(retries):
|
131 |
+
try:
|
132 |
+
r = requests.post(url, data=payload, files=files)
|
133 |
+
r.raise_for_status()
|
134 |
+
return r.json()
|
135 |
+
except requests.RequestException as e:
|
136 |
+
if i == retries - 1:
|
137 |
+
return {"error": str(e)}
|
138 |
+
finally:
|
139 |
+
for f in file_handles:
|
140 |
+
f.close()
|
141 |
+
|
142 |
def send_audio(message_id, to_number, audio_path, retries=3):
|
143 |
logging.debug("send_audio")
|
144 |
chat_id = to_number if to_number.endswith("@g.us") else to_number
|
|
|
185 |
|
186 |
def handle_image_generation(message_id, chat_id, prompt):
|
187 |
try:
|
188 |
+
images_info = []
|
189 |
+
# Generate 4 images using the same prompt.
|
190 |
+
for _ in range(4):
|
191 |
+
img, path, ret_prompt, url = generate_image(prompt, message_id, message_id, image_dir)
|
192 |
+
if not img:
|
193 |
+
send_message(message_id, chat_id, "Image generation failed.")
|
194 |
+
return
|
195 |
+
images_info.append((path, ret_prompt, url))
|
196 |
+
# Create a caption listing the URLs of all generated images.
|
197 |
+
caption = "β¨ Images ready:\n" + "\n".join(url for _, _, url in images_info)
|
198 |
+
# Send all generated images at once.
|
199 |
+
image_paths = [path for path, _, _ in images_info]
|
200 |
+
send_images(message_id, chat_id, image_paths, caption=caption)
|
201 |
+
# Cleanup generated image files.
|
202 |
+
for path in image_paths:
|
203 |
+
if os.path.exists(path):
|
204 |
+
os.remove(path)
|
205 |
except Exception as e:
|
206 |
logging.error("Error in handle_image_generation: %s", e)
|
207 |
+
send_message(message_id, chat_id, "Error generating images.")
|
208 |
|
209 |
# --- Startup Message ---
|
210 |
def send_startup_message():
|
|
|
217 |
logging.warning("BOT_STATUS_CHAT is not set; startup message not sent.")
|
218 |
|
219 |
help_text = (
|
220 |
+
"π€ *Hi there, I'm Eve!* Here are the commands you can use:\n\n"
|
221 |
+
"β’ */help* β _Show this help message._\n"
|
222 |
+
"β’ */summarize <text>* β _Get a quick summary of your text._\n"
|
223 |
+
"β’ */translate <language>|<text>* β _Translate text to your chosen language._\n"
|
224 |
+
"β’ */joke* β _Enjoy a random, funny joke._\n"
|
225 |
+
"β’ */weather <location>* β _Get the current weather for a location._\n"
|
226 |
+
"β’ */inspire* β _Receive a short inspirational quote._\n"
|
227 |
+
"β’ */trivia* β _Start a new trivia question._\n"
|
228 |
+
"β’ */answer [your answer]* β _Reveal the trivia answer or check your answer if provided._\n"
|
229 |
+
"β’ */meme <text>* β _Generate a fun meme image._\n"
|
230 |
+
"β’ */poll <Question>|<Option1>|<Option2>|β¦* β _Create a poll._\n"
|
231 |
+
"β’ */results* β _See current poll results._\n"
|
232 |
+
"β’ */endpoll* β _End the poll and show final results._\n"
|
233 |
+
"β’ */gen <prompt>* β _Generate an image from your prompt._\n\n"
|
234 |
+
"Send any other text and I'll reply with a voice message. I'm here to help, so don't hesitate to ask!"
|
235 |
+
)
|
236 |
|
237 |
# --- Webhook ---
|
238 |
@app.post("/whatsapp")
|
|
|
351 |
|
352 |
# ANSWER: Accept any message starting with /answer. If additional text is provided, check it.
|
353 |
if low.startswith("/answer"):
|
|
|
354 |
user_response = body[len("/answer"):].strip()
|
355 |
if chat in trivia_store:
|
356 |
correct_answer = trivia_store[chat]["answer"]
|
357 |
question = trivia_store[chat]["question"]
|
|
|
358 |
if user_response:
|
359 |
eval_prompt = (
|
360 |
f"Question: {question}\n"
|
|
|
459 |
return "Server is running!"
|
460 |
|
461 |
if __name__ == "__main__":
|
|
|
462 |
def send_startup_message():
|
463 |
if BOT_STATUS_CHAT:
|
464 |
startup_msg = "π Hi! I'm Eve, your friendly AI assistant. I'm now live and ready to help with images, voice replies, and more!"
|