TransnumeriX / app.py
PierreH's picture
Create app.py
d0d27e5 verified
import os
import requests
from telegram import Update
from telegram.ext import Application, MessageHandler, filters, CallbackContext
# 🔹 Configuration avec des variables d'environnement
TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN")
HUGGINGFACE_MODEL = "mistralai/Mistral-7B-Instruct"
# 🔹 Fonction pour appeler le modèle Hugging Face
def get_ai_response(user_input):
url = f"https://api-inference.huggingface.co/models/{HUGGINGFACE_MODEL}"
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"}
data = {"inputs": user_input}
response = requests.post(url, json=data, headers=headers)
return response.json()[0]["generated_text"] if "generated_text" in response.json()[0] else "Je ne comprends pas."
# 🔹 Fonction pour gérer les messages Telegram
async def handle_message(update: Update, context: CallbackContext):
user_message = update.message.text
bot_response = get_ai_response(user_message)
await update.message.reply_text(bot_response)
# 🔹 Configuration du bot Telegram
app = Application.builder().token(TELEGRAM_TOKEN).build()
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
# 🔹 Lancer le bot
if __name__ == "__main__":
app.run_polling()