Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 2 |
+
import transformers
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# model = "tiiuae/falcon-40b-instruct"
|
| 6 |
+
model = "tiiuae/falcon-7b-instruct"
|
| 7 |
+
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model)
|
| 9 |
+
pipeline = transformers.pipeline(
|
| 10 |
+
"text-generation",
|
| 11 |
+
model=model,
|
| 12 |
+
tokenizer=tokenizer,
|
| 13 |
+
# torch_dtype=torch.bfloat16,
|
| 14 |
+
trust_remote_code=True,
|
| 15 |
+
device_map="auto",
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
def format_chat_prompt(message, chat_history):
|
| 19 |
+
prompt = ""
|
| 20 |
+
for turn in chat_history:
|
| 21 |
+
user_message, bot_message = turn
|
| 22 |
+
prompt = f"{prompt}\nUser: {user_message}\nAssistant: {bot_message}"
|
| 23 |
+
prompt = f"{prompt}\nUser: {message}\nAssistant:"
|
| 24 |
+
return prompt
|
| 25 |
+
|
| 26 |
+
def respond(message, chat_history):
|
| 27 |
+
formatted_prompt = format_chat_prompt(message, chat_history)
|
| 28 |
+
# print(formatted_prompt)
|
| 29 |
+
bot_message = generate_seqs(prompt = formatted_prompt,
|
| 30 |
+
max_new_tokens=1024,
|
| 31 |
+
stop_sequence=["\nUser:", "<|endoftext|>"]).split('Assistant: ')[-1]
|
| 32 |
+
|
| 33 |
+
chat_history.append((message, bot_message))
|
| 34 |
+
return "", chat_history
|
| 35 |
+
|
| 36 |
+
def generate_seqs(prompt, max_new_tokens=None, stop_sequence=None):
|
| 37 |
+
output = pipeline(prompt,
|
| 38 |
+
max_length=200,
|
| 39 |
+
max_new_tokens = max_new_tokens,
|
| 40 |
+
stop_sequence = stop_sequence,
|
| 41 |
+
do_sample=True,
|
| 42 |
+
top_k=10,
|
| 43 |
+
num_return_sequences=1,
|
| 44 |
+
eos_token_id=tokenizer.eos_token_id)
|
| 45 |
+
return output[0]['generated_text']
|
| 46 |
+
|
| 47 |
+
with gr.Blocks() as demo:
|
| 48 |
+
chatbot = gr.Chatbot(height=240) #just to fit the notebook
|
| 49 |
+
msg = gr.Textbox(label="Prompt")
|
| 50 |
+
btn = gr.Button("Submit")
|
| 51 |
+
clear = gr.ClearButton(components=[msg, chatbot], value="Clear console")
|
| 52 |
+
|
| 53 |
+
btn.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 54 |
+
msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot]) #Press enter to submit
|
| 55 |
+
demo.launch()
|