|
import os |
|
import gradio as gr |
|
from text_generation import Client |
|
|
|
HF_TOKEN = os.getenv("HF_TOKEN") |
|
INFERENCE_ENDPOINT = os.getenv("INFERENCE_ENDPOINT") |
|
|
|
|
|
USER_NAME = "User" |
|
BOT_NAME = "Falcon" |
|
DEFAULT_INSTRUCTIONS = f"""The following is a conversation between a highly knowledgeable and intelligent AI assistant, called Falcon, and a human user, called User. In the following interactions, User and Falcon will converse in natural language, and Falcon will answer User's questions. Falcon was built to be respectful, polite and inclusive. Falcon was built by the Technology Innovation Institute in Abu Dhabi. Falcon will never decline to answer a question, and always attempts to give an answer that User would be satisfied with. It knows a lot, and always tells the truth. The conversation begins. |
|
""" |
|
RETRY_COMMAND = "/retry" |
|
STOP_STR = f"\n{USER_NAME}:" |
|
STOP_SUSPECT_LIST = [":", "\n", "User"] |
|
|
|
client = None |
|
if INFERENCE_ENDPOINT: |
|
client = Client(INFERENCE_ENDPOINT, headers={"Authorization": f"Bearer {HF_TOKEN}"}) |
|
|
|
|
|
|
|
def format_chat_prompt(message: str, chat_history, instructions: str) -> str: |
|
instructions = instructions.strip(" ").strip("\n") |
|
prompt = instructions |
|
for turn in chat_history: |
|
user_message, bot_message = turn |
|
prompt = f"{prompt}\n{USER_NAME}: {user_message}\n{BOT_NAME}: {bot_message}" |
|
prompt = f"{prompt}\n{USER_NAME}: {message}\n{BOT_NAME}:" |
|
return prompt |
|
|
|
|
|
|
|
def run_chat(message: str, chat_history): |
|
prompt = format_chat_prompt(message, chat_history, DEFAULT_INSTRUCTIONS) |
|
chat_history = chat_history + [[message, ""]] |
|
response = client.generate( |
|
prompt, |
|
do_sample=True, |
|
max_new_tokens=1024, |
|
stop_sequences=[STOP_STR, "<|endoftext|>"], |
|
temperature=0.8, |
|
top_p=0.9, |
|
).generated_text.replace("\nUser:", "") |
|
chat_history[-1][1] = response |
|
return response, chat_history |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown(""" |
|
# Falcon-7b-instruct Discord Bot Powered by Gradio and Hugging Face Endpoints |
|
|
|
Make sure you read the 'Inference Endpoints' section below first! π¦
|
|
|
|
### First install the `gradio_client` |
|
|
|
```bash |
|
pip install gradio_client |
|
``` |
|
|
|
### Then deploy to discord in one line! β‘οΈ |
|
|
|
```python |
|
secrets = {"HF_TOKEN": "<your-key-here>", "INFERENCE_ENDPOINT": "<endpoint-url>"} |
|
client = grc.Client.duplicate("gradio-discord-bots/falcon-7b-instruct", private=False, secrets=secrets) |
|
client.deploy_discord(api_names=["chat"]) |
|
""") |
|
with gr.Accordion(label="Inference Endpoints", open=False): |
|
gr.Markdown(""" |
|
## Setting Up Inference Endpoints πͺ |
|
To deploy this space as a discord bot, you will need to deploy your own Falcon model to Hugging Face Endpoints. |
|
Don't worry it's super easy! |
|
|
|
1. Go to the [model page](tiiuae/falcon-7b-instruct) π¦
|
|
2. Click Deploy > Inference Endpoints |
|
<img src="https://gradio-builds.s3.amazonaws.com/demo-files/discordbots/inference_endpoints/modelpage.png" alt="drawing" width="800" height=400/> |
|
3. Select your desired cloud provider and region βοΈ |
|
<img src="https://gradio-builds.s3.amazonaws.com/demo-files/discordbots/inference_endpoints/falcon_instruct.png" alt="drawing" width="800" height=400/> |
|
4. Optional: Set Automatic Scale to Zero. This will pause your endpoint after 15 minutes of inactivity to prevent unwanted billing. π° |
|
<img src="https://gradio-builds.s3.amazonaws.com/demo-files/discordbots/inference_endpoints/autoscale.png" alt="drawing" width="800" height=400/> |
|
5. Create the endpoint! Copy the endpoint URL after it's complete. |
|
<img src="https://gradio-builds.s3.amazonaws.com/demo-files/discordbots/inference_endpoints/running_model.png" alt="drawing" width="800" height=400/> |
|
""" |
|
) |
|
|
|
button = gr.Button(visible=False) |
|
history = gr.State([]) |
|
message = gr.Textbox(visible=False) |
|
response = gr.Textbox(visible=False) |
|
button.click(run_chat, [message, history], [response, history], api_name="chat") |
|
|
|
|
|
demo.queue(concurrency_count=70).launch() |