Spaces:
Runtime error
Runtime error
Commit
Β·
0e02ca5
1
Parent(s):
4a8282c
working streaming interface
Browse files- app.py +112 -14
- requirements.txt +4 -1
app.py
CHANGED
|
@@ -1,13 +1,19 @@
|
|
| 1 |
# https://www.gradio.app/guides/using-hugging-face-integrations
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
|
|
|
| 11 |
title = "Shisa 7B"
|
| 12 |
description = "Test out Shisa 7B in either English or Japanese."
|
| 13 |
placeholder = "Type Here / γγγ«ε
₯εγγ¦γγ γγ"
|
|
@@ -18,23 +24,114 @@ examples = [
|
|
| 18 |
"γγγ«γ‘γ―γγγγγιγγγ§γγοΌ",
|
| 19 |
]
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# Docs: https://github.com/huggingface/transformers/blob/main/src/transformers/pipelines/conversational.py
|
| 22 |
conversation = Conversation()
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
def chat(input, history
|
| 26 |
conversation.add_message({"role": "user", "content": input})
|
| 27 |
# we do this shuffle so local shadow response doesn't get created
|
| 28 |
-
response_conversation =
|
| 29 |
-
print(response_conversation)
|
| 30 |
-
print(response_conversation.messages)
|
| 31 |
-
print(response_conversation.messages[-1]["content"])
|
| 32 |
|
| 33 |
conversation.add_message(response_conversation.messages[-1])
|
|
|
|
| 34 |
response = conversation.messages[-1]["content"]
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
gr.ChatInterface(
|
| 38 |
chat,
|
| 39 |
chatbot=gr.Chatbot(height=400),
|
| 40 |
textbox=gr.Textbox(placeholder=placeholder, container=False, scale=7),
|
|
@@ -48,4 +145,5 @@ gr.ChatInterface(
|
|
| 48 |
).launch()
|
| 49 |
|
| 50 |
# For async
|
| 51 |
-
# ).queue().launch(
|
|
|
|
|
|
| 1 |
# https://www.gradio.app/guides/using-hugging-face-integrations
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
+
import logging
|
| 5 |
+
import html
|
| 6 |
+
import time
|
| 7 |
+
import torch
|
| 8 |
+
from threading import Thread
|
| 9 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 10 |
|
| 11 |
+
# Model
|
| 12 |
+
model_name = "mistralai/Mistral-7B-Instruct-v0.1"
|
| 13 |
+
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v0.3"
|
| 14 |
+
model_name = "/models/llm/hf/mistralai_Mistral-7B-Instruct-v0.1"
|
| 15 |
|
| 16 |
+
# UI Settings
|
| 17 |
title = "Shisa 7B"
|
| 18 |
description = "Test out Shisa 7B in either English or Japanese."
|
| 19 |
placeholder = "Type Here / γγγ«ε
₯εγγ¦γγ γγ"
|
|
|
|
| 24 |
"γγγ«γ‘γ―γγγγγιγγγ§γγοΌ",
|
| 25 |
]
|
| 26 |
|
| 27 |
+
# LLM Settings
|
| 28 |
+
system_prompt = 'You are a helpful, friendly assistant.'
|
| 29 |
+
chat_history = [{"role": "system", "content": system_prompt}]
|
| 30 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 31 |
+
tokenizer.chat_template = "{%- for idx in range(0, messages|length) -%}\n{%- if messages[idx]['role'] == 'user' -%}\n{%- if idx > 1 -%}\n{{- bos_token + '[INST] ' + messages[idx]['content'] + ' [/INST]' -}}\n{%- else -%}\n{{- messages[idx]['content'] + ' [/INST]' -}}\n{%- endif -%}\n{% elif messages[idx]['role'] == 'system' %}\n{{- '[INST] <<SYS>>\\n' + messages[idx]['content'] + '\\n<</SYS>>\\n\\n' -}}\n{%- elif messages[idx]['role'] == 'assistant' -%}\n{{- ' ' + messages[idx]['content'] + ' ' + eos_token -}}\n{% endif %}\n{% endfor %}\n"
|
| 32 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 33 |
+
model_name,
|
| 34 |
+
torch_dtype=torch.bfloat16,
|
| 35 |
+
device_map="auto",
|
| 36 |
+
load_in_8bit=True,
|
| 37 |
+
)
|
| 38 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
| 39 |
+
|
| 40 |
+
def chat(message, history):
|
| 41 |
+
chat_history.append({"role": "user", "content": message})
|
| 42 |
+
input_ids = tokenizer.apply_chat_template(chat_history, add_generation_prompt=True, return_tensors="pt").to('cuda')
|
| 43 |
+
generate_kwargs = dict(
|
| 44 |
+
inputs=input_ids,
|
| 45 |
+
streamer=streamer,
|
| 46 |
+
max_new_tokens=200,
|
| 47 |
+
do_sample=True,
|
| 48 |
+
temperature=0.7,
|
| 49 |
+
top_p=0.95,
|
| 50 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 51 |
+
)
|
| 52 |
+
# https://www.gradio.app/main/guides/creating-a-chatbot-fast#example-using-a-local-open-source-llm-with-hugging-face
|
| 53 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 54 |
+
t.start()
|
| 55 |
+
partial_message = ""
|
| 56 |
+
for new_token in streamer:
|
| 57 |
+
partial_message += new_token # html.escape(new_token)
|
| 58 |
+
yield partial_message
|
| 59 |
+
|
| 60 |
+
'''
|
| 61 |
+
# https://www.gradio.app/main/guides/creating-a-chatbot-fast#streaming-chatbots
|
| 62 |
+
for i in range(len(message)):
|
| 63 |
+
time.sleep(0.3)
|
| 64 |
+
yield message[: i+1]
|
| 65 |
+
'''
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
chat_interface = gr.ChatInterface(
|
| 69 |
+
chat,
|
| 70 |
+
chatbot=gr.Chatbot(height=400),
|
| 71 |
+
textbox=gr.Textbox(placeholder=placeholder, container=False, scale=7),
|
| 72 |
+
title=title,
|
| 73 |
+
description=description,
|
| 74 |
+
theme="soft",
|
| 75 |
+
examples=examples,
|
| 76 |
+
cache_examples=False,
|
| 77 |
+
undo_btn="Delete Previous",
|
| 78 |
+
clear_btn="Clear",
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI/blob/main/app.py#L219 - we use this with construction b/c Gradio barfs on autoreload otherwise
|
| 82 |
+
with gr.Blocks() as demo:
|
| 83 |
+
chat_interface.render()
|
| 84 |
+
gr.Markdown("You can try these greetings in English, Japanese, familiar Japanese, or formal Japanese. We limit output to 200 tokens.")
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
demo.queue().launch()
|
| 88 |
+
|
| 89 |
+
'''
|
| 90 |
+
# Works for Text input...
|
| 91 |
+
demo = gr.Interface.from_pipeline(pipe)
|
| 92 |
+
'''
|
| 93 |
+
|
| 94 |
+
'''
|
| 95 |
+
def chat(message, history):
|
| 96 |
+
print("foo")
|
| 97 |
+
for i in range(len(message)):
|
| 98 |
+
time.sleep(0.3)
|
| 99 |
+
yield "You typed: " + message[: i+1]
|
| 100 |
+
# print('history:', history)
|
| 101 |
+
# print('message:', message)
|
| 102 |
+
# for new_next in streamer:
|
| 103 |
+
# yield new_text
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
'''
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
'''
|
| 110 |
# Docs: https://github.com/huggingface/transformers/blob/main/src/transformers/pipelines/conversational.py
|
| 111 |
conversation = Conversation()
|
| 112 |
+
conversation.add_message({"role": "system", "content": system})
|
| 113 |
+
device = torch.device('cuda')
|
| 114 |
+
pipe = pipeline(
|
| 115 |
+
'conversational',
|
| 116 |
+
model=model,
|
| 117 |
+
tokenizer=tokenizer,
|
| 118 |
+
streamer=streamer,
|
| 119 |
+
|
| 120 |
+
)
|
| 121 |
|
| 122 |
+
def chat(input, history):
|
| 123 |
conversation.add_message({"role": "user", "content": input})
|
| 124 |
# we do this shuffle so local shadow response doesn't get created
|
| 125 |
+
response_conversation = pipe(conversation)
|
| 126 |
+
print("foo:", response_conversation.messages[-1]["content"])
|
|
|
|
|
|
|
| 127 |
|
| 128 |
conversation.add_message(response_conversation.messages[-1])
|
| 129 |
+
print("boo:", response_conversation.messages[-1]["content"])
|
| 130 |
response = conversation.messages[-1]["content"]
|
| 131 |
+
response = "ping"
|
| 132 |
+
return response
|
| 133 |
|
| 134 |
+
demo = gr.ChatInterface(
|
| 135 |
chat,
|
| 136 |
chatbot=gr.Chatbot(height=400),
|
| 137 |
textbox=gr.Textbox(placeholder=placeholder, container=False, scale=7),
|
|
|
|
| 145 |
).launch()
|
| 146 |
|
| 147 |
# For async
|
| 148 |
+
# ).queue().launch()
|
| 149 |
+
'''
|
requirements.txt
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
| 1 |
gradio
|
|
|
|
| 2 |
torch
|
| 3 |
-
transformers
|
|
|
|
| 1 |
+
accelerate
|
| 2 |
+
bitsandbytes
|
| 3 |
gradio
|
| 4 |
+
scipy
|
| 5 |
torch
|
| 6 |
+
transformers
|