fix syntax
This commit is contained in:
@@ -148,16 +148,18 @@ async def private_voice(update: Update, context: ContextTypes.DEFAULT_TYPE) -> N
|
||||
)
|
||||
status_message = await update.message.reply_text("Распознаю голосовое сообщение...")
|
||||
temp_path: Path | None = None
|
||||
transcript = ""
|
||||
|
||||
try:
|
||||
telegram_file = await context.bot.get_file(voice.file_id)
|
||||
file_descriptor, raw_path = tempfile.mkstemp(suffix=".ogg")
|
||||
os.close(file_descriptor)
|
||||
temp_path = Path(raw_path)
|
||||
await telegram_file.download_to_drive(custom_path=temp_path)
|
||||
voice_path = Path(raw_path)
|
||||
temp_path = voice_path
|
||||
await telegram_file.download_to_drive(custom_path=voice_path)
|
||||
transcript = await asyncio.to_thread(
|
||||
get_speech_recognizer(context).transcribe,
|
||||
temp_path,
|
||||
voice_path,
|
||||
)
|
||||
except (SpeechRecognitionError, TelegramError):
|
||||
logger.exception("Failed to transcribe Telegram voice message")
|
||||
@@ -364,7 +366,7 @@ async def remind_command(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
||||
|
||||
reminder_id = get_storage(context).add_reminder(
|
||||
user_id=user_id,
|
||||
chat_id=int(update.effective_chat.id),
|
||||
chat_id=int(update.message.chat_id),
|
||||
text=parsed.text,
|
||||
remind_at_utc=parsed.remind_at_utc,
|
||||
)
|
||||
|
||||
@@ -2,7 +2,7 @@ import sys
|
||||
import threading
|
||||
import warnings
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable, Protocol
|
||||
from typing import Any, Callable, Protocol, cast
|
||||
|
||||
|
||||
class SpeechRecognitionError(RuntimeError):
|
||||
@@ -21,9 +21,9 @@ def load_whisper_model_class() -> Any:
|
||||
missing = object()
|
||||
loaded_torch = sys.modules.get("torch", missing)
|
||||
if loaded_torch is missing:
|
||||
sys.modules["torch"] = None
|
||||
sys.modules["torch"] = cast(Any, None)
|
||||
try:
|
||||
with warnings.catch_warnings():
|
||||
with warnings.catch_warnings(record=False):
|
||||
warnings.filterwarnings(
|
||||
"ignore",
|
||||
message="pkg_resources is deprecated as an API.*",
|
||||
|
||||
@@ -9,6 +9,17 @@ from .config import get_default_model
|
||||
from .time_utils import to_utc_iso, utc_now
|
||||
|
||||
|
||||
def row_to_dict(row: sqlite3.Row) -> dict[str, Any]:
|
||||
return {str(key): row[key] for key in row.keys()}
|
||||
|
||||
|
||||
def required_lastrowid(cursor: sqlite3.Cursor) -> int:
|
||||
lastrowid = cursor.lastrowid
|
||||
if lastrowid is None:
|
||||
raise RuntimeError("SQLite insert did not return a row ID")
|
||||
return lastrowid
|
||||
|
||||
|
||||
class AssistantStorage:
|
||||
def __init__(self, path: Path) -> None:
|
||||
self.path = path
|
||||
@@ -162,7 +173,7 @@ class AssistantStorage:
|
||||
""",
|
||||
(user_id, chat_id, started_after_id, limit),
|
||||
).fetchall()
|
||||
return [dict(row) for row in reversed(rows)]
|
||||
return [row_to_dict(row) for row in reversed(rows)]
|
||||
|
||||
def start_new_conversation(self, user_id: int, chat_id: int) -> None:
|
||||
now = to_utc_iso(utc_now())
|
||||
@@ -230,7 +241,7 @@ class AssistantStorage:
|
||||
age = max(0, newest_id - int(row["id"]))
|
||||
recency_weight = 1.0 / (1.0 + age / 50.0)
|
||||
score = (coverage + phrase_bonus) * (0.5 + 0.5 * recency_weight)
|
||||
item = dict(row)
|
||||
item = row_to_dict(row)
|
||||
item["score"] = round(score, 4)
|
||||
matches.append((score, item))
|
||||
|
||||
@@ -266,7 +277,7 @@ class AssistantStorage:
|
||||
(user_id, chat_id, message_id, radius),
|
||||
).fetchall()
|
||||
rows = [*reversed(before), *after]
|
||||
return [dict(row) for row in rows]
|
||||
return [row_to_dict(row) for row in rows]
|
||||
|
||||
def get_user_model(self, user_id: int, provider: str = "local") -> str:
|
||||
column = self._model_column(provider)
|
||||
@@ -315,7 +326,7 @@ class AssistantStorage:
|
||||
"INSERT INTO memories(user_id, text, created_at) VALUES (?, ?, ?)",
|
||||
(user_id, text, to_utc_iso(utc_now())),
|
||||
)
|
||||
return int(cursor.lastrowid)
|
||||
return required_lastrowid(cursor)
|
||||
|
||||
def list_memories(self, user_id: int, limit: int = 20) -> list[dict[str, Any]]:
|
||||
with self._connection() as connection:
|
||||
@@ -329,7 +340,7 @@ class AssistantStorage:
|
||||
""",
|
||||
(user_id, limit),
|
||||
).fetchall()
|
||||
return [dict(row) for row in rows]
|
||||
return [row_to_dict(row) for row in rows]
|
||||
|
||||
def delete_memory(self, user_id: int, memory_id: int) -> bool:
|
||||
with self._connection() as connection:
|
||||
@@ -345,7 +356,7 @@ class AssistantStorage:
|
||||
"INSERT INTO notes(user_id, text, created_at) VALUES (?, ?, ?)",
|
||||
(user_id, text, to_utc_iso(utc_now())),
|
||||
)
|
||||
return int(cursor.lastrowid)
|
||||
return required_lastrowid(cursor)
|
||||
|
||||
def list_notes(self, user_id: int, limit: int = 20) -> list[dict[str, Any]]:
|
||||
with self._connection() as connection:
|
||||
@@ -359,7 +370,7 @@ class AssistantStorage:
|
||||
""",
|
||||
(user_id, limit),
|
||||
).fetchall()
|
||||
return [dict(row) for row in rows]
|
||||
return [row_to_dict(row) for row in rows]
|
||||
|
||||
def delete_note(self, user_id: int, note_id: int) -> bool:
|
||||
with self._connection() as connection:
|
||||
@@ -390,7 +401,7 @@ class AssistantStorage:
|
||||
to_utc_iso(utc_now()),
|
||||
),
|
||||
)
|
||||
return int(cursor.lastrowid)
|
||||
return required_lastrowid(cursor)
|
||||
|
||||
def list_reminders(self, user_id: int, limit: int = 20) -> list[dict[str, Any]]:
|
||||
with self._connection() as connection:
|
||||
@@ -404,7 +415,7 @@ class AssistantStorage:
|
||||
""",
|
||||
(user_id, limit),
|
||||
).fetchall()
|
||||
return [dict(row) for row in rows]
|
||||
return [row_to_dict(row) for row in rows]
|
||||
|
||||
def cancel_reminder(self, user_id: int, reminder_id: int) -> bool:
|
||||
with self._connection() as connection:
|
||||
@@ -430,7 +441,7 @@ class AssistantStorage:
|
||||
""",
|
||||
(to_utc_iso(now_utc), limit),
|
||||
).fetchall()
|
||||
return [dict(row) for row in rows]
|
||||
return [row_to_dict(row) for row in rows]
|
||||
|
||||
def mark_reminder_sent(self, reminder_id: int) -> None:
|
||||
with self._connection() as connection:
|
||||
@@ -453,7 +464,7 @@ class AssistantStorage:
|
||||
""",
|
||||
(user_id, title, status, now, now),
|
||||
)
|
||||
return int(cursor.lastrowid)
|
||||
return required_lastrowid(cursor)
|
||||
|
||||
def list_tracked_items(self, user_id: int, limit: int = 30) -> list[dict[str, Any]]:
|
||||
with self._connection() as connection:
|
||||
@@ -467,7 +478,7 @@ class AssistantStorage:
|
||||
""",
|
||||
(user_id, limit),
|
||||
).fetchall()
|
||||
return [dict(row) for row in rows]
|
||||
return [row_to_dict(row) for row in rows]
|
||||
|
||||
def set_tracked_status(self, user_id: int, item_id: int, status: str) -> bool:
|
||||
with self._connection() as connection:
|
||||
|
||||
@@ -57,12 +57,13 @@ def get_voice_max_duration(context: ContextTypes.DEFAULT_TYPE) -> int:
|
||||
|
||||
|
||||
def command_text(context: ContextTypes.DEFAULT_TYPE) -> str:
|
||||
return " ".join(context.args).strip()
|
||||
return " ".join(context.args or []).strip()
|
||||
|
||||
|
||||
def require_user_id(update: Update) -> int | None:
|
||||
if update.effective_user:
|
||||
return int(update.effective_user.id)
|
||||
user = update.effective_user
|
||||
if user:
|
||||
return int(user.id)
|
||||
return None
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user