Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,32 +2,28 @@ import streamlit as st
|
|
2 |
from transformers import pipeline
|
3 |
import torch
|
4 |
|
5 |
-
MODEL_PATH = "HuggingFaceH4/zephyr-7b-beta"
|
6 |
|
7 |
def main():
|
8 |
st.title("Chatbot with Hugging Face Model")
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
pipe = pipeline("text-generation", model=MODEL_PATH, torch_dtype=torch.bfloat16, device_map="auto")
|
13 |
-
except:
|
14 |
-
# If not saved, load the model and save it
|
15 |
-
st.warning("Model not found locally. Downloading and saving the model. Please wait...")
|
16 |
-
pipe = pipeline("text-generation", model="HuggingFaceH4/zephyr-7b-beta", torch_dtype=torch.bfloat16, device_map="auto")
|
17 |
-
pipe.save_pretrained(MODEL_PATH)
|
18 |
|
|
|
|
|
19 |
# Define chat messages
|
20 |
messages = [
|
21 |
-
{"role": "system", "content": "You are a friendly chatbot who always responds in
|
22 |
-
{"role": "user", "content":
|
23 |
]
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
31 |
|
32 |
if __name__ == "__main__":
|
33 |
main()
|
|
|
2 |
from transformers import pipeline
|
3 |
import torch
|
4 |
|
|
|
5 |
|
6 |
def main():
|
7 |
st.title("Chatbot with Hugging Face Model")
|
8 |
|
9 |
+
# If not saved, load the model and save it
|
10 |
+
pipe = pipeline("text-generation", model="HuggingFaceH4/zephyr-7b-beta", torch_dtype=torch.bfloat16, device_map="auto")
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
|
13 |
+
user_input = st.text_area("Enter Text")
|
14 |
# Define chat messages
|
15 |
messages = [
|
16 |
+
{"role": "system", "content": "You are a friendly chatbot who always responds in kind way."},
|
17 |
+
{"role": "user", "content": user_input},
|
18 |
]
|
19 |
+
submit = st.button('Generate')
|
20 |
+
if submit:
|
21 |
+
# Generate response
|
22 |
+
prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
23 |
+
outputs = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
|
24 |
+
|
25 |
+
# Display generated text
|
26 |
+
st.text(outputs[0]["generated_text"])
|
27 |
|
28 |
if __name__ == "__main__":
|
29 |
main()
|