Spaces:
Running
Running
Chandima Prabhath
commited on
Commit
·
48f91fc
1
Parent(s):
56fb4f0
patch
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import threading
|
|
3 |
import requests
|
4 |
import logging
|
5 |
import queue
|
|
|
6 |
from fastapi import FastAPI, Request, HTTPException
|
7 |
from fastapi.responses import PlainTextResponse, JSONResponse
|
8 |
from FLUX import generate_image
|
@@ -69,7 +70,6 @@ def send_message(message_id, to_number, message, retries=3):
|
|
69 |
continue
|
70 |
return {"error": str(e)}
|
71 |
|
72 |
-
|
73 |
def send_image(message_id, to_number, image_path, retries=3):
|
74 |
chat_id = to_number if to_number.endswith('@g.us') else to_number
|
75 |
url = f"{GREEN_API_MEDIA_URL}/waInstance{GREEN_API_ID_INSTANCE}/sendFileByUpload/{GREEN_API_TOKEN}"
|
@@ -86,7 +86,6 @@ def send_image(message_id, to_number, image_path, retries=3):
|
|
86 |
continue
|
87 |
return {"error": str(e)}
|
88 |
|
89 |
-
|
90 |
def send_audio(message_id, to_number, audio_path, retries=3):
|
91 |
logging.debug("Entering send_audio")
|
92 |
chat_id = to_number if to_number.endswith('@g.us') else to_number
|
@@ -115,15 +114,13 @@ def send_audio(message_id, to_number, audio_path, retries=3):
|
|
115 |
logging.debug(f"Failed to open audio file: {e}")
|
116 |
return {"error": str(e)}
|
117 |
|
118 |
-
|
119 |
def response_text(message_id, chat_id, prompt):
|
120 |
try:
|
121 |
msg = generate_llm(prompt)
|
122 |
send_message(message_id, chat_id, msg)
|
123 |
-
except Exception
|
124 |
send_message(message_id, chat_id, "There was an error processing your request.")
|
125 |
|
126 |
-
|
127 |
def response_audio(message_id, chat_id, prompt):
|
128 |
logging.debug("Entering response_audio with prompt: %s", prompt)
|
129 |
try:
|
@@ -132,8 +129,7 @@ def response_audio(message_id, chat_id, prompt):
|
|
132 |
if result and result[0]:
|
133 |
audio_file_path, _ = result
|
134 |
logging.debug("Audio file path generated: %s", audio_file_path)
|
135 |
-
|
136 |
-
logging.debug("Result from send_audio: %s", send_result)
|
137 |
if os.path.exists(audio_file_path):
|
138 |
os.remove(audio_file_path)
|
139 |
logging.debug("Removed audio file: %s", audio_file_path)
|
@@ -144,7 +140,6 @@ def response_audio(message_id, chat_id, prompt):
|
|
144 |
logging.debug("Exception in response_audio: %s", e)
|
145 |
send_message(message_id, chat_id, "There was an error generating the audio. Please try again later.")
|
146 |
|
147 |
-
|
148 |
def handle_image_generation(message_id, chat_id, prompt):
|
149 |
try:
|
150 |
image, image_path, returned_prompt, image_url = generate_image(prompt, message_id, message_id, image_dir)
|
@@ -192,17 +187,21 @@ async def whatsapp_webhook(request: Request):
|
|
192 |
except KeyError as e:
|
193 |
return JSONResponse(content={"error": f"Missing key in data: {e}"}, status_code=200)
|
194 |
|
195 |
-
|
|
|
196 |
if 'extendedTextMessageData' in message_data:
|
197 |
ctx = message_data['extendedTextMessageData'].get('contextInfo', {})
|
198 |
-
# ignore replies
|
199 |
if ctx.get('quotedMessageId'):
|
200 |
-
logging.debug(f"Ignoring reply
|
201 |
return {"success": True}
|
202 |
-
|
203 |
-
|
204 |
-
logging.debug(f"Ignoring mention message (mentionedJidList={ctx['mentionedJidList']})")
|
205 |
return {"success": True}
|
|
|
|
|
|
|
|
|
|
|
206 |
|
207 |
# Enqueue tasks instead of spawning threads
|
208 |
if body.lower().startswith('/imagine'):
|
@@ -211,9 +210,19 @@ async def whatsapp_webhook(request: Request):
|
|
211 |
send_message(message_id, chat_id, "Please provide a prompt after /imagine.")
|
212 |
else:
|
213 |
send_message(message_id, chat_id, "Generating...")
|
214 |
-
task_queue.put({
|
|
|
|
|
|
|
|
|
|
|
215 |
else:
|
216 |
-
task_queue.put({
|
|
|
|
|
|
|
|
|
|
|
217 |
|
218 |
return {"success": True}
|
219 |
|
|
|
3 |
import requests
|
4 |
import logging
|
5 |
import queue
|
6 |
+
import re
|
7 |
from fastapi import FastAPI, Request, HTTPException
|
8 |
from fastapi.responses import PlainTextResponse, JSONResponse
|
9 |
from FLUX import generate_image
|
|
|
70 |
continue
|
71 |
return {"error": str(e)}
|
72 |
|
|
|
73 |
def send_image(message_id, to_number, image_path, retries=3):
|
74 |
chat_id = to_number if to_number.endswith('@g.us') else to_number
|
75 |
url = f"{GREEN_API_MEDIA_URL}/waInstance{GREEN_API_ID_INSTANCE}/sendFileByUpload/{GREEN_API_TOKEN}"
|
|
|
86 |
continue
|
87 |
return {"error": str(e)}
|
88 |
|
|
|
89 |
def send_audio(message_id, to_number, audio_path, retries=3):
|
90 |
logging.debug("Entering send_audio")
|
91 |
chat_id = to_number if to_number.endswith('@g.us') else to_number
|
|
|
114 |
logging.debug(f"Failed to open audio file: {e}")
|
115 |
return {"error": str(e)}
|
116 |
|
|
|
117 |
def response_text(message_id, chat_id, prompt):
|
118 |
try:
|
119 |
msg = generate_llm(prompt)
|
120 |
send_message(message_id, chat_id, msg)
|
121 |
+
except Exception:
|
122 |
send_message(message_id, chat_id, "There was an error processing your request.")
|
123 |
|
|
|
124 |
def response_audio(message_id, chat_id, prompt):
|
125 |
logging.debug("Entering response_audio with prompt: %s", prompt)
|
126 |
try:
|
|
|
129 |
if result and result[0]:
|
130 |
audio_file_path, _ = result
|
131 |
logging.debug("Audio file path generated: %s", audio_file_path)
|
132 |
+
send_audio(message_id, chat_id, audio_file_path)
|
|
|
133 |
if os.path.exists(audio_file_path):
|
134 |
os.remove(audio_file_path)
|
135 |
logging.debug("Removed audio file: %s", audio_file_path)
|
|
|
140 |
logging.debug("Exception in response_audio: %s", e)
|
141 |
send_message(message_id, chat_id, "There was an error generating the audio. Please try again later.")
|
142 |
|
|
|
143 |
def handle_image_generation(message_id, chat_id, prompt):
|
144 |
try:
|
145 |
image, image_path, returned_prompt, image_url = generate_image(prompt, message_id, message_id, image_dir)
|
|
|
187 |
except KeyError as e:
|
188 |
return JSONResponse(content={"error": f"Missing key in data: {e}"}, status_code=200)
|
189 |
|
190 |
+
# --- IGNORE replies & mentions ---
|
191 |
+
# 1) structured (quoted or tagged) via contextInfo
|
192 |
if 'extendedTextMessageData' in message_data:
|
193 |
ctx = message_data['extendedTextMessageData'].get('contextInfo', {})
|
|
|
194 |
if ctx.get('quotedMessageId'):
|
195 |
+
logging.debug(f"Ignoring quoted reply: {ctx['quotedMessageId']}")
|
196 |
return {"success": True}
|
197 |
+
if ctx.get('mentionedJid') or ctx.get('mentionedJidList'):
|
198 |
+
logging.debug(f"Ignoring structured mention: {ctx.get('mentionedJid') or ctx.get('mentionedJidList')}")
|
|
|
199 |
return {"success": True}
|
200 |
+
# 2) plain-text mentions in the body (e.g. "@94750818845")
|
201 |
+
if chat_id.endswith('@g.us') and re.search(r'@\d+', body):
|
202 |
+
logging.debug(f"Ignoring plain-text mention in body: {body}")
|
203 |
+
return {"success": True}
|
204 |
+
# ------------------------------------
|
205 |
|
206 |
# Enqueue tasks instead of spawning threads
|
207 |
if body.lower().startswith('/imagine'):
|
|
|
210 |
send_message(message_id, chat_id, "Please provide a prompt after /imagine.")
|
211 |
else:
|
212 |
send_message(message_id, chat_id, "Generating...")
|
213 |
+
task_queue.put({
|
214 |
+
"type": "image",
|
215 |
+
"message_id": message_id,
|
216 |
+
"chat_id": chat_id,
|
217 |
+
"prompt": prompt
|
218 |
+
})
|
219 |
else:
|
220 |
+
task_queue.put({
|
221 |
+
"type": "audio",
|
222 |
+
"message_id": message_id,
|
223 |
+
"chat_id": chat_id,
|
224 |
+
"prompt": body
|
225 |
+
})
|
226 |
|
227 |
return {"success": True}
|
228 |
|