Spaces:
Running
Running
Update chatbot.py
Browse files- chatbot.py +6 -14
chatbot.py
CHANGED
|
@@ -1,34 +1,26 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, BitsAndBytesConfig
|
| 3 |
import torch
|
| 4 |
-
from huggingface_hub import login
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
# Authenticate with secret token
|
| 8 |
-
login(token=os.getenv("HF_TOKEN"))
|
| 9 |
# ================= CACHE THE MODEL =================
|
| 10 |
@st.cache_resource
|
| 11 |
def load_model():
|
| 12 |
-
model_id = "ammoncoder123/
|
| 13 |
|
| 14 |
-
st.write("Loading tokenizer...")
|
| 15 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 16 |
-
|
| 17 |
-
st.write("Loading model (this may take a few minutes the first time)...")
|
| 18 |
quantization_config = BitsAndBytesConfig(
|
| 19 |
load_in_4bit=True,
|
| 20 |
bnb_4bit_compute_dtype=torch.float16
|
| 21 |
)
|
| 22 |
|
|
|
|
| 23 |
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
model_id,
|
| 25 |
quantization_config=quantization_config,
|
| 26 |
-
device_map="auto",
|
| 27 |
torch_dtype=torch.float16,
|
| 28 |
-
trust_remote_code=True
|
| 29 |
)
|
| 30 |
|
| 31 |
-
st.write("Model loaded successfully!")
|
| 32 |
return pipeline(
|
| 33 |
"text-generation",
|
| 34 |
model=model,
|
|
@@ -44,7 +36,7 @@ pipe = load_model()
|
|
| 44 |
# ==================== CHAT INTERFACE ====================
|
| 45 |
st.title("My 1.7B Fine-Tuned IPT Chatbot")
|
| 46 |
|
| 47 |
-
st.info("
|
| 48 |
|
| 49 |
if "messages" not in st.session_state:
|
| 50 |
st.session_state.messages = []
|
|
@@ -63,7 +55,7 @@ if prompt := st.chat_input("Ask about IPT, ICT, or anything..."):
|
|
| 63 |
chat_messages = [{"role": "user", "content": prompt}]
|
| 64 |
outputs = pipe(chat_messages, max_new_tokens=300, temperature=0.7, do_sample=True, top_p=0.9)
|
| 65 |
response = outputs[0]["generated_text"]
|
| 66 |
-
if response.startswith(prompt):
|
| 67 |
response = response[len(prompt):].strip()
|
| 68 |
st.markdown(response)
|
| 69 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, BitsAndBytesConfig
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
| 5 |
# ================= CACHE THE MODEL =================
|
| 6 |
@st.cache_resource
|
| 7 |
def load_model():
|
| 8 |
+
model_id = "ammoncoder123/IPTchatbotModel1-1.7B"
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
quantization_config = BitsAndBytesConfig(
|
| 11 |
load_in_4bit=True,
|
| 12 |
bnb_4bit_compute_dtype=torch.float16
|
| 13 |
)
|
| 14 |
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 16 |
model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
model_id,
|
| 18 |
quantization_config=quantization_config,
|
| 19 |
+
device_map="auto",
|
| 20 |
torch_dtype=torch.float16,
|
| 21 |
+
trust_remote_code=True
|
| 22 |
)
|
| 23 |
|
|
|
|
| 24 |
return pipeline(
|
| 25 |
"text-generation",
|
| 26 |
model=model,
|
|
|
|
| 36 |
# ==================== CHAT INTERFACE ====================
|
| 37 |
st.title("My 1.7B Fine-Tuned IPT Chatbot")
|
| 38 |
|
| 39 |
+
st.info("Small fine-tuned model (1.7B). Answers may vary — verify facts.")
|
| 40 |
|
| 41 |
if "messages" not in st.session_state:
|
| 42 |
st.session_state.messages = []
|
|
|
|
| 55 |
chat_messages = [{"role": "user", "content": prompt}]
|
| 56 |
outputs = pipe(chat_messages, max_new_tokens=300, temperature=0.7, do_sample=True, top_p=0.9)
|
| 57 |
response = outputs[0]["generated_text"]
|
| 58 |
+
if isinstance(response, str) and response.startswith(prompt):
|
| 59 |
response = response[len(prompt):].strip()
|
| 60 |
st.markdown(response)
|
| 61 |
|