Spaces:
Sleeping
Sleeping
Create bot.py
Browse files
bot.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import discord
|
2 |
+
import os
|
3 |
+
from discord.ext import commands
|
4 |
+
from huggingface_hub import InferenceClient
|
5 |
+
|
6 |
+
client = commands.Bot(command_prefix='!')
|
7 |
+
inference_client = InferenceClient("HuggingFaceH4/zephyr-7b-alpha")
|
8 |
+
|
9 |
+
@client.event
|
10 |
+
async def on_ready():
|
11 |
+
print(f'Logged in as {client.user.name}')
|
12 |
+
|
13 |
+
@client.command(name="ask")
|
14 |
+
async def ask(ctx, *, question):
|
15 |
+
history = [] # Add the conversation history if needed
|
16 |
+
response = next(generate(question, history))
|
17 |
+
await ctx.send(response)
|
18 |
+
|
19 |
+
def generate(prompt, history, temperature=0.9, max_new_tokens=500, top_p=0.95, repetition_penalty=1.0):
|
20 |
+
temperature = float(temperature)
|
21 |
+
if temperature < 1e-2:
|
22 |
+
temperature = 1e-2
|
23 |
+
top_p = float(top_p)
|
24 |
+
|
25 |
+
generate_kwargs = dict(
|
26 |
+
temperature=temperature,
|
27 |
+
max_new_tokens=max_new_tokens,
|
28 |
+
top_p=top_p,
|
29 |
+
repetition_penalty=repetition_penalty,
|
30 |
+
do_sample=True,
|
31 |
+
seed=42,
|
32 |
+
)
|
33 |
+
|
34 |
+
formatted_prompt = format_prompt(prompt, history)
|
35 |
+
|
36 |
+
# Use the InferenceClient for text generation
|
37 |
+
stream = inference_client.text_generation(
|
38 |
+
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False
|
39 |
+
)
|
40 |
+
|
41 |
+
output = ""
|
42 |
+
for response in stream:
|
43 |
+
output += response.token.text
|
44 |
+
return output
|
45 |
+
|
46 |
+
client.run(os.getenv("token"))
|