| import os |
| import threading |
| import telebot |
| from telebot import apihelper |
| from huggingface_hub import InferenceClient |
| from flask import Flask |
|
|
| |
| apihelper.CONNECT_TIMEOUT = 90 |
| apihelper.READ_TIMEOUT = 90 |
|
|
| |
| app = Flask(__name__) |
|
|
| @app.route('/') |
| def home(): |
| return "Бот активен и работает!" |
|
|
| |
| HF_TOKEN = os.environ.get("HF_TOKEN") |
| client = InferenceClient(token=HF_TOKEN) |
|
|
| TG_TOKEN = "8531345451:AAE8qJlFEwIcuQdUiIFk2viZS9xLLMk2c_o" |
| bot = telebot.TeleBot(TG_TOKEN) |
|
|
| MODEL_NAME = "meta-llama/Meta-Llama-3-8B-Instruct" |
|
|
| @bot.message_handler(content_types=['text']) |
| def handle_message(message): |
| try: |
| response = client.chat_completion( |
| model=MODEL_NAME, |
| messages=[ |
| {"role": "system", "content": "Ты умный AI ассистент. Отвечай четко и только на русском языке."}, |
| {"role": "user", "content": message.text} |
| ], |
| max_tokens=512 |
| ) |
| bot.reply_to(message, response.choices[0].message.content) |
| except Exception as e: |
| bot.reply_to(message, f"Ошибка: {str(e)}") |
|
|
| |
| def run_bot(): |
| while True: |
| try: |
| |
| bot.polling(non_stop=True, timeout=90, long_polling_timeout=90) |
| except Exception: |
| import time |
| time.sleep(5) |
|
|
| if __name__ == "__main__": |
| |
| bot_thread = threading.Thread(target=run_bot) |
| bot_thread.daemon = True |
| bot_thread.start() |
| |
| |
| app.run(host="0.0.0.0", port=7860) |
| |