Spaces:
Running
Running
Upload telegram_bot.py with huggingface_hub
Browse files- telegram_bot.py +25 -25
telegram_bot.py
CHANGED
|
@@ -111,30 +111,30 @@ def call_kora_chat(prompt: str, model: str = "kora-chat-0.5b") -> str:
|
|
| 111 |
# 1. Try direct in-memory generation if running within gateway process
|
| 112 |
try:
|
| 113 |
import sys
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
except Exception as err:
|
| 139 |
print(f"ℹ️ In-memory inference notice: {err}", flush=True)
|
| 140 |
|
|
@@ -202,7 +202,7 @@ def handle_update(update: dict, token: str = None):
|
|
| 202 |
img_url = call_kora_image(prompt)
|
| 203 |
send_telegram_photo(chat_id, img_url, f"✨ {prompt}")
|
| 204 |
elif text.startswith("/code"):
|
| 205 |
-
prompt = text.replace("/code", "").strip()
|
| 206 |
send_telegram_message(chat_id, "💻 Generating code with Kora Coder...")
|
| 207 |
reply = call_kora_chat(prompt, model="kora-coder-0.5b")
|
| 208 |
send_telegram_message(chat_id, f"```\n{reply}\n```")
|
|
|
|
| 111 |
# 1. Try direct in-memory generation if running within gateway process
|
| 112 |
try:
|
| 113 |
import sys
|
| 114 |
+
gw = sys.modules.get("gateway") or sys.modules.get("__main__")
|
| 115 |
+
if gw and hasattr(gw, "CODER_MODEL") and hasattr(gw, "CODER_TOKENIZER"):
|
| 116 |
+
tok = gw.CODER_TOKENIZER
|
| 117 |
+
mdl = gw.CODER_MODEL
|
| 118 |
+
is_coder = "coder" in model or "code" in prompt.lower()
|
| 119 |
+
system_prompt = "You are Kora Coder, an expert AI programming assistant built by Kontyra." if is_coder else "You are Kora, an intelligent, helpful, and friendly AI assistant built by Kontyra."
|
| 120 |
+
messages = [
|
| 121 |
+
{"role": "system", "content": system_prompt},
|
| 122 |
+
{"role": "user", "content": prompt}
|
| 123 |
+
]
|
| 124 |
+
prompt_str = tok.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 125 |
+
device = next(mdl.parameters()).device
|
| 126 |
+
inputs = tok([prompt_str], return_tensors="pt").to(device)
|
| 127 |
+
import torch
|
| 128 |
+
with torch.no_grad():
|
| 129 |
+
out = mdl.generate(
|
| 130 |
+
**inputs,
|
| 131 |
+
max_new_tokens=300,
|
| 132 |
+
temperature=0.3 if is_coder else 0.7,
|
| 133 |
+
pad_token_id=tok.eos_token_id
|
| 134 |
+
)
|
| 135 |
+
reply = tok.decode(out[0][inputs.input_ids.shape[1]:], skip_special_tokens=True).strip()
|
| 136 |
+
if reply:
|
| 137 |
+
return reply
|
| 138 |
except Exception as err:
|
| 139 |
print(f"ℹ️ In-memory inference notice: {err}", flush=True)
|
| 140 |
|
|
|
|
| 202 |
img_url = call_kora_image(prompt)
|
| 203 |
send_telegram_photo(chat_id, img_url, f"✨ {prompt}")
|
| 204 |
elif text.startswith("/code"):
|
| 205 |
+
prompt = text.replace("/code", "").strip() or "Write a Python function to demonstrate binary search"
|
| 206 |
send_telegram_message(chat_id, "💻 Generating code with Kora Coder...")
|
| 207 |
reply = call_kora_chat(prompt, model="kora-coder-0.5b")
|
| 208 |
send_telegram_message(chat_id, f"```\n{reply}\n```")
|