Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +54 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import streamlit as st
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
4 |
+
|
5 |
+
# Model name
|
6 |
+
model_name = "ybelkada/falcon-7b-sharded-bf16"
|
7 |
+
|
8 |
+
# Load the tokenizer
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
10 |
+
tokenizer.pad_token = tokenizer.eos_token
|
11 |
+
|
12 |
+
# Load model with quantization
|
13 |
+
bnb_config = BitsAndBytesConfig(
|
14 |
+
load_in_4bit=True,
|
15 |
+
bnb_4bit_quant_type="nf4",
|
16 |
+
bnb_4bit_compute_dtype=torch.float16,
|
17 |
+
)
|
18 |
+
|
19 |
+
model = AutoModelForCausalLM.from_pretrained(
|
20 |
+
model_name,
|
21 |
+
quantization_config=bnb_config,
|
22 |
+
trust_remote_code=True
|
23 |
+
)
|
24 |
+
model.config.use_cache = False
|
25 |
+
|
26 |
+
# Streamlit UI
|
27 |
+
st.title("🦜 Falcon-7B Chatbot")
|
28 |
+
st.write("Ask me anything!")
|
29 |
+
|
30 |
+
# Store chat history
|
31 |
+
if "chat_history" not in st.session_state:
|
32 |
+
st.session_state.chat_history = []
|
33 |
+
|
34 |
+
# User input
|
35 |
+
user_input = st.text_input("You:", "")
|
36 |
+
|
37 |
+
if user_input:
|
38 |
+
# Tokenize input
|
39 |
+
inputs = tokenizer(user_input, return_tensors="pt")
|
40 |
+
|
41 |
+
# Generate response
|
42 |
+
with torch.no_grad():
|
43 |
+
output = model.generate(**inputs, max_length=200, do_sample=True, top_k=50, top_p=0.95)
|
44 |
+
|
45 |
+
# Decode response
|
46 |
+
response = tokenizer.decode(output[:, inputs.input_ids.shape[-1]:][0], skip_special_tokens=True)
|
47 |
+
|
48 |
+
# Store and display chat history
|
49 |
+
st.session_state.chat_history.append(("You", user_input))
|
50 |
+
st.session_state.chat_history.append(("Bot", response))
|
51 |
+
|
52 |
+
# Display chat history
|
53 |
+
for sender, message in st.session_state.chat_history:
|
54 |
+
st.write(f"**{sender}:** {message}")
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
torch
|
3 |
+
transformers
|
4 |
+
bitsandbytes
|