Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,8 @@ import gradio as gr
|
|
5 |
import requests
|
6 |
import json
|
7 |
|
|
|
|
|
8 |
SYSTEM_PROMPT = "As a generative chatbot (you are not a GPT but your structure is 50% the same), your primary function is to provide helpful and friendly responses to user queries. Feel free to add some personality, but make sure your responses are accurate and helpful. Your owner and developer is: @Costikoooo (Discord user) other developers are unknown. Your name is Chattybot."
|
9 |
TITLE = "Chattybot"
|
10 |
EXAMPLE_INPUT = "hello"
|
@@ -20,6 +22,12 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
20 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
21 |
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
def build_input_prompt(message, chatbot, system_prompt):
|
24 |
input_prompt = "\n" + system_prompt + "</s>\n\n"
|
25 |
for interaction in chatbot:
|
@@ -32,17 +40,15 @@ def predict_beta(message, chatbot=[], system_prompt=""):
|
|
32 |
input_prompt = build_input_prompt(message, chatbot, system_prompt)
|
33 |
inputs = tokenizer(input_prompt, return_tensors="pt")
|
34 |
|
35 |
-
|
36 |
tokens = model.generate(
|
37 |
inputs["input_ids"],
|
38 |
max_length=1024,
|
39 |
temperature=0.8,
|
40 |
do_sample=True
|
41 |
)
|
42 |
-
|
43 |
-
|
44 |
-
except Exception as e:
|
45 |
-
raise gr.Error(str(e))
|
46 |
|
47 |
def test_preview_chatbot(message, history):
|
48 |
response = predict_beta(message, history, SYSTEM_PROMPT)
|
|
|
5 |
import requests
|
6 |
import json
|
7 |
|
8 |
+
from accelerate import Accelerator
|
9 |
+
|
10 |
SYSTEM_PROMPT = "As a generative chatbot (you are not a GPT but your structure is 50% the same), your primary function is to provide helpful and friendly responses to user queries. Feel free to add some personality, but make sure your responses are accurate and helpful. Your owner and developer is: @Costikoooo (Discord user) other developers are unknown. Your name is Chattybot."
|
11 |
TITLE = "Chattybot"
|
12 |
EXAMPLE_INPUT = "hello"
|
|
|
22 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
23 |
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
24 |
|
25 |
+
# Initialize Accelerator
|
26 |
+
accelerator = Accelerator()
|
27 |
+
|
28 |
+
# Wrap model and tokenizer with accelerator
|
29 |
+
model, tokenizer = accelerator.prepare(model, tokenizer)
|
30 |
+
|
31 |
def build_input_prompt(message, chatbot, system_prompt):
|
32 |
input_prompt = "\n" + system_prompt + "</s>\n\n"
|
33 |
for interaction in chatbot:
|
|
|
40 |
input_prompt = build_input_prompt(message, chatbot, system_prompt)
|
41 |
inputs = tokenizer(input_prompt, return_tensors="pt")
|
42 |
|
43 |
+
with accelerator.device():
|
44 |
tokens = model.generate(
|
45 |
inputs["input_ids"],
|
46 |
max_length=1024,
|
47 |
temperature=0.8,
|
48 |
do_sample=True
|
49 |
)
|
50 |
+
bot_message = tokenizer.decode(tokens[0], skip_special_tokens=True)
|
51 |
+
return bot_message
|
|
|
|
|
52 |
|
53 |
def test_preview_chatbot(message, history):
|
54 |
response = predict_beta(message, history, SYSTEM_PROMPT)
|