Spaces:
Runtime error
Runtime error
| import os | |
| import time | |
| import socket | |
| import requests | |
| import discord | |
| from discord import app_commands | |
| def wait_for_internet(host="discord.com", port=443, timeout=60): | |
| start = time.time() | |
| while True: | |
| try: | |
| socket.getaddrinfo(host, port) | |
| print("🌐 Internet ready") | |
| return | |
| except socket.gaierror: | |
| if time.time() - start > timeout: | |
| raise RuntimeError("❌ Internet not available") | |
| print("⏳ Waiting for internet...") | |
| time.sleep(5) | |
| wait_for_internet() | |
| DISCORD_TOKEN = os.getenv("DISCORD_TOKEN") | |
| HF_TOKEN = os.getenv("HF_TOKEN") | |
| HF_MODEL = "mistralai/Mistral-7B-Instruct-v0.2" | |
| intents = discord.Intents.default() | |
| client = discord.Client(intents=intents) | |
| tree = app_commands.CommandTree(client) | |
| def ask_llm(prompt: str) -> str: | |
| headers = { | |
| "Authorization": f"Bearer {HF_TOKEN}", | |
| "Content-Type": "application/json" | |
| } | |
| payload = { | |
| "inputs": f"Ответь философски:\n{prompt}" | |
| } | |
| r = requests.post( | |
| f"https://api-inference.huggingface.co/models/{HF_MODEL}", | |
| headers=headers, | |
| json=payload, | |
| timeout=60 | |
| ) | |
| if r.status_code != 200: | |
| return "❌ LLM не ответил" | |
| return r.json()[0]["generated_text"] | |
| async def experiment(interaction: discord.Interaction, question: str): | |
| await interaction.response.defer(thinking=True) | |
| answer = ask_llm(question) | |
| await interaction.followup.send(answer[:1900]) | |
| async def on_ready(): | |
| await tree.sync() | |
| print(f"🩸 CultCore online as {client.user}") | |
| client.run(DISCORD_TOKEN) |