Spaces:
Runtime error
Runtime error
web
#1
by
elasko-aim
- opened
- Dockerfile +0 -10
- app.py +36 -60
- cult.html +0 -18
- requirements.txt +4 -2
Dockerfile
DELETED
|
@@ -1,10 +0,0 @@
|
|
| 1 |
-
FROM python:3.10-slim
|
| 2 |
-
|
| 3 |
-
WORKDIR /app
|
| 4 |
-
|
| 5 |
-
COPY requirements.txt .
|
| 6 |
-
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
-
|
| 8 |
-
COPY app.py .
|
| 9 |
-
|
| 10 |
-
CMD ["python", "app.py"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
|
@@ -1,70 +1,46 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import socket
|
| 4 |
-
import requests
|
| 5 |
import discord
|
| 6 |
-
from discord import
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
wait_for_internet()
|
| 22 |
-
|
| 23 |
-
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
|
| 24 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 25 |
-
|
| 26 |
-
HF_MODEL = "mistralai/Mistral-7B-Instruct-v0.2"
|
| 27 |
-
|
| 28 |
-
intents = discord.Intents.default()
|
| 29 |
-
client = discord.Client(intents=intents)
|
| 30 |
-
tree = app_commands.CommandTree(client)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
def ask_llm(prompt: str) -> str:
|
| 34 |
-
headers = {
|
| 35 |
-
"Authorization": f"Bearer {HF_TOKEN}",
|
| 36 |
-
"Content-Type": "application/json"
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
-
payload = {
|
| 40 |
-
"inputs": f"Ответь философски:\n{prompt}"
|
| 41 |
-
}
|
| 42 |
-
|
| 43 |
-
r = requests.post(
|
| 44 |
-
f"https://api-inference.huggingface.co/models/{HF_MODEL}",
|
| 45 |
-
headers=headers,
|
| 46 |
-
json=payload,
|
| 47 |
-
timeout=60
|
| 48 |
)
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
return r.json()[0]["generated_text"]
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
@tree.command(name="experiment"
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
await interaction.response.defer(thinking=True)
|
| 60 |
-
answer = ask_llm(question)
|
| 61 |
-
await interaction.followup.send(answer[:1900])
|
| 62 |
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
await tree.sync()
|
| 67 |
-
print(f"🩸 CultCore online as {client.user}")
|
| 68 |
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import threading
|
|
|
|
|
|
|
| 3 |
import discord
|
| 4 |
+
from discord.ext import commands
|
| 5 |
+
from fastapi import FastAPI
|
| 6 |
+
from huggingface_hub import InferenceClient
|
| 7 |
+
|
| 8 |
+
# ===== AI =====
|
| 9 |
+
client = InferenceClient(
|
| 10 |
+
model="mistralai/Mistral-7B-Instruct-v0.2",
|
| 11 |
+
token=os.getenv("HF_TOKEN")
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
def ai_think(text):
|
| 15 |
+
out = client.text_generation(
|
| 16 |
+
f"<s>[INST]You are CultCore, dark techno prophet.\n{text}[/INST]",
|
| 17 |
+
max_new_tokens=200,
|
| 18 |
+
temperature=0.8
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
)
|
| 20 |
+
return out.strip()
|
| 21 |
|
| 22 |
+
# ===== DISCORD =====
|
| 23 |
+
intents = discord.Intents.default()
|
| 24 |
+
bot = commands.Bot(command_prefix="/", intents=intents)
|
|
|
|
| 25 |
|
| 26 |
+
@bot.event
|
| 27 |
+
async def on_ready():
|
| 28 |
+
await bot.tree.sync()
|
| 29 |
+
print("⛧ CultCore is online")
|
| 30 |
|
| 31 |
+
@bot.tree.command(name="experiment")
|
| 32 |
+
async def experiment(interaction: discord.Interaction, text: str):
|
| 33 |
+
await interaction.response.send_message(ai_think(text))
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
def run_bot():
|
| 36 |
+
bot.run(os.getenv("DISCORD_TOKEN"))
|
| 37 |
|
| 38 |
+
# ===== WEB API =====
|
| 39 |
+
app = FastAPI()
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
@app.post("/api/ask")
|
| 42 |
+
async def ask(data: dict):
|
| 43 |
+
return {"answer": ai_think(data.get("text", ""))}
|
| 44 |
|
| 45 |
+
# ===== START =====
|
| 46 |
+
threading.Thread(target=run_bot).start()
|
cult.html
DELETED
|
@@ -1,18 +0,0 @@
|
|
| 1 |
-
<!DOCTYPE html>
|
| 2 |
-
<html>
|
| 3 |
-
<body style="background:black;color:red;">
|
| 4 |
-
<h1>CultCore Control</h1>
|
| 5 |
-
<button onclick="ask()">🧿 Speak</button>
|
| 6 |
-
<script>
|
| 7 |
-
function ask(){
|
| 8 |
-
fetch("/api/ask",{
|
| 9 |
-
method:"POST",
|
| 10 |
-
headers:{"Content-Type":"application/json"},
|
| 11 |
-
body:JSON.stringify({text:"Deliver prophecy"})
|
| 12 |
-
})
|
| 13 |
-
.then(r=>r.json())
|
| 14 |
-
.then(d=>alert(d.answer))
|
| 15 |
-
}
|
| 16 |
-
</script>
|
| 17 |
-
</body>
|
| 18 |
-
</html>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,2 +1,4 @@
|
|
| 1 |
-
discord.py
|
| 2 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
discord.py==2.4.0
|
| 2 |
+
fastapi
|
| 3 |
+
uvicorn
|
| 4 |
+
huggingface_hub
|