Spaces:
Runtime error
Runtime error
File size: 1,252 Bytes
d0d27e5 9798394 d0d27e5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
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() |