Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
!pip install -U "transformers==4.40.0" --upgrade
|
| 2 |
+
!pip install -i https://pypi.org/simple/ bitsandbytes
|
| 3 |
+
!pip install accelerate
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
import transformers
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
model_id = "unsloth/llama-3-8b-Instruct-bnb-4bit"
|
| 10 |
+
|
| 11 |
+
pipeline = transformers.pipeline(
|
| 12 |
+
"text-generation",
|
| 13 |
+
model=model_id,
|
| 14 |
+
model_kwargs={
|
| 15 |
+
"torch_dtype": torch.float16,
|
| 16 |
+
"quantization_config": {"load_in_4bit": True},
|
| 17 |
+
"low_cpu_mem_usage": True,
|
| 18 |
+
},
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
messages = [
|
| 22 |
+
{"role" : "system",
|
| 23 |
+
"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"},
|
| 24 |
+
{"role" : "user",
|
| 25 |
+
"content": """hi there!"""},
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
prompt = pipeline.tokenizer.apply_chat_template(
|
| 29 |
+
messages,
|
| 30 |
+
tokenize=False,
|
| 31 |
+
add_generation_prompt=True
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
terminators = [
|
| 35 |
+
pipeline.tokenizer.eos_token_id,
|
| 36 |
+
pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
outputs = pipeline(
|
| 40 |
+
prompt,
|
| 41 |
+
max_new_tokens=256,
|
| 42 |
+
eos_token_id=terminators,
|
| 43 |
+
do_sample=True,
|
| 44 |
+
temperature=0.6,
|
| 45 |
+
top_p=0.9,
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
print(outputs[0]["generated_text"][len(prompt):])
|
| 49 |
+
|
| 50 |
+
!pip install gradio
|
| 51 |
+
|
| 52 |
+
import gradio as gr
|
| 53 |
+
|
| 54 |
+
messages = [{"role" : "system",
|
| 55 |
+
"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"},
|
| 56 |
+
{"role" : "user",
|
| 57 |
+
"content": """hi there!"""},]
|
| 58 |
+
|
| 59 |
+
def add_text(history, text):
|
| 60 |
+
global messages #message[list] is defined globally
|
| 61 |
+
history = history + [(text,'')]
|
| 62 |
+
messages = messages + [{"role":'user', 'content': text}]
|
| 63 |
+
return history, ''
|
| 64 |
+
|
| 65 |
+
def generate(history):
|
| 66 |
+
global messages
|
| 67 |
+
prompt = pipeline.tokenizer.apply_chat_template(
|
| 68 |
+
messages,
|
| 69 |
+
tokenize=False,
|
| 70 |
+
add_generation_prompt=True
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
terminators = [
|
| 74 |
+
pipeline.tokenizer.eos_token_id,
|
| 75 |
+
pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
+
outputs = pipeline(
|
| 79 |
+
prompt,
|
| 80 |
+
max_new_tokens=256,
|
| 81 |
+
eos_token_id=terminators,
|
| 82 |
+
do_sample=True,
|
| 83 |
+
temperature=0.6,
|
| 84 |
+
top_p=0.9,
|
| 85 |
+
)
|
| 86 |
+
response_msg = outputs[0]["generated_text"][len(prompt):]
|
| 87 |
+
for char in response_msg:
|
| 88 |
+
history[-1][1] += char
|
| 89 |
+
yield history
|
| 90 |
+
pass
|
| 91 |
+
|
| 92 |
+
with gr.Blocks() as demo:
|
| 93 |
+
|
| 94 |
+
chatbot = gr.Chatbot(value=[], elem_id="chatbot")
|
| 95 |
+
with gr.Row():
|
| 96 |
+
txt = gr.Textbox(
|
| 97 |
+
show_label=False,
|
| 98 |
+
placeholder="Enter text and press enter",
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
| 102 |
+
generate, inputs =[chatbot,],outputs = chatbot,)
|
| 103 |
+
|
| 104 |
+
demo.queue()
|
| 105 |
+
demo.launch(debug=True)
|