Spaces:
Running
Running
Chandima Prabhath
commited on
Commit
Β·
1e6d365
1
Parent(s):
dc0ed92
Remove polling functionality and related intent models from the bot
Browse files
app.py
CHANGED
@@ -146,7 +146,6 @@ client = BotClient(BotConfig)
|
|
146 |
# --- Threading & Queues ---------------------------------------------------
|
147 |
|
148 |
task_queue = queue.Queue()
|
149 |
-
polls = {}
|
150 |
executor = ThreadPoolExecutor(max_workers=4)
|
151 |
|
152 |
def worker():
|
@@ -222,43 +221,6 @@ def _fn_meme(mid, cid, txt):
|
|
222 |
"prompt": f"meme: {txt}"
|
223 |
})
|
224 |
|
225 |
-
def _fn_poll_create(mid, cid, question, options):
|
226 |
-
votes = {i+1:0 for i in range(len(options))}
|
227 |
-
polls[cid] = {"question": question, "options": options, "votes": votes, "voters": {}}
|
228 |
-
text = f"π *Poll:* {question}\n" + "\n".join(f"{i+1}. {o}" for i,o in enumerate(options))
|
229 |
-
_fn_send_text(mid, cid, text)
|
230 |
-
|
231 |
-
def _fn_poll_vote(mid, cid, voter, choice):
|
232 |
-
poll = polls.get(cid)
|
233 |
-
if not poll or choice<1 or choice>len(poll["options"]):
|
234 |
-
return
|
235 |
-
prev = poll["voters"].get(voter)
|
236 |
-
if prev:
|
237 |
-
poll["votes"][prev] -= 1
|
238 |
-
poll["votes"][choice] += 1
|
239 |
-
poll["voters"][voter] = choice
|
240 |
-
_fn_send_text(mid, cid, f"β
Voted for {poll['options'][choice-1]}")
|
241 |
-
|
242 |
-
def _fn_poll_results(mid, cid):
|
243 |
-
poll = polls.get(cid)
|
244 |
-
if not poll:
|
245 |
-
_fn_send_text(mid, cid, "No active poll.")
|
246 |
-
return
|
247 |
-
txt = f"π *Results:* {poll['question']}\n" + "\n".join(
|
248 |
-
f"{i}. {o}: {poll['votes'][i]}" for i,o in enumerate(poll["options"],1)
|
249 |
-
)
|
250 |
-
_fn_send_text(mid, cid, txt)
|
251 |
-
|
252 |
-
def _fn_poll_end(mid, cid):
|
253 |
-
poll = polls.pop(cid, None)
|
254 |
-
if not poll:
|
255 |
-
_fn_send_text(mid, cid, "No active poll.")
|
256 |
-
return
|
257 |
-
txt = f"π *Final Results:* {poll['question']}\n" + "\n".join(
|
258 |
-
f"{i}. {o}: {poll['votes'][i]}" for i,o in enumerate(poll["options"],1)
|
259 |
-
)
|
260 |
-
_fn_send_text(mid, cid, txt)
|
261 |
-
|
262 |
def _fn_generate_images(
|
263 |
message_id: str,
|
264 |
chat_id: str,
|
@@ -338,22 +300,6 @@ class MemeIntent(BaseIntent):
|
|
338 |
action: Literal["meme"]
|
339 |
text: str
|
340 |
|
341 |
-
class PollCreateIntent(BaseModel):
|
342 |
-
action: Literal["poll_create"]
|
343 |
-
question: str
|
344 |
-
options: List[str]
|
345 |
-
|
346 |
-
class PollVoteIntent(BaseModel):
|
347 |
-
action: Literal["poll_vote"]
|
348 |
-
voter: str
|
349 |
-
choice: int
|
350 |
-
|
351 |
-
class PollResultsIntent(BaseModel):
|
352 |
-
action: Literal["poll_results"]
|
353 |
-
|
354 |
-
class PollEndIntent(BaseModel):
|
355 |
-
action: Literal["poll_end"]
|
356 |
-
|
357 |
class GenerateImageIntent(BaseModel):
|
358 |
action: Literal["generate_image"]
|
359 |
prompt: str
|
@@ -368,8 +314,7 @@ class SendTextIntent(BaseModel):
|
|
368 |
# list of all intent models
|
369 |
INTENT_MODELS = [
|
370 |
SummarizeIntent, TranslateIntent, JokeIntent, WeatherIntent,
|
371 |
-
InspireIntent, MemeIntent,
|
372 |
-
PollResultsIntent, PollEndIntent, GenerateImageIntent, SendTextIntent
|
373 |
]
|
374 |
|
375 |
ACTION_HANDLERS = {
|
@@ -379,10 +324,6 @@ ACTION_HANDLERS = {
|
|
379 |
"weather": lambda mid,cid,**i: _fn_weather(mid,cid,i["location"]),
|
380 |
"inspire": lambda mid,cid,**i: _fn_inspire(mid,cid),
|
381 |
"meme": lambda mid,cid,**i: _fn_meme(mid,cid,i["text"]),
|
382 |
-
"poll_create": lambda mid,cid,**i: _fn_poll_create(mid,cid,i["question"],i["options"]),
|
383 |
-
"poll_vote": lambda mid,cid,**i: _fn_poll_vote(mid,cid,i["voter"],i["choice"]),
|
384 |
-
"poll_results": lambda mid,cid,**i: _fn_poll_results(mid,cid),
|
385 |
-
"poll_end": lambda mid,cid,**i: _fn_poll_end(mid,cid),
|
386 |
"generate_image": _fn_generate_images,
|
387 |
"send_text": lambda mid,cid,**i: _fn_send_text(mid,cid,i["message"]),
|
388 |
}
|
@@ -456,12 +397,6 @@ def route_intent(user_input: str, chat_id: str, sender: str):
|
|
456 |
kwargs["location"] = data.get("location","")
|
457 |
elif action == "meme":
|
458 |
kwargs["text"] = data.get("text","")
|
459 |
-
elif action == "poll_create":
|
460 |
-
kwargs["question"] = data.get("question","")
|
461 |
-
kwargs["options"] = data.get("options",[])
|
462 |
-
elif action == "poll_vote":
|
463 |
-
kwargs["voter"] = sender
|
464 |
-
kwargs["choice"] = int(data.get("choice",0))
|
465 |
try:
|
466 |
model = next(
|
467 |
m for m in INTENT_MODELS
|
@@ -551,19 +486,6 @@ async def whatsapp_webhook(request: Request):
|
|
551 |
if low.startswith("/meme "):
|
552 |
_fn_meme(mid, chat_id, body[6:].strip())
|
553 |
return {"success": True}
|
554 |
-
if low.startswith("/poll "):
|
555 |
-
parts = [p.strip() for p in body[6:].split("|")]
|
556 |
-
_fn_poll_create(mid, chat_id, parts[0], parts[1:])
|
557 |
-
return {"success": True}
|
558 |
-
if chat_id in polls and low.isdigit():
|
559 |
-
_fn_poll_vote(mid, chat_id, sender, int(low))
|
560 |
-
return {"success": True}
|
561 |
-
if low == "/results":
|
562 |
-
_fn_poll_results(mid, chat_id)
|
563 |
-
return {"success": True}
|
564 |
-
if low == "/endpoll":
|
565 |
-
_fn_poll_end(mid, chat_id)
|
566 |
-
return {"success": True}
|
567 |
if low.startswith("/gen"):
|
568 |
parts = body[4:].split("|")
|
569 |
pr = parts[0].strip()
|
|
|
146 |
# --- Threading & Queues ---------------------------------------------------
|
147 |
|
148 |
task_queue = queue.Queue()
|
|
|
149 |
executor = ThreadPoolExecutor(max_workers=4)
|
150 |
|
151 |
def worker():
|
|
|
221 |
"prompt": f"meme: {txt}"
|
222 |
})
|
223 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
def _fn_generate_images(
|
225 |
message_id: str,
|
226 |
chat_id: str,
|
|
|
300 |
action: Literal["meme"]
|
301 |
text: str
|
302 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
303 |
class GenerateImageIntent(BaseModel):
|
304 |
action: Literal["generate_image"]
|
305 |
prompt: str
|
|
|
314 |
# list of all intent models
|
315 |
INTENT_MODELS = [
|
316 |
SummarizeIntent, TranslateIntent, JokeIntent, WeatherIntent,
|
317 |
+
InspireIntent, MemeIntent, GenerateImageIntent, SendTextIntent
|
|
|
318 |
]
|
319 |
|
320 |
ACTION_HANDLERS = {
|
|
|
324 |
"weather": lambda mid,cid,**i: _fn_weather(mid,cid,i["location"]),
|
325 |
"inspire": lambda mid,cid,**i: _fn_inspire(mid,cid),
|
326 |
"meme": lambda mid,cid,**i: _fn_meme(mid,cid,i["text"]),
|
|
|
|
|
|
|
|
|
327 |
"generate_image": _fn_generate_images,
|
328 |
"send_text": lambda mid,cid,**i: _fn_send_text(mid,cid,i["message"]),
|
329 |
}
|
|
|
397 |
kwargs["location"] = data.get("location","")
|
398 |
elif action == "meme":
|
399 |
kwargs["text"] = data.get("text","")
|
|
|
|
|
|
|
|
|
|
|
|
|
400 |
try:
|
401 |
model = next(
|
402 |
m for m in INTENT_MODELS
|
|
|
486 |
if low.startswith("/meme "):
|
487 |
_fn_meme(mid, chat_id, body[6:].strip())
|
488 |
return {"success": True}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
489 |
if low.startswith("/gen"):
|
490 |
parts = body[4:].split("|")
|
491 |
pr = parts[0].strip()
|