Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
import os
|
3 |
+
from fastapi import FastAPI, Request
|
4 |
+
from telegram import Update
|
5 |
+
from telegram.ext import Application, CommandHandler, MessageHandler, filters, CallbackContext
|
6 |
+
import asyncio
|
7 |
+
from transformers import pipeline
|
8 |
+
from langdetect import detect
|
9 |
+
from huggingface_hub import login
|
10 |
+
|
11 |
+
|
12 |
+
# Global variables
|
13 |
+
TOKEN = os.getenv("TELEGRAM_BOT_TOKEN") # Telegram Token
|
14 |
+
WEBHOOK_URL = "https://demaking-decision-helper-bot.hf.space/"
|
15 |
+
PORT = int(os.getenv("PORT", 7860)) # HF Spaces listens on port 7860 by default
|
16 |
+
|
17 |
+
"""HF_HUB_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
18 |
+
|
19 |
+
|
20 |
+
# Verify Hugging Face token
|
21 |
+
if not HF_HUB_TOKEN:
|
22 |
+
raise ValueError("Missing Hugging Face API token. Please set HUGGINGFACEHUB_API_TOKEN in environment variables.")
|
23 |
+
login(token=HF_HUB_TOKEN)
|
24 |
+
"""
|
25 |
+
|
26 |
+
|
27 |
+
# Load Hebrew text generation model
|
28 |
+
hebrew_generator = pipeline("text-generation", model="Norod78/hebrew-gpt_neo-small")
|
29 |
+
|
30 |
+
|
31 |
+
# Load English text generation model
|
32 |
+
english_generator = pipeline("text-generation", model="distilgpt2")
|
33 |
+
|
34 |
+
|
35 |
+
# Function to detect language
|
36 |
+
def detect_language(user_input):
|
37 |
+
try:
|
38 |
+
lang = detect(user_input)
|
39 |
+
if lang == "he":
|
40 |
+
return "hebrew"
|
41 |
+
elif lang == "en":
|
42 |
+
return "english"
|
43 |
+
else:
|
44 |
+
return "unsupported"
|
45 |
+
except:
|
46 |
+
return "unsupported"
|
47 |
+
|
48 |
+
|
49 |
+
# Function to generate a response based on detected language
|
50 |
+
def generate_response(text):
|
51 |
+
language = detect_language(text)
|
52 |
+
if language == "hebrew":
|
53 |
+
response = hebrew_generator(text, max_length=100)[0]["generated_text"]
|
54 |
+
elif language == "english":
|
55 |
+
response = english_generator(text, max_length=100)[0]["generated_text"]
|
56 |
+
else:
|
57 |
+
response = "Sorry, I only support Hebrew and English."
|
58 |
+
return response
|
59 |
+
|
60 |
+
|
61 |
+
# Create FastAPI app
|
62 |
+
app = FastAPI()
|
63 |
+
|
64 |
+
|
65 |
+
# Initialize Telegram Bot
|
66 |
+
telegram_app = Application.builder().token(TOKEN).build()
|
67 |
+
|
68 |
+
|
69 |
+
# Configure logging
|
70 |
+
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
|
71 |
+
logger = logging.getLogger(__name__)
|
72 |
+
|
73 |
+
|
74 |
+
# Start command handler
|
75 |
+
async def start(update: Update, context: CallbackContext):
|
76 |
+
await update.message.reply_text("Hello! Tell me your decision-making issue in HE or EN, and I'll try to help you.")
|
77 |
+
|
78 |
+
|
79 |
+
# Message handler
|
80 |
+
async def handle_message(update: Update, context: CallbackContext):
|
81 |
+
user_text = update.message.text
|
82 |
+
response = generate_response(user_text) # Generate AI-based response
|
83 |
+
await update.message.reply_text(response)
|
84 |
+
|
85 |
+
|
86 |
+
# Add handlers to the bot
|
87 |
+
telegram_app.add_handler(CommandHandler("start", start))
|
88 |
+
telegram_app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
89 |
+
|
90 |
+
|
91 |
+
# API Route to receive updates from Telegram
|
92 |
+
@app.post("/")
|
93 |
+
async def receive_update(request: Request):
|
94 |
+
update = Update.de_json(await request.json(), telegram_app.bot)
|
95 |
+
await telegram_app.process_update(update)
|
96 |
+
return {"status": "ok"}
|
97 |
+
|
98 |
+
|
99 |
+
# Function to set up webhook
|
100 |
+
async def set_webhook():
|
101 |
+
webhook_url = WEBHOOK_URL
|
102 |
+
await telegram_app.bot.set_webhook(webhook_url)
|
103 |
+
logger.info(f"Webhook set to {webhook_url}")
|
104 |
+
|
105 |
+
|
106 |
+
# Run the server
|
107 |
+
if __name__ == "__main__":
|
108 |
+
import uvicorn
|
109 |
+
loop = asyncio.get_event_loop()
|
110 |
+
loop.run_until_complete(set_webhook())
|
111 |
+
uvicorn.run(app, host="0.0.0.0", port=PORT)
|
112 |
+
|