SiennaClarke commited on
Commit
cd09b92
·
verified ·
1 Parent(s): 8fe6af1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -27
app.py CHANGED
@@ -1,59 +1,63 @@
1
  import streamlit as st
2
- from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
3
  from threading import Thread
4
  import torch
5
 
6
- # 1. Page Configuration (Centered and No Sidebar)
7
- st.set_page_config(
8
- page_title="Qwen 3 0.6B Chat",
9
- page_icon="⚡",
10
- layout="centered",
11
- initial_sidebar_state="collapsed"
12
- )
13
-
14
- # Custom CSS to hide the sidebar toggle button entirely
15
  st.markdown("<style>[data-testid='collapsedControl'] { display: none; }</style>", unsafe_allow_html=True)
16
 
17
- # 2. Model & Tokenizer Initialization (Using your direct load logic)
18
- MODEL_ID = "Qwen/Qwen3-0.6B"
19
 
20
  @st.cache_resource
21
  def load_llm():
22
- # Loading the tokenizer and model directly as requested
23
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
 
 
 
 
 
 
 
 
24
  model = AutoModelForCausalLM.from_pretrained(
25
  MODEL_ID,
26
- device_map="auto",
27
- torch_dtype="auto"
28
  )
29
  return tokenizer, model
30
 
31
  tokenizer, model = load_llm()
32
 
33
- # 3. Chat UI Logic
34
- st.title(" Qwen 3 0.6B")
35
- st.caption("Using your direct-load logic with real-time streaming.")
36
 
37
  if "messages" not in st.session_state:
38
  st.session_state.messages = []
39
 
 
 
 
 
 
40
  # Display history
41
  for msg in st.session_state.messages:
42
  with st.chat_message(msg["role"]):
43
  st.markdown(msg["content"])
44
 
45
- # 4. Input & Streaming Generation
46
- if prompt := st.chat_input("Ask Qwen 3..."):
47
- # Store and display user message
48
  st.session_state.messages.append({"role": "user", "content": prompt})
49
  with st.chat_message("user"):
50
  st.markdown(prompt)
51
 
52
  with st.chat_message("assistant"):
53
- # Initialize the streamer
54
  streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
55
 
56
- # Using your chat template logic
57
  inputs = tokenizer.apply_chat_template(
58
  st.session_state.messages,
59
  add_generation_prompt=True,
@@ -62,24 +66,22 @@ if prompt := st.chat_input("Ask Qwen 3..."):
62
  return_tensors="pt",
63
  ).to(model.device)
64
 
65
- # Background thread for generation
66
  generation_kwargs = dict(
67
  **inputs,
68
  streamer=streamer,
69
  max_new_tokens=512,
70
  do_sample=True,
71
  temperature=0.7,
72
- top_p=0.8,
73
  pad_token_id=tokenizer.eos_token_id
74
  )
75
 
76
  thread = Thread(target=model.generate, kwargs=generation_kwargs)
77
  thread.start()
78
 
79
- # Update the UI as tokens arrive
80
  placeholder = st.empty()
81
  full_response = ""
82
-
83
  for new_text in streamer:
84
  full_response += new_text
85
  placeholder.markdown(full_response + "▌")
 
1
  import streamlit as st
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer, BitsAndBytesConfig
3
  from threading import Thread
4
  import torch
5
 
6
+ # UI Setup (No Sidebar as requested)
7
+ st.set_page_config(page_title="Qwen 2.5 32B Chat", page_icon="🐘", layout="centered", initial_sidebar_state="collapsed")
 
 
 
 
 
 
 
8
  st.markdown("<style>[data-testid='collapsedControl'] { display: none; }</style>", unsafe_allow_html=True)
9
 
10
+ # 1. Model Configuration (Quantized to fit on 24GB VRAM or 32GB RAM)
11
+ MODEL_ID = "Qwen/Qwen2.5-32B-Instruct"
12
 
13
  @st.cache_resource
14
  def load_llm():
 
15
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
16
+
17
+ # 4-bit config allows this 64GB model to fit in ~18-20GB of memory
18
+ quant_config = BitsAndBytesConfig(
19
+ load_in_4bit=True,
20
+ bnb_4bit_compute_dtype=torch.float16,
21
+ bnb_4bit_quant_type="nf4"
22
+ )
23
+
24
  model = AutoModelForCausalLM.from_pretrained(
25
  MODEL_ID,
26
+ quantization_config=quant_config,
27
+ device_map="auto" # Automatically splits between GPU and CPU
28
  )
29
  return tokenizer, model
30
 
31
  tokenizer, model = load_llm()
32
 
33
+ # 2. Chat Interface
34
+ st.title("🐘 Qwen 2.5 32B")
35
+ st.caption("Running high-parameter model with 4-bit quantization")
36
 
37
  if "messages" not in st.session_state:
38
  st.session_state.messages = []
39
 
40
+ # Action Button
41
+ if st.button("Clear History"):
42
+ st.session_state.messages = []
43
+ st.rerun()
44
+
45
  # Display history
46
  for msg in st.session_state.messages:
47
  with st.chat_message(msg["role"]):
48
  st.markdown(msg["content"])
49
 
50
+ # 3. Chat Logic with your exact Template Code
51
+ if prompt := st.chat_input("Message Qwen 2.5 32B..."):
 
52
  st.session_state.messages.append({"role": "user", "content": prompt})
53
  with st.chat_message("user"):
54
  st.markdown(prompt)
55
 
56
  with st.chat_message("assistant"):
57
+ # Setup Streamer
58
  streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
59
 
60
+ # YOUR EXACT LOGIC: Applying the chat template
61
  inputs = tokenizer.apply_chat_template(
62
  st.session_state.messages,
63
  add_generation_prompt=True,
 
66
  return_tensors="pt",
67
  ).to(model.device)
68
 
69
+ # Threading for live streaming
70
  generation_kwargs = dict(
71
  **inputs,
72
  streamer=streamer,
73
  max_new_tokens=512,
74
  do_sample=True,
75
  temperature=0.7,
 
76
  pad_token_id=tokenizer.eos_token_id
77
  )
78
 
79
  thread = Thread(target=model.generate, kwargs=generation_kwargs)
80
  thread.start()
81
 
82
+ # Word-by-word UI update
83
  placeholder = st.empty()
84
  full_response = ""
 
85
  for new_text in streamer:
86
  full_response += new_text
87
  placeholder.markdown(full_response + "▌")