fix: markdown format

This commit is contained in:
yihong0618 2023-12-17 22:56:54 +08:00
parent cb739306ec
commit e56b35cfd1
3 changed files with 116 additions and 5 deletions

@ -41,6 +41,7 @@ for yihong0618's channel: https://t.me/hyi0618
- poster use my repo -> https://github.com/yihong0618/GitHubPoster
- pretty map use wonder repo -> https://github.com/chrieke/prettymapp
- Gemini use -> https://github.com/google/generative-ai-python
- Telegram markdownV2 change code copy from https://github.com/yym68686/md2tgmd/blob/main/src/md2tgmd.py thanks a lot.
## Appreciation

@ -48,9 +48,13 @@ def wrap_handler(handler: T, bot: TeleBot) -> T:
bot.reply_to(message, "Please provide info after start words.")
return
return handler(message, *args, **kwargs)
except Exception:
except Exception as e:
traceback.print_exc()
bot.reply_to(message, "Something wrong, please check the log")
# handle more here
if str(e).find("RECITATION") > 0:
bot.reply_to(message, "Your prompt `RECITATION` please check the log")
else:
bot.reply_to(message, "Something wrong, please check the log")
return update_wrapper(wrapper, handler)

@ -1,5 +1,6 @@
from os import environ
from pathlib import Path
import re
import google.generativeai as genai
from telebot import TeleBot
@ -32,6 +33,107 @@ safety_settings = [
gemini_player_dict = {}
#### Utils for gemini ####
# Note this code copy from https://github.com/yym68686/md2tgmd/blob/main/src/md2tgmd.py
# great thanks
def find_all_index(str, pattern):
index_list = [0]
for match in re.finditer(pattern, str, re.MULTILINE):
if match.group(1) != None:
start = match.start(1)
end = match.end(1)
index_list += [start, end]
index_list.append(len(str))
return index_list
def replace_all(text, pattern, function):
poslist = [0]
strlist = []
originstr = []
poslist = find_all_index(text, pattern)
for i in range(1, len(poslist[:-1]), 2):
start, end = poslist[i : i + 2]
strlist.append(function(text[start:end]))
for i in range(0, len(poslist), 2):
j, k = poslist[i : i + 2]
originstr.append(text[j:k])
if len(strlist) < len(originstr):
strlist.append("")
else:
originstr.append("")
new_list = [item for pair in zip(originstr, strlist) for item in pair]
return "".join(new_list)
def escapeshape(text):
return "▎*" + text.split()[1] + "*"
def escapeminus(text):
return "\\" + text
def escapebackquote(text):
return r"\`\`"
def escapeplus(text):
return "\\" + text
def escape(text, flag=0):
# In all other places characters
# _ * [ ] ( ) ~ ` > # + - = | { } . !
# must be escaped with the preceding character '\'.
text = re.sub(r"\\\[", "@->@", text)
text = re.sub(r"\\\]", "@<-@", text)
text = re.sub(r"\\\(", "@-->@", text)
text = re.sub(r"\\\)", "@<--@", text)
if flag:
text = re.sub(r"\\\\", "@@@", text)
text = re.sub(r"\\", r"\\\\", text)
if flag:
text = re.sub(r"\@{3}", r"\\\\", text)
text = re.sub(r"_", "\_", text)
text = re.sub(r"\*{2}(.*?)\*{2}", "@@@\\1@@@", text)
text = re.sub(r"\n{1,2}\*\s", "\n\n", text)
text = re.sub(r"\*", "\*", text)
text = re.sub(r"\@{3}(.*?)\@{3}", "*\\1*", text)
text = re.sub(r"\!?\[(.*?)\]\((.*?)\)", "@@@\\1@@@^^^\\2^^^", text)
text = re.sub(r"\[", "\[", text)
text = re.sub(r"\]", "\]", text)
text = re.sub(r"\(", "\(", text)
text = re.sub(r"\)", "\)", text)
text = re.sub(r"\@\-\>\@", "\[", text)
text = re.sub(r"\@\<\-\@", "\]", text)
text = re.sub(r"\@\-\-\>\@", "\(", text)
text = re.sub(r"\@\<\-\-\@", "\)", text)
text = re.sub(r"\@{3}(.*?)\@{3}\^{3}(.*?)\^{3}", "[\\1](\\2)", text)
text = re.sub(r"~", "\~", text)
text = re.sub(r">", "\>", text)
text = replace_all(text, r"(^#+\s.+?$)|```[\D\d\s]+?```", escapeshape)
text = re.sub(r"#", "\#", text)
text = replace_all(
text, r"(\+)|\n[\s]*-\s|```[\D\d\s]+?```|`[\D\d\s]*?`", escapeplus
)
text = re.sub(r"\n{1,2}(\s*)-\s", "\n\n\\1• ", text)
text = re.sub(r"\n{1,2}(\s*\d{1,2}\.\s)", "\n\n\\1", text)
text = replace_all(
text, r"(-)|\n[\s]*-\s|```[\D\d\s]+?```|`[\D\d\s]*?`", escapeminus
)
text = re.sub(r"```([\D\d\s]+?)```", "@@@\\1@@@", text)
text = replace_all(text, r"(``)", escapebackquote)
text = re.sub(r"\@{3}([\D\d\s]+?)\@{3}", "```\\1```", text)
text = re.sub(r"=", "\=", text)
text = re.sub(r"\|", "\|", text)
text = re.sub(r"{", "\{", text)
text = re.sub(r"}", "\}", text)
text = re.sub(r"\.", "\.", text)
text = re.sub(r"!", "\!", text)
return text
def make_new_gemini_convo():
model = genai.GenerativeModel(
model_name="gemini-pro",
@ -56,19 +158,22 @@ def gemini_handler(message: Message, bot: TeleBot) -> None:
gemini_player_dict[str(message.from_user.id)] = player
else:
player = gemini_player_dict[str(message.from_user.id)]
# keep the last 5, every has two ask and answer.
if len(player.history) > 10:
player.history = player.history[2:]
player.send_message(m)
gemini_reply_text = player.last.text.strip()
try:
bot.reply_to(
message,
"Gemini answer:\n" + player.last.text,
"Gemini answer:\n" + escape(gemini_reply_text),
parse_mode="MarkdownV2",
)
except:
print("wrong markdown format")
bot.reply_to(
message,
"Gemini answer:\n" + player.last.text,
"Gemini answer:\n\n" + gemini_reply_text,
)
finally:
bot.delete_message(reply_message.chat.id, reply_message.message_id)
@ -78,9 +183,10 @@ def gemini_photo_handler(message: Message, bot: TeleBot) -> None:
s = message.caption
reply_message = bot.reply_to(
message,
"Generating google gemini vision answer please wait,",
"Generating google gemini vision answer please wait.",
)
prompt = s.strip()
# get the high quaility picture.
max_size_photo = max(message.photo, key=lambda p: p.file_size)
file_path = bot.get_file(max_size_photo.file_id).file_path
downloaded_file = bot.download_file(file_path)