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