Spaces:
Runtime error
Runtime error
'''import gradio as gr | |
from huggingface_hub import InferenceClient | |
""" | |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference | |
""" | |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") | |
def respond( | |
message, | |
history: list[tuple[str, str]], | |
system_message, | |
max_tokens, | |
temperature, | |
top_p, | |
): | |
messages = [{"role": "system", "content": system_message}] | |
for val in history: | |
if val[0]: | |
messages.append({"role": "user", "content": val[0]}) | |
if val[1]: | |
messages.append({"role": "assistant", "content": val[1]}) | |
messages.append({"role": "user", "content": message}) | |
response = "" | |
for message in client.chat_completion( | |
messages, | |
max_tokens=max_tokens, | |
stream=True, | |
temperature=temperature, | |
top_p=top_p, | |
): | |
token = message.choices[0].delta.content | |
response += token | |
yield response | |
""" | |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface | |
""" | |
demo = gr.ChatInterface( | |
respond, | |
additional_inputs=[ | |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"), | |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), | |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), | |
gr.Slider( | |
minimum=0.1, | |
maximum=1.0, | |
value=0.95, | |
step=0.05, | |
label="Top-p (nucleus sampling)", | |
), | |
], | |
) | |
if __name__ == "__main__": | |
demo.launch()''' | |
#!pip install -U "transformers==4.40.0" --upgrade | |
#!pip install -i https://pypi.org/simple/ bitsandbytes | |
#!pip install accelerate | |
import transformers | |
import torch | |
model_id = "unsloth/llama-3-8b-Instruct-bnb-4bit" | |
pipeline = transformers.pipeline( | |
"text-generation", | |
model=model_id, | |
model_kwargs={ | |
"torch_dtype": torch.float16, | |
"quantization_config": {"load_in_4bit": True}, | |
"low_cpu_mem_usage": True, | |
}, | |
) | |
messages = [ | |
{"role" : "system", | |
"content": "You are an interviewer testing the user whether he can be a good manager or not. When the user says hi there!, i want you to begin"}, | |
{"role" : "user", | |
"content": """hi there!"""}, | |
] | |
prompt = pipeline.tokenizer.apply_chat_template( | |
messages, | |
tokenize=False, | |
add_generation_prompt=True | |
) | |
terminators = [ | |
pipeline.tokenizer.eos_token_id, | |
pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>") | |
] | |
outputs = pipeline( | |
prompt, | |
max_new_tokens=256, | |
eos_token_id=terminators, | |
do_sample=True, | |
temperature=0.6, | |
top_p=0.9, | |
) | |
print(outputs[0]["generated_text"][len(prompt):]) | |
#!pip install gradio | |
import gradio as gr | |
messages = [{"role" : "system", | |
"content": "You are an interviewer testing the user whether he can be a good manager or not. When the user says hi there!, i want you to begin"}, | |
{"role" : "user", | |
"content": """hi there!"""},] | |
def add_text(history, text): | |
global messages #message[list] is defined globally | |
history = history + [(text,'')] | |
messages = messages + [{"role":'user', 'content': text}] | |
return history, '' | |
def generate(history): | |
global messages | |
prompt = pipeline.tokenizer.apply_chat_template( | |
messages, | |
tokenize=False, | |
add_generation_prompt=True | |
) | |
terminators = [ | |
pipeline.tokenizer.eos_token_id, | |
pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>") | |
] | |
outputs = pipeline( | |
prompt, | |
max_new_tokens=256, | |
eos_token_id=terminators, | |
do_sample=True, | |
temperature=0.6, | |
top_p=0.9, | |
) | |
response_msg = outputs[0]["generated_text"][len(prompt):] | |
for char in response_msg: | |
history[-1][1] += char | |
yield history | |
pass | |
with gr.Blocks() as demo: | |
chatbot = gr.Chatbot(value=[], elem_id="chatbot") | |
with gr.Row(): | |
txt = gr.Textbox( | |
show_label=False, | |
placeholder="Enter text and press enter", | |
) | |
txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then( | |
generate, inputs =[chatbot,],outputs = chatbot,) | |
demo.queue() | |
demo.launch(debug=True) |