fix: more info and ci

Signed-off-by: yihong0618 <zouzou0208@gmail.com>
This commit is contained in:
yihong0618 2023-12-15 12:57:32 +08:00
parent 08e9a41d7b
commit dbacdd60f0
2 changed files with 72 additions and 4 deletions

27
.github/workflows/CI.yaml vendored Normal file
View File

@ -0,0 +1,27 @@
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:
concurrency:
group: ${{ github.event.number || github.run_id }}
cancel-in-progress: true
jobs:
testing:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: install python 3.9
uses: actions/setup-python@v4
with:
python-version: "3.9"
cache: "pip" # caching pip dependencies
- name: Check formatting (black)
run: |
pip install black
black . --check

49
tg.py
View File

@ -7,6 +7,8 @@ import traceback
from tempfile import SpooledTemporaryFile from tempfile import SpooledTemporaryFile
from os import environ from os import environ
from pathlib import Path from pathlib import Path
from typing import Optional
import numpy as np import numpy as np
import PIL import PIL
@ -47,6 +49,36 @@ safety_settings = [
}, },
] ]
BOT_STARTS_WORDS_LIST = ["map:", "/map", "github:", "/github", "gemini", "/gemini"]
#### Utils ####
def extract_prompt(message: Message, bot_name: str) -> Optional[str]:
"""
This function filters messages for prompts.
Returns:
str: If it is not a prompt, return None. Otherwise, return the trimmed prefix of the actual prompt.
"""
msg_text: str = message.text.strip()
if msg_text.startswith("@"):
if not msg_text.startswith(f"@{bot_name} "):
return None
s = msg_text[len(bot_name) + 2 :]
else:
prefix = next(
(w for w in BOT_STARTS_WORDS_LIST if msg_text.startswith(w)), None
)
if not prefix:
return None
s = msg_text[len(prefix) :]
# If the first word is '@bot_name', remove it as it is considered part of the command when in a group chat.
if s.startswith("@"):
if not s.startswith(f"@{bot_name} "):
return None
s = " ".join(s.split(" ")[1:])
return s
def make_new_gemini_convo(): def make_new_gemini_convo():
model = genai.GenerativeModel( model = genai.GenerativeModel(
@ -143,13 +175,17 @@ def main():
BotCommand("gemini", "Gemini : /gemini <question>"), BotCommand("gemini", "Gemini : /gemini <question>"),
] ]
) )
bot_name = bot.get_me().username
print("Bot init done.") print("Bot init done.")
@bot.message_handler(commands=["github"]) @bot.message_handler(commands=["github"])
@bot.message_handler(regexp="^github:") @bot.message_handler(regexp="^github:")
def github_poster_handler(message: Message): def github_poster_handler(message: Message):
reply_message = bot.reply_to(message, "Generating poster please wait:") reply_message = bot.reply_to(message, "Generating poster please wait:")
m = message.text.strip().split(maxsplit=1)[1].strip() m = extract_prompt(message, bot_name)
if not m:
bot.reply_to(message, "Please provide info after start words.")
return
message_list = m.split(",") message_list = m.split(",")
name = message_list[0].strip() name = message_list[0].strip()
cmd_list = ["github_poster", "github", "--github_user_name", name, "--me", name] cmd_list = ["github_poster", "github", "--github_user_name", name, "--me", name]
@ -179,7 +215,10 @@ def main():
reply_message = bot.reply_to( reply_message = bot.reply_to(
message, "Generating pretty map may take some time please wait:" message, "Generating pretty map may take some time please wait:"
) )
m = message.text.strip().split(maxsplit=1)[1].strip() m = extract_prompt(message, bot_name)
if not m:
bot.reply_to(message, "Please provide info after start words.")
return
location = m.strip() location = m.strip()
styles_list = list(STYLES.keys()) styles_list = list(STYLES.keys())
style = random.choice(styles_list) style = random.choice(styles_list)
@ -236,7 +275,10 @@ def main():
message, message,
"Generating google gemini answer please wait, note, will only keep the last five messages:", "Generating google gemini answer please wait, note, will only keep the last five messages:",
) )
m = message.text.strip().split(maxsplit=1)[1].strip() m = extract_prompt(message, bot_name)
if not m:
bot.reply_to(message, "Please provide info after start words.")
return
player = None player = None
# restart will lose all TODO # restart will lose all TODO
if str(message.from_user.id) not in gemini_player_dict: if str(message.from_user.id) not in gemini_player_dict:
@ -290,7 +332,6 @@ def main():
} }
try: try:
response = model.generate_content(contents=contents) response = model.generate_content(contents=contents)
print(response.text)
bot.reply_to(message, "Gemini vision answer:\n" + response.text) bot.reply_to(message, "Gemini vision answer:\n" + response.text)
except Exception as e: except Exception as e:
traceback.print_exc() traceback.print_exc()