ysn-rfd commited on
Commit
5d5ac92
·
verified ·
1 Parent(s): 575b072

Update runs/python/telegram_gemini2.py

Browse files
Files changed (1) hide show
  1. runs/python/telegram_gemini2.py +56 -56
runs/python/telegram_gemini2.py CHANGED
@@ -1,56 +1,56 @@
1
- import asyncio
2
- import aiohttp # برای درخواست‌های async
3
- from telegram import Update
4
- from telegram.ext import ApplicationBuilder, MessageHandler, ContextTypes, filters
5
- import logging
6
-
7
- # تنظیمات
8
- BOT_TOKEN = "7490823724:AAEcskSIKg9t63nBME3Igkxw_QE4dl2Ql_U"
9
- LLAMA_API_URL = "http://127.0.0.1:8080/completion"
10
-
11
- # لاگ‌گیری
12
- logging.basicConfig(
13
- format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
14
- )
15
-
16
- # گرفتن پاسخ از llama.cpp به صورت async
17
- async def get_llama_response(prompt: str) -> str:
18
- system_prompt = f"User: {prompt}\nAssistant:"
19
- payload = {
20
- "prompt": system_prompt,
21
- "max_tokens": 64,
22
- "temperature": 0.7,
23
- "stop": ["</s>", "User:"]
24
- }
25
- try:
26
- async with aiohttp.ClientSession() as session:
27
- async with session.post(LLAMA_API_URL, json=payload, timeout=60) as resp:
28
- if resp.status == 200:
29
- data = await resp.json()
30
- return data.get("content", "").strip()
31
- else:
32
- logging.error(f"LLaMA API Error: {resp.status}")
33
- return "❌ خطا در دریافت پاسخ از مدل زبان."
34
- except asyncio.TimeoutError:
35
- return "⏱️ مدل دیر پاسخ داد. لطفاً دوباره تلاش کنید."
36
- except Exception as e:
37
- logging.exception("خطا در ارتباط با مدل:")
38
- return "⚠️ خطا در پردازش درخواست شما."
39
-
40
- # هندل کردن پیام‌های دستوری
41
- async def handle_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
42
- message = update.message
43
- if message and message.text:
44
- user_input = message.text.lstrip('/')
45
- await message.chat.send_action("typing")
46
- response = await get_llama_response(user_input)
47
- await message.reply_text(response)
48
-
49
- # راه‌اندازی ربات
50
- def main():
51
- app = ApplicationBuilder().token(BOT_TOKEN).build()
52
- app.add_handler(MessageHandler(filters.COMMAND, handle_command))
53
- app.run_polling()
54
-
55
- if __name__ == "__main__":
56
- main()
 
1
+ import asyncio
2
+ import aiohttp # برای درخواست‌های async
3
+ from telegram import Update
4
+ from telegram.ext import ApplicationBuilder, MessageHandler, ContextTypes, filters
5
+ import logging
6
+
7
+ # تنظیمات
8
+ BOT_TOKEN = ""
9
+ LLAMA_API_URL = "http://127.0.0.1:8080/completion"
10
+
11
+ # لاگ‌گیری
12
+ logging.basicConfig(
13
+ format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
14
+ )
15
+
16
+ # گرفتن پاسخ از llama.cpp به صورت async
17
+ async def get_llama_response(prompt: str) -> str:
18
+ system_prompt = f"User: {prompt}\nAssistant:"
19
+ payload = {
20
+ "prompt": system_prompt,
21
+ "max_tokens": 64,
22
+ "temperature": 0.7,
23
+ "stop": ["</s>", "User:"]
24
+ }
25
+ try:
26
+ async with aiohttp.ClientSession() as session:
27
+ async with session.post(LLAMA_API_URL, json=payload, timeout=60) as resp:
28
+ if resp.status == 200:
29
+ data = await resp.json()
30
+ return data.get("content", "").strip()
31
+ else:
32
+ logging.error(f"LLaMA API Error: {resp.status}")
33
+ return "❌ خطا در دریافت پاسخ از مدل زبان."
34
+ except asyncio.TimeoutError:
35
+ return "⏱️ مدل دیر پاسخ داد. لطفاً دوباره تلاش کنید."
36
+ except Exception as e:
37
+ logging.exception("خطا در ارتباط با مدل:")
38
+ return "⚠️ خطا در پردازش درخواست شما."
39
+
40
+ # هندل کردن پیام‌های دستوری
41
+ async def handle_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
42
+ message = update.message
43
+ if message and message.text:
44
+ user_input = message.text.lstrip('/')
45
+ await message.chat.send_action("typing")
46
+ response = await get_llama_response(user_input)
47
+ await message.reply_text(response)
48
+
49
+ # راه‌اندازی ربات
50
+ def main():
51
+ app = ApplicationBuilder().token(BOT_TOKEN).build()
52
+ app.add_handler(MessageHandler(filters.COMMAND, handle_command))
53
+ app.run_polling()
54
+
55
+ if __name__ == "__main__":
56
+ main()