lunarflu HF staff commited on
Commit
b510f8d
1 Parent(s): 947f09f

Delete falcon180b.py

Browse files
Files changed (1) hide show
  1. falcon180b.py +0 -160
falcon180b.py DELETED
@@ -1,160 +0,0 @@
1
- import asyncio
2
- import os
3
- import threading
4
- from threading import Event
5
- from typing import Optional
6
-
7
- import discord
8
- import gradio as gr
9
- from discord.ext import commands
10
- import gradio_client as grc
11
- from gradio_client.utils import QueueError
12
-
13
- event = Event()
14
-
15
- DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
16
-
17
-
18
- async def wait(job):
19
- while not job.done():
20
- await asyncio.sleep(0.2)
21
-
22
-
23
- def get_client(session: Optional[str] = None) -> grc.Client:
24
- client = grc.Client("https://tiiuae-falcon-180b-demo.hf.space", hf_token=os.getenv("HF_TOKEN"))
25
- if session:
26
- client.session_hash = session
27
- return client
28
-
29
-
30
- def truncate_response(response: str) -> str:
31
- ending = "...\nTruncating response to 2000 characters due to discord api limits."
32
- if len(response) > 2000:
33
- return response[: 2000 - len(ending)] + ending
34
- else:
35
- return response
36
-
37
-
38
- intents = discord.Intents.default()
39
- intents.message_content = True
40
- bot = commands.Bot(command_prefix="/", intents=intents)
41
-
42
-
43
- @bot.event
44
- async def on_ready():
45
- print(f"Logged in as {bot.user} (ID: {bot.user.id})")
46
- synced = await bot.tree.sync()
47
- print(f"Synced commands: {', '.join([s.name for s in synced])}.")
48
- event.set()
49
- print("------")
50
-
51
-
52
- thread_to_client = {}
53
- thread_to_user = {}
54
-
55
-
56
- @bot.hybrid_command(
57
- name="falcon180",
58
- description="Enter some text to chat with the bot! Like this: /falcon180 Hello, how are you?",
59
- )
60
- async def chat(ctx, prompt: str):
61
- if ctx.author.id == bot.user.id:
62
- return
63
- try:
64
- if ctx.guild.id == 879548962464493619:
65
- if ctx.channel.id != 1119313248056004729:
66
- return
67
- message = await ctx.send("Creating thread...")
68
-
69
- thread = await message.create_thread(name=prompt[:100])
70
- loop = asyncio.get_running_loop()
71
- client = await loop.run_in_executor(None, get_client, None)
72
- job = client.submit(prompt, "", 0.9, 256, 0.95, 1.0, api_name="/chat")
73
- await wait(job)
74
-
75
- try:
76
- job.result()
77
- response = job.outputs()[-1]
78
- await thread.send(truncate_response(response))
79
- thread_to_client[thread.id] = client
80
- thread_to_user[thread.id] = ctx.author.id
81
- except QueueError:
82
- await thread.send("The gradio space powering this bot is really busy! Please try again later!")
83
-
84
- except Exception as e:
85
- print(f"{e}")
86
-
87
-
88
- async def continue_chat(message):
89
- """Continues a given conversation based on chathistory"""
90
- try:
91
- client = thread_to_client[message.channel.id]
92
- prompt = message.content
93
- job = client.submit(prompt, "", 0.9, 256, 0.95, 1.0, api_name="/chat")
94
- await wait(job)
95
- try:
96
- job.result()
97
- response = job.outputs()[-1]
98
- await message.reply(truncate_response(response))
99
- except QueueError:
100
- await message.reply("The gradio space powering this bot is really busy! Please try again later!")
101
-
102
- except Exception as e:
103
- print(f"Error: {e}")
104
-
105
-
106
- @bot.event
107
- async def on_message(message):
108
- """Continue the chat"""
109
- try:
110
- if not message.author.bot:
111
- if message.channel.id in thread_to_user:
112
- if thread_to_user[message.channel.id] == message.author.id:
113
- await continue_chat(message)
114
- else:
115
- await bot.process_commands(message)
116
-
117
- except Exception as e:
118
- print(f"Error: {e}")
119
-
120
-
121
- # running in thread
122
- def run_bot():
123
- if not DISCORD_TOKEN:
124
- print("DISCORD_TOKEN NOT SET")
125
- event.set()
126
- else:
127
- bot.run(DISCORD_TOKEN)
128
-
129
-
130
- threading.Thread(target=run_bot).start()
131
-
132
- event.wait()
133
-
134
-
135
- welcome_message = """
136
- ## Add this bot to your server by clicking this link:
137
-
138
- https://discord.com/api/oauth2/authorize?client_id=1155169841276260546&permissions=326417516544&scope=bot
139
-
140
- ## How to use it?
141
-
142
- The bot can be triggered via `/falcon180` followed by your text prompt.
143
-
144
- This will create a thread with the bot's response to your text prompt.
145
- You can reply in the thread (without `/falcon180`) to continue the conversation.
146
- In the thread, the bot will only reply to the original author of the command.
147
-
148
- ⚠️ Note ⚠️: Please make sure this bot's command does have the same name as another command in your server.
149
-
150
- ⚠️ Note ⚠️: Bot commands do not work in DMs with the bot as of now.
151
- """
152
-
153
-
154
- with gr.Blocks() as demo:
155
- gr.Markdown(f"""
156
- # Discord bot of https://tiiuae-falcon-180b-demo.hf.space
157
- {welcome_message}
158
- """)
159
-
160
- demo.launch()