Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,18 +4,20 @@ import torch
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
# checkpoint = "tiiuae/falcon-40b-instruct"
|
| 7 |
-
|
| 8 |
# checkpoint = "tiiuae/falcon-7b"
|
| 9 |
-
checkpoint = "gpt2"
|
| 10 |
|
| 11 |
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
checkpoint, device_map="auto", offload_folder="offload",
|
| 14 |
trust_remote_code=True,
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
tokenizer = AutoTokenizer.from_pretrained(checkpoint,
|
| 18 |
trust_remote_code=True,
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
pipeline = transformers.pipeline(
|
|
@@ -28,41 +30,103 @@ pipeline = transformers.pipeline(
|
|
| 28 |
device_map="auto",
|
| 29 |
)
|
| 30 |
|
| 31 |
-
def format_chat_prompt(message, chat_history):
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
for turn in chat_history:
|
| 34 |
user_message, bot_message = turn
|
| 35 |
prompt = f"{prompt}\nUser: {user_message}\nAssistant: {bot_message}"
|
| 36 |
prompt = f"{prompt}\nUser: {message}\nAssistant:"
|
| 37 |
return prompt
|
| 38 |
|
| 39 |
-
def
|
| 40 |
-
formatted_prompt = format_chat_prompt(message, chat_history)
|
| 41 |
-
# print(formatted_prompt)
|
| 42 |
-
bot_message = generate_seqs(prompt = formatted_prompt,
|
| 43 |
-
max_new_tokens=1024,
|
| 44 |
-
stop_sequence=["\nUser:", "<|endoftext|>"]).split('Assistant: ')[-1]
|
| 45 |
-
|
| 46 |
-
chat_history.append((message, bot_message))
|
| 47 |
-
return "", chat_history
|
| 48 |
-
|
| 49 |
-
def generate_seqs(prompt, max_new_tokens=None, stop_sequence=None):
|
| 50 |
output = pipeline(prompt,
|
| 51 |
max_length=200,
|
| 52 |
max_new_tokens = max_new_tokens,
|
| 53 |
stop_sequence = stop_sequence,
|
|
|
|
| 54 |
do_sample=True,
|
| 55 |
top_k=10,
|
| 56 |
num_return_sequences=1,
|
| 57 |
eos_token_id=tokenizer.eos_token_id)
|
| 58 |
return output[0]['generated_text']
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
with gr.Blocks() as demo:
|
| 61 |
chatbot = gr.Chatbot(height=240) #just to fit the notebook
|
| 62 |
msg = gr.Textbox(label="Prompt")
|
|
|
|
|
|
|
|
|
|
| 63 |
btn = gr.Button("Submit")
|
| 64 |
clear = gr.ClearButton(components=[msg, chatbot], value="Clear console")
|
| 65 |
|
| 66 |
-
btn.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 67 |
-
msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot]) #Press enter to submit
|
| 68 |
-
|
|
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
# checkpoint = "tiiuae/falcon-40b-instruct"
|
| 7 |
+
checkpoint = "tiiuae/falcon-7b-instruct"
|
| 8 |
# checkpoint = "tiiuae/falcon-7b"
|
| 9 |
+
# checkpoint = "gpt2"
|
| 10 |
|
| 11 |
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
checkpoint, device_map="auto", offload_folder="offload",
|
| 14 |
trust_remote_code=True,
|
| 15 |
+
# torch_dtype="auto",
|
| 16 |
)
|
| 17 |
|
| 18 |
tokenizer = AutoTokenizer.from_pretrained(checkpoint,
|
| 19 |
trust_remote_code=True,
|
| 20 |
+
torch_dtype="auto"
|
| 21 |
)
|
| 22 |
|
| 23 |
pipeline = transformers.pipeline(
|
|
|
|
| 30 |
device_map="auto",
|
| 31 |
)
|
| 32 |
|
| 33 |
+
# def format_chat_prompt(message, chat_history):
|
| 34 |
+
# prompt = ""
|
| 35 |
+
# for turn in chat_history:
|
| 36 |
+
# user_message, bot_message = turn
|
| 37 |
+
# prompt = f"{prompt}\nUser: {user_message}\nAssistant: {bot_message}"
|
| 38 |
+
# prompt = f"{prompt}\nUser: {message}\nAssistant:"
|
| 39 |
+
# return prompt
|
| 40 |
+
|
| 41 |
+
# def respond(message, chat_history):
|
| 42 |
+
# formatted_prompt = format_chat_prompt(message, chat_history)
|
| 43 |
+
# # print(formatted_prompt)
|
| 44 |
+
# bot_message = generate_seqs(prompt = formatted_prompt,
|
| 45 |
+
# max_new_tokens=1024,
|
| 46 |
+
# stop_sequence=["\nUser:", "<|endoftext|>"]).split('Assistant: ')[-1]
|
| 47 |
+
|
| 48 |
+
# chat_history.append((message, bot_message))
|
| 49 |
+
# return "", chat_history
|
| 50 |
+
|
| 51 |
+
# def generate_seqs(prompt, max_new_tokens=None, stop_sequence=None):
|
| 52 |
+
# output = pipeline(prompt,
|
| 53 |
+
# max_length=200,
|
| 54 |
+
# max_new_tokens = max_new_tokens,
|
| 55 |
+
# stop_sequence = stop_sequence,
|
| 56 |
+
# do_sample=True,
|
| 57 |
+
# top_k=10,
|
| 58 |
+
# num_return_sequences=1,
|
| 59 |
+
# eos_token_id=tokenizer.eos_token_id)
|
| 60 |
+
# return output[0]['generated_text']
|
| 61 |
+
|
| 62 |
+
# with gr.Blocks() as demo:
|
| 63 |
+
# chatbot = gr.Chatbot(height=240) #just to fit the notebook
|
| 64 |
+
# msg = gr.Textbox(label="Prompt")
|
| 65 |
+
# btn = gr.Button("Submit")
|
| 66 |
+
# clear = gr.ClearButton(components=[msg, chatbot], value="Clear console")
|
| 67 |
+
|
| 68 |
+
# btn.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 69 |
+
# msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot]) #Press enter to submit
|
| 70 |
+
# demo.launch()
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def format_chat_prompt(message, chat_history, instruction):
|
| 74 |
+
prompt = f"System:{instruction}"
|
| 75 |
for turn in chat_history:
|
| 76 |
user_message, bot_message = turn
|
| 77 |
prompt = f"{prompt}\nUser: {user_message}\nAssistant: {bot_message}"
|
| 78 |
prompt = f"{prompt}\nUser: {message}\nAssistant:"
|
| 79 |
return prompt
|
| 80 |
|
| 81 |
+
def generate_seqs(prompt, max_new_tokens=None, stop_sequence=None, temperature=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
output = pipeline(prompt,
|
| 83 |
max_length=200,
|
| 84 |
max_new_tokens = max_new_tokens,
|
| 85 |
stop_sequence = stop_sequence,
|
| 86 |
+
temperature=temperature,
|
| 87 |
do_sample=True,
|
| 88 |
top_k=10,
|
| 89 |
num_return_sequences=1,
|
| 90 |
eos_token_id=tokenizer.eos_token_id)
|
| 91 |
return output[0]['generated_text']
|
| 92 |
|
| 93 |
+
def respond(message, chat_history, instruction, temperature=0.7):
|
| 94 |
+
prompt = format_chat_prompt(message, chat_history, instruction)
|
| 95 |
+
chat_history = chat_history + [[message, ""]]
|
| 96 |
+
stream = generate_seqs(prompt = prompt,
|
| 97 |
+
max_new_tokens=1024,
|
| 98 |
+
stop_sequence=["\nUser:", "<|endoftext|>"],
|
| 99 |
+
temperature=temperature).split('Assistant: ')[-1]
|
| 100 |
+
#stop_sequence to not generate the user answer
|
| 101 |
+
acc_text = ""
|
| 102 |
+
#Streaming the tokens
|
| 103 |
+
for idx, response in enumerate(stream):
|
| 104 |
+
# text_token = response.token.text
|
| 105 |
+
text_token = response
|
| 106 |
+
|
| 107 |
+
# if response.details:
|
| 108 |
+
# return
|
| 109 |
+
|
| 110 |
+
if idx == 0 and text_token.startswith(" "):
|
| 111 |
+
text_token = text_token[1:]
|
| 112 |
+
|
| 113 |
+
acc_text += text_token
|
| 114 |
+
last_turn = list(chat_history.pop(-1))
|
| 115 |
+
last_turn[-1] += acc_text
|
| 116 |
+
chat_history = chat_history + [last_turn]
|
| 117 |
+
yield "", chat_history
|
| 118 |
+
acc_text = ""
|
| 119 |
+
|
| 120 |
with gr.Blocks() as demo:
|
| 121 |
chatbot = gr.Chatbot(height=240) #just to fit the notebook
|
| 122 |
msg = gr.Textbox(label="Prompt")
|
| 123 |
+
with gr.Accordion(label="Advanced options",open=False):
|
| 124 |
+
system = gr.Textbox(label="System message", lines=2, value="A conversation between a user and an LLM-based AI assistant. The assistant gives helpful and honest answers.")
|
| 125 |
+
temperature = gr.Slider(label="temperature", minimum=0.1, maximum=1, value=0.7, step=0.1)
|
| 126 |
btn = gr.Button("Submit")
|
| 127 |
clear = gr.ClearButton(components=[msg, chatbot], value="Clear console")
|
| 128 |
|
| 129 |
+
btn.click(respond, inputs=[msg, chatbot, system], outputs=[msg, chatbot])
|
| 130 |
+
msg.submit(respond, inputs=[msg, chatbot, system], outputs=[msg, chatbot]) #Press enter to submit
|
| 131 |
+
gr.close_all()
|
| 132 |
+
demo.queue().launch()
|