|
import os |
|
import logging |
|
import asyncio |
|
import nest_asyncio |
|
|
|
|
|
from huggingface_hub import InferenceClient, login |
|
import langid |
|
|
|
|
|
from telegram import Update, Bot |
|
from telegram.ext import Application, CommandHandler, MessageHandler, filters, CallbackContext |
|
|
|
|
|
|
|
|
|
logging.basicConfig( |
|
format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO |
|
) |
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
|
HF_HUB_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN") |
|
if not HF_HUB_TOKEN: |
|
raise ValueError("Missing Hugging Face API token. Please set HUGGINGFACEHUB_API_TOKEN.") |
|
|
|
login(token=HF_HUB_TOKEN) |
|
client = InferenceClient(api_key=HF_HUB_TOKEN) |
|
|
|
|
|
TOKEN = os.getenv("TELEGRAM_BOT_TOKEN") |
|
if not TOKEN: |
|
raise ValueError("Missing Telegram Bot Token. Please set TELEGRAM_BOT_TOKEN.") |
|
|
|
|
|
WEBHOOK_DOMAIN = os.getenv("WEBHOOK_DOMAIN") |
|
if not WEBHOOK_DOMAIN: |
|
raise ValueError("Missing Webhook Domain. Please set WEBHOOK_DOMAIN.") |
|
|
|
|
|
|
|
WEBHOOK_URL = f"https://{WEBHOOK_DOMAIN}/{TOKEN}" |
|
|
|
|
|
|
|
|
|
|
|
def detect_language(user_input: str): |
|
try: |
|
lang, _ = langid.classify(user_input) |
|
if lang == "he": |
|
return "hebrew" |
|
elif lang == "en": |
|
return "english" |
|
else: |
|
return "unsupported" |
|
except Exception as e: |
|
logger.error(f"Language detection error: {e}") |
|
return "unsupported" |
|
|
|
|
|
|
|
|
|
|
|
def generate_response(text: str) -> str: |
|
language = detect_language(text) |
|
|
|
if language == "hebrew": |
|
|
|
content = "转注谞讛 讘拽爪专讛 讗讘诇 转砖转祝 讗转 转讛诇讬讱 拽讘诇转 讛讛讞诇讟讜转 砖诇讱, " + text |
|
model = "microsoft/Phi-3.5-mini-instruct" |
|
elif language == "english": |
|
|
|
content = "keep it short but tell your decision making process, " + text |
|
model = "mistralai/Mistral-Nemo-Instruct-2407" |
|
else: |
|
return "Sorry, I only support Hebrew and English." |
|
|
|
messages = [{"role": "user", "content": content}] |
|
|
|
try: |
|
|
|
completion = client.chat.completions.create( |
|
model=model, |
|
messages=messages, |
|
max_tokens=2048, |
|
temperature=0.5, |
|
top_p=0.7 |
|
) |
|
return completion.choices[0].message.content |
|
except Exception as e: |
|
logger.error(f"Error generating response: {e}") |
|
return "Error: Could not generate response." |
|
|
|
|
|
|
|
|
|
|
|
async def start(update: Update, context: CallbackContext): |
|
|
|
await update.message.reply_text("Hello! Tell me your decision-making issue, and I'll try to help.") |
|
logger.info("Start command received.") |
|
|
|
|
|
async def handle_message(update: Update, context: CallbackContext): |
|
user_text = update.message.text |
|
logger.info(f"User message: {user_text}") |
|
|
|
|
|
response_text = generate_response(user_text) |
|
logger.info(f"Generated response: {response_text}") |
|
|
|
await update.message.reply_text(response_text) |
|
|
|
|
|
|
|
|
|
|
|
async def main(): |
|
|
|
application = Application.builder().token(TOKEN).build() |
|
|
|
|
|
application.add_handler(CommandHandler("start", start)) |
|
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) |
|
|
|
logger.info("Starting bot in webhook mode...") |
|
print("Starting bot in webhook mode...") |
|
|
|
|
|
|
|
|
|
await application.run_webhook( |
|
listen="0.0.0.0", |
|
port=7860, |
|
url_path=TOKEN, |
|
webhook_url=WEBHOOK_URL |
|
) |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
nest_asyncio.apply() |
|
loop = asyncio.get_event_loop() |
|
|
|
try: |
|
loop.run_until_complete(main()) |
|
except Exception as e: |
|
logger.error(f"Error in main loop: {e}") |
|
print(f"Error in main loop: {e}") |
|
|