not-lain commited on
Commit
7163b1f
·
verified ·
1 Parent(s): b2540ee

Synced repo using 'sync_with_huggingface' Github Action

Browse files
Files changed (3) hide show
  1. LICENSE +21 -0
  2. app.py +114 -0
  3. requirements.txt +7 -0
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2023 lain making bread
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import discord
2
+ from gradio_client import Client
3
+ import os
4
+ import threading
5
+ import gradio as gr
6
+ from threading import Event
7
+
8
+ event = Event()
9
+
10
+ DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
11
+
12
+ intents = discord.Intents.default()
13
+ intents.message_content = True
14
+ bot = discord.Bot(intents=intents)
15
+
16
+ client = Client("hysts/mistral-7b")
17
+ def predict(text,history=""):
18
+ return client.predict(text,history, 0.9, 256, 0.95, 1.0)
19
+
20
+ @bot.event
21
+ async def on_ready():
22
+ print(f"logged as {bot.user}")
23
+ event.set()
24
+
25
+
26
+
27
+ # AI prediction command
28
+ @bot.slash_command(name='mistral7b',description='ask mistral-7b-demo AI')
29
+ async def mistral7b(ctx,*,question):
30
+ """
31
+ AI command to create the thread and ask the AI
32
+ """
33
+ # if channel name is mistral7b
34
+ try:
35
+ if ctx.channel.name == "mistral7b":
36
+ await ctx.respond(f"Creating a thread for {ctx.author.mention} ...")
37
+ try :
38
+ # preparing the prediction before creating the thread
39
+ # need to make sure AI sends the first message
40
+ prediction = predict(question)
41
+ thread = await ctx.channel.create_thread(name=question,type=discord.ChannelType.public_thread)
42
+ await thread.send(prediction)
43
+ except Exception as e:
44
+ await thread.send(e)
45
+ else:
46
+ # TODO:
47
+ # tag the channel #mistral7b
48
+ # create the channel if we can't find it, tag it and let the user know that we created it
49
+ await ctx.respond(f"""
50
+ use this command in the channel #mistral7b\nuse `/setup` to create the channel if it doesn't exist""")
51
+ except Exception as e:
52
+ await ctx.respond(e)
53
+
54
+ @bot.event
55
+ async def on_message(message):
56
+ """
57
+ continue the chat in the thread
58
+ """
59
+ # if the message is from the bot ignore it
60
+ if message.author != bot.user:
61
+ # if the message is from the thread
62
+ if message.channel.type in [ discord.ChannelType.public_thread, discord.ChannelType.private_thread ]:
63
+ # if the thread is mistral7b
64
+ if message.channel.parent.name == "mistral7b":
65
+ # preparing the prediction
66
+ # get channel's last 10 messages
67
+ history = await message.channel.history(limit=10).flatten()
68
+ # remove the first message which is the question
69
+ prompt = history.pop(0)
70
+ print("prompt :",prompt.content)
71
+ print("history is ")
72
+ for h in history:
73
+ print(f"{h.author} : {h.content}")
74
+ # TODO: prepare the history for the prediction
75
+ # predict the response
76
+ prediction = predict(message.content,history="")
77
+ await message.channel.send(prediction)
78
+
79
+
80
+
81
+
82
+
83
+ # setup create the mistral7b channel
84
+ @bot.slash_command(name='setup',description='setup the bot')
85
+ async def setup(ctx):
86
+ """
87
+ create the #mistral7b channel
88
+ """
89
+ # if channel mistral7b doesn't exist create it
90
+ if not discord.utils.get(ctx.guild.channels, name="mistral7b"):
91
+ await ctx.guild.create_text_channel("mistral7b",category=ctx.channel.category)
92
+ await ctx.respond("mistral7b-demo channel created")
93
+ else:
94
+ # TODO: tag the channel
95
+ await ctx.respond("#mistral7b channel already exist")
96
+
97
+
98
+ # running in thread
99
+ def run_bot():
100
+ if not DISCORD_TOKEN:
101
+ print("DISCORD_TOKEN NOT SET")
102
+ event.set()
103
+ else:
104
+ bot.run(DISCORD_TOKEN)
105
+
106
+
107
+ threading.Thread(target=run_bot).start()
108
+ event.wait()
109
+
110
+ with gr.Blocks() as demo:
111
+ gr.Markdown("## mistral7b")
112
+
113
+
114
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ py-cord
2
+ python-dotenv
3
+ gradio
4
+ gradio_client
5
+ fastapi
6
+ asyncio
7
+ uvicorn[standard]