Spaces:
Running
Running
Chandima Prabhath
commited on
Commit
Β·
3b132e6
1
Parent(s):
49de99a
Increase conversation history limit to 20 messages; add summarize, translate, meme, and poll functionalities with corresponding intents and command support in the configuration.
Browse files- app.py +128 -3
- config.yaml +7 -0
app.py
CHANGED
@@ -53,7 +53,7 @@ def get_thread_context():
|
|
53 |
|
54 |
# --- Conversation History -------------------------------------------------
|
55 |
|
56 |
-
history = defaultdict(lambda: deque(maxlen=
|
57 |
|
58 |
def record_user_message(chat_id, sender, message):
|
59 |
history[(chat_id, sender)].append(f"User: {message}")
|
@@ -185,6 +185,14 @@ def _fn_send_accept(mid, cid, message):
|
|
185 |
if chat_id and sender:
|
186 |
record_bot_message(chat_id, sender, message)
|
187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
def _fn_joke(mid, cid):
|
189 |
try:
|
190 |
j = requests.get(
|
@@ -205,6 +213,52 @@ def _fn_inspire(mid, cid):
|
|
205 |
quote = generate_llm("Give me a unique, random short inspirational quote.")
|
206 |
_fn_send_text(mid, cid, f"β¨ {quote}")
|
207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
def _fn_generate_images(
|
209 |
message_id: str,
|
210 |
chat_id: str,
|
@@ -252,6 +306,15 @@ def _fn_voice_reply(
|
|
252 |
class BaseIntent(BaseModel):
|
253 |
action: str
|
254 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
255 |
class JokeIntent(BaseIntent):
|
256 |
action: Literal["joke"]
|
257 |
|
@@ -262,6 +325,26 @@ class WeatherIntent(BaseIntent):
|
|
262 |
class InspireIntent(BaseIntent):
|
263 |
action: Literal["inspire"]
|
264 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
265 |
class GenerateImageIntent(BaseModel):
|
266 |
action: Literal["generate_image"]
|
267 |
prompt: str
|
@@ -275,14 +358,22 @@ class SendTextIntent(BaseIntent):
|
|
275 |
|
276 |
# list of all intent models
|
277 |
INTENT_MODELS = [
|
278 |
-
JokeIntent, WeatherIntent,
|
279 |
-
InspireIntent,
|
|
|
280 |
]
|
281 |
|
282 |
ACTION_HANDLERS = {
|
|
|
|
|
283 |
"joke": lambda mid,cid,**i: _fn_joke(mid,cid),
|
284 |
"weather": lambda mid,cid,**i: _fn_weather(mid,cid,i["location"]),
|
285 |
"inspire": lambda mid,cid,**i: _fn_inspire(mid,cid),
|
|
|
|
|
|
|
|
|
|
|
286 |
"generate_image": _fn_generate_images,
|
287 |
"send_text": lambda mid,cid,**i: _fn_send_text(mid,cid,i["message"]),
|
288 |
}
|
@@ -293,9 +384,16 @@ def route_intent(user_input: str, chat_id: str, sender: str):
|
|
293 |
history_text = get_history_text(chat_id, sender)
|
294 |
sys_prompt = (
|
295 |
"You are Eve. You can either chat or call one of these functions:\n"
|
|
|
|
|
296 |
"- joke()\n"
|
297 |
"- weather(location)\n"
|
298 |
"- inspire()\n"
|
|
|
|
|
|
|
|
|
|
|
299 |
"- generate_image(prompt, count, width, height)\n"
|
300 |
"- send_text(message)\n\n"
|
301 |
"Return only raw JSON matching one of these shapes. For example:\n"
|
@@ -378,9 +476,13 @@ app = FastAPI()
|
|
378 |
help_text = (
|
379 |
"π€ *Eve* commands:\n"
|
380 |
"β’ /help\n"
|
|
|
|
|
381 |
"β’ /joke\n"
|
382 |
"β’ /weather <loc>\n"
|
383 |
"β’ /inspire\n"
|
|
|
|
|
384 |
"β’ /gen <prompt>|<count>|<width>|<height>\n"
|
385 |
"Otherwise chat or reply to my message to invoke tools."
|
386 |
)
|
@@ -414,6 +516,13 @@ async def whatsapp_webhook(request: Request):
|
|
414 |
if low == "/help":
|
415 |
_fn_send_text(mid, chat_id, help_text)
|
416 |
return {"success": True}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
417 |
if low == "/joke":
|
418 |
_fn_joke(mid, chat_id)
|
419 |
return {"success": True}
|
@@ -423,6 +532,22 @@ async def whatsapp_webhook(request: Request):
|
|
423 |
if low == "/inspire":
|
424 |
_fn_inspire(mid, chat_id)
|
425 |
return {"success": True}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
426 |
if low.startswith("/gen"):
|
427 |
parts = body[4:].split("|")
|
428 |
pr = parts[0].strip()
|
|
|
53 |
|
54 |
# --- Conversation History -------------------------------------------------
|
55 |
|
56 |
+
history = defaultdict(lambda: deque(maxlen=20))
|
57 |
|
58 |
def record_user_message(chat_id, sender, message):
|
59 |
history[(chat_id, sender)].append(f"User: {message}")
|
|
|
185 |
if chat_id and sender:
|
186 |
record_bot_message(chat_id, sender, message)
|
187 |
|
188 |
+
def _fn_summarize(mid, cid, text):
|
189 |
+
summary = generate_llm(f"Summarize:\n\n{text}")
|
190 |
+
_fn_send_text(mid, cid, summary)
|
191 |
+
|
192 |
+
def _fn_translate(mid, cid, lang, text):
|
193 |
+
resp = generate_llm(f"Translate to {lang}:\n\n{text}")
|
194 |
+
_fn_send_text(mid, cid, resp)
|
195 |
+
|
196 |
def _fn_joke(mid, cid):
|
197 |
try:
|
198 |
j = requests.get(
|
|
|
213 |
quote = generate_llm("Give me a unique, random short inspirational quote.")
|
214 |
_fn_send_text(mid, cid, f"β¨ {quote}")
|
215 |
|
216 |
+
def _fn_meme(mid, cid, txt):
|
217 |
+
_fn_send_accept(mid, cid, "π¨ Generating memeβ¦")
|
218 |
+
task_queue.put({
|
219 |
+
"type": "image",
|
220 |
+
"message_id": mid,
|
221 |
+
"chat_id": cid,
|
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,
|
|
|
306 |
class BaseIntent(BaseModel):
|
307 |
action: str
|
308 |
|
309 |
+
class SummarizeIntent(BaseIntent):
|
310 |
+
action: Literal["summarize"]
|
311 |
+
text: str
|
312 |
+
|
313 |
+
class TranslateIntent(BaseIntent):
|
314 |
+
action: Literal["translate"]
|
315 |
+
lang: str
|
316 |
+
text: str
|
317 |
+
|
318 |
class JokeIntent(BaseIntent):
|
319 |
action: Literal["joke"]
|
320 |
|
|
|
325 |
class InspireIntent(BaseIntent):
|
326 |
action: Literal["inspire"]
|
327 |
|
328 |
+
class MemeIntent(BaseIntent):
|
329 |
+
action: Literal["meme"]
|
330 |
+
text: str
|
331 |
+
|
332 |
+
class PollCreateIntent(BaseIntent):
|
333 |
+
action: Literal["poll_create"]
|
334 |
+
question: str
|
335 |
+
options: List[str]
|
336 |
+
|
337 |
+
class PollVoteIntent(BaseIntent):
|
338 |
+
action: Literal["poll_vote"]
|
339 |
+
voter: str
|
340 |
+
choice: int
|
341 |
+
|
342 |
+
class PollResultsIntent(BaseIntent):
|
343 |
+
action: Literal["poll_results"]
|
344 |
+
|
345 |
+
class PollEndIntent(BaseIntent):
|
346 |
+
action: Literal["poll_end"]
|
347 |
+
|
348 |
class GenerateImageIntent(BaseModel):
|
349 |
action: Literal["generate_image"]
|
350 |
prompt: str
|
|
|
358 |
|
359 |
# list of all intent models
|
360 |
INTENT_MODELS = [
|
361 |
+
SummarizeIntent, TranslateIntent, JokeIntent, WeatherIntent,
|
362 |
+
InspireIntent, MemeIntent, PollCreateIntent, PollVoteIntent,
|
363 |
+
PollResultsIntent, PollEndIntent, GenerateImageIntent, SendTextIntent
|
364 |
]
|
365 |
|
366 |
ACTION_HANDLERS = {
|
367 |
+
"summarize": lambda mid,cid,**i: _fn_summarize(mid,cid,i["text"]),
|
368 |
+
"translate": lambda mid,cid,**i: _fn_translate(mid,cid,i["lang"],i["text"]),
|
369 |
"joke": lambda mid,cid,**i: _fn_joke(mid,cid),
|
370 |
"weather": lambda mid,cid,**i: _fn_weather(mid,cid,i["location"]),
|
371 |
"inspire": lambda mid,cid,**i: _fn_inspire(mid,cid),
|
372 |
+
"meme": lambda mid,cid,**i: _fn_meme(mid,cid,i["text"]),
|
373 |
+
"poll_create": lambda mid,cid,**i: _fn_poll_create(mid,cid,i["question"],i["options"]),
|
374 |
+
"poll_vote": lambda mid,cid,**i: _fn_poll_vote(mid,cid,i["voter"],i["choice"]),
|
375 |
+
"poll_results": lambda mid,cid,**i: _fn_poll_results(mid,cid),
|
376 |
+
"poll_end": lambda mid,cid,**i: _fn_poll_end(mid,cid),
|
377 |
"generate_image": _fn_generate_images,
|
378 |
"send_text": lambda mid,cid,**i: _fn_send_text(mid,cid,i["message"]),
|
379 |
}
|
|
|
384 |
history_text = get_history_text(chat_id, sender)
|
385 |
sys_prompt = (
|
386 |
"You are Eve. You can either chat or call one of these functions:\n"
|
387 |
+
"- summarize(text)\n"
|
388 |
+
"- translate(lang, text)\n"
|
389 |
"- joke()\n"
|
390 |
"- weather(location)\n"
|
391 |
"- inspire()\n"
|
392 |
+
"- meme(text)\n"
|
393 |
+
"- poll_create(question, options)\n"
|
394 |
+
"- poll_vote(voter, choice)\n"
|
395 |
+
"- poll_results()\n"
|
396 |
+
"- poll_end()\n"
|
397 |
"- generate_image(prompt, count, width, height)\n"
|
398 |
"- send_text(message)\n\n"
|
399 |
"Return only raw JSON matching one of these shapes. For example:\n"
|
|
|
476 |
help_text = (
|
477 |
"π€ *Eve* commands:\n"
|
478 |
"β’ /help\n"
|
479 |
+
"β’ /summarize <text>\n"
|
480 |
+
"β’ /translate <lang>|<text>\n"
|
481 |
"β’ /joke\n"
|
482 |
"β’ /weather <loc>\n"
|
483 |
"β’ /inspire\n"
|
484 |
+
"β’ /meme <text>\n"
|
485 |
+
"β’ /poll <Q>|β¦ / /results / /endpoll\n"
|
486 |
"β’ /gen <prompt>|<count>|<width>|<height>\n"
|
487 |
"Otherwise chat or reply to my message to invoke tools."
|
488 |
)
|
|
|
516 |
if low == "/help":
|
517 |
_fn_send_text(mid, chat_id, help_text)
|
518 |
return {"success": True}
|
519 |
+
if low.startswith("/summarize "):
|
520 |
+
_fn_summarize(mid, chat_id, body[11:].strip())
|
521 |
+
return {"success": True}
|
522 |
+
if low.startswith("/translate "):
|
523 |
+
lang, txt = body[11:].split("|", 1)
|
524 |
+
_fn_translate(mid, chat_id, lang.strip(), txt.strip())
|
525 |
+
return {"success": True}
|
526 |
if low == "/joke":
|
527 |
_fn_joke(mid, chat_id)
|
528 |
return {"success": True}
|
|
|
532 |
if low == "/inspire":
|
533 |
_fn_inspire(mid, chat_id)
|
534 |
return {"success": True}
|
535 |
+
if low.startswith("/meme "):
|
536 |
+
_fn_meme(mid, chat_id, body[6:].strip())
|
537 |
+
return {"success": True}
|
538 |
+
if low.startswith("/poll "):
|
539 |
+
parts = [p.strip() for p in body[6:].split("|")]
|
540 |
+
_fn_poll_create(mid, chat_id, parts[0], parts[1:])
|
541 |
+
return {"success": True}
|
542 |
+
if chat_id in polls and low.isdigit():
|
543 |
+
_fn_poll_vote(mid, chat_id, sender, int(low))
|
544 |
+
return {"success": True}
|
545 |
+
if low == "/results":
|
546 |
+
_fn_poll_results(mid, chat_id)
|
547 |
+
return {"success": True}
|
548 |
+
if low == "/endpoll":
|
549 |
+
_fn_poll_end(mid, chat_id)
|
550 |
+
return {"success": True}
|
551 |
if low.startswith("/gen"):
|
552 |
parts = body[4:].split("|")
|
553 |
pr = parts[0].strip()
|
config.yaml
CHANGED
@@ -1,13 +1,20 @@
|
|
1 |
config:
|
2 |
llm:
|
|
|
3 |
system_prompt: |-
|
4 |
You are {char}, a sweet and helpful AI assistant in Telegram and WhatsApp.
|
5 |
You generate images, voice and text replies, and support these commands:
|
6 |
β’ /help β list all commands
|
7 |
β’ /gen <prompt>|<count>|<width>|<height> β generate <count> images (default 4) posible width, height 512x512 (squre), 512x1024 (portrait), 1024x512, 1024x1024, 1704x960 (landscape)
|
|
|
|
|
8 |
β’ /joke β tell a short joke
|
9 |
β’ /weather <location> β short, creative weather report in Β°C
|
10 |
β’ /inspire β inspirational quote
|
|
|
|
|
|
|
|
|
11 |
Use a concise, friendly tone. If a command is malformed, gently ask the user to correct it.
|
12 |
For any other message, you can either chat normally or invoke one of your tools.
|
13 |
char: Eve
|
|
|
1 |
config:
|
2 |
llm:
|
3 |
+
model: koboldcpp/HF_SPACE_Tiefighter-13B
|
4 |
system_prompt: |-
|
5 |
You are {char}, a sweet and helpful AI assistant in Telegram and WhatsApp.
|
6 |
You generate images, voice and text replies, and support these commands:
|
7 |
β’ /help β list all commands
|
8 |
β’ /gen <prompt>|<count>|<width>|<height> β generate <count> images (default 4) posible width, height 512x512 (squre), 512x1024 (portrait), 1024x512, 1024x1024, 1704x960 (landscape)
|
9 |
+
β’ /summarize <text> β get a concise summary
|
10 |
+
β’ /translate <lang>|<text> β translate text
|
11 |
β’ /joke β tell a short joke
|
12 |
β’ /weather <location> β short, creative weather report in Β°C
|
13 |
β’ /inspire β inspirational quote
|
14 |
+
β’ /meme <text> β generate a meme image
|
15 |
+
β’ /poll <Q>|<opt1>|<opt2>|β¦ β create a poll
|
16 |
+
β’ /results β show poll results
|
17 |
+
β’ /endpoll β end the poll
|
18 |
Use a concise, friendly tone. If a command is malformed, gently ask the user to correct it.
|
19 |
For any other message, you can either chat normally or invoke one of your tools.
|
20 |
char: Eve
|