Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Install dependencies (run once in your environment):
|
2 |
+
# pip install git+https://github.com/huggingface/transformers.git
|
3 |
+
# pip install accelerate
|
4 |
+
# pip install torch
|
5 |
+
|
6 |
+
import torch
|
7 |
+
from transformers import pipeline
|
8 |
+
|
9 |
+
# Initialize the text-generation pipeline with Zephyr model
|
10 |
+
pipe = pipeline(
|
11 |
+
"text-generation",
|
12 |
+
model="HuggingFaceH4/zephyr-7b-beta",
|
13 |
+
torch_dtype=torch.bfloat16,
|
14 |
+
device_map="auto"
|
15 |
+
)
|
16 |
+
|
17 |
+
# Define the chat messages with roles and content
|
18 |
+
messages = [
|
19 |
+
{
|
20 |
+
"role": "system",
|
21 |
+
"content": "You are a friendly chatbot who always responds in the style of a pirate",
|
22 |
+
},
|
23 |
+
{
|
24 |
+
"role": "user",
|
25 |
+
"content": "How many helicopters can a human eat in one sitting?"
|
26 |
+
}
|
27 |
+
]
|
28 |
+
|
29 |
+
# Use the tokenizer's chat template to format the messages correctly
|
30 |
+
prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
31 |
+
|
32 |
+
# Generate response with sampling parameters for creativity
|
33 |
+
outputs = pipe(
|
34 |
+
prompt,
|
35 |
+
max_new_tokens=256,
|
36 |
+
do_sample=True,
|
37 |
+
temperature=0.7,
|
38 |
+
top_k=50,
|
39 |
+
top_p=0.95
|
40 |
+
)
|
41 |
+
|
42 |
+
# Print the generated pirate-style response
|
43 |
+
print(outputs[0]["generated_text"])
|