ProfessorCEO commited on
Commit
eb02f98
·
verified ·
1 Parent(s): 8c4718b

Upload telegram_bot.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. 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
- if "gateway" in sys.modules:
115
- gw = sys.modules["gateway"]
116
- if hasattr(gw, "CODER_MODEL") and hasattr(gw, "CODER_TOKENIZER"):
117
- tok = gw.CODER_TOKENIZER
118
- mdl = gw.CODER_MODEL
119
- system_prompt = "You are Kora, an intelligent, helpful, and friendly AI assistant built by Kontyra." if "coder" not in model else "You are Kora Coder, an expert AI programming 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=256,
132
- temperature=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,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```")