| import logging |
| import os |
| from fastapi import FastAPI, Request |
| from contextlib import asynccontextmanager |
| from telegram import Update |
| from telegram.ext import Application, CommandHandler, MessageHandler, filters, CallbackContext |
| import asyncio |
| from transformers import pipeline |
| from langdetect import detect |
| from huggingface_hub import login |
| import httpx |
|
|
|
|
| |
| TOKEN = os.getenv("TELEGRAM_BOT_TOKEN") |
| |
| |
| 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 in environment variables.") |
| login(token=HF_HUB_TOKEN) |
|
|
|
|
| |
| hebrew_generator = pipeline("text-generation", model="Norod78/hebrew-gpt_neo-small") |
|
|
|
|
| |
| english_generator = pipeline("text-generation", model="distilgpt2") |
|
|
|
|
| |
| def detect_language(user_input): |
| try: |
| lang = detect(user_input) |
| if lang == "he": |
| return "hebrew" |
| elif lang == "en": |
| return "english" |
| else: |
| return "unsupported" |
| except: |
| return "unsupported" |
|
|
|
|
| |
| def generate_response(text): |
| language = detect_language(text) |
| if language == "hebrew": |
| response = hebrew_generator(text, max_length=100)[0]["generated_text"] |
| elif language == "english": |
| response = english_generator(text, max_length=100)[0]["generated_text"] |
| else: |
| response = "Sorry, I only support Hebrew and English." |
| return response |
|
|
|
|
| |
| app = FastAPI() |
|
|
|
|
| |
| @app.get("/") |
| async def root(): |
| return {"message": "Decision Helper Bot is running!"} |
|
|
|
|
| |
| telegram_app = Application.builder().token(TOKEN).build() |
|
|
| async def init_telegram(): |
| await telegram_app.initialize() |
|
|
|
|
| |
| logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) |
| logger = logging.getLogger(__name__) |
|
|
|
|
| |
| async def start(update: Update, context: CallbackContext): |
| await update.message.reply_text("Hello! Tell me your decision-making issue in HE or EN, and I'll try to help you.") |
|
|
|
|
| |
| async def handle_message(update: Update, context: CallbackContext): |
| user_text = update.message.text |
| response = generate_response(user_text) |
| await update.message.reply_text(response) |
|
|
|
|
| |
| telegram_app.add_handler(CommandHandler("start", start)) |
| telegram_app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) |
|
|
|
|
| @asynccontextmanager |
| async def lifespan(app: FastAPI): |
| print("Starting application...") |
|
|
| |
| try: |
| async with httpx.AsyncClient() as client: |
| response = await client.get("https://api.telegram.org") |
| print(f"Network test successful! Status Code: {response.status_code}") |
| except Exception as e: |
| print(f"Network test failed: {e}") |
|
|
| |
| await init_telegram() |
| await telegram_app.start() |
| print("Telegram bot initialized successfully!") |
|
|
| yield |
| |
| print("Shutting down application...") |
|
|
|
|
| app = FastAPI(lifespan=lifespan) |
|
|
|
|
| @app.get("/") |
| async def root(): |
| return {"message": "Hello, FastAPI with Telegram Bot!"} |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
|
|
|
|
|
|
| |
| @app.post("/") |
| async def receive_update(request: Request): |
| update = Update.de_json(await request.json(), telegram_app.bot) |
| await telegram_app.process_update(update) |
| return {"status": "ok"} |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| |
| if __name__ == "__main__": |
| import uvicorn |
| |
| |
| uvicorn.run(app, host="0.0.0.0", port=7860) |
| |