MadT21 commited on
Commit
a6893c8
·
verified ·
1 Parent(s): 2c9c43c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -17
app.py CHANGED
@@ -1,22 +1,35 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
 
 
 
 
 
3
 
4
  MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
5
- OFFLOAD_DIR = "./offload" # Folder to store offloaded weights
 
 
 
 
 
 
 
 
6
 
7
  # Load tokenizer
8
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
9
 
10
- # Load model with 8-bit quantization and offloading
11
  model = AutoModelForCausalLM.from_pretrained(
12
  MODEL_NAME,
13
- device_map="auto", # automatically places layers on CPU/GPU
14
- load_in_8bit=True, # reduce memory usage
15
- offload_folder=OFFLOAD_DIR,
16
- torch_dtype="auto" # chooses FP16 if possible
17
  )
18
 
19
- # Create text-generation pipeline
20
  generator = pipeline(
21
  "text-generation",
22
  model=model,
@@ -30,12 +43,8 @@ Speak in a friendly, school-spirited, and enthusiastic tone.
30
  Always provide interesting facts about WPI when asked questions, and stay in character as Gompei.
31
  """
32
 
33
- # Chat history
34
- chat_history = []
35
-
36
  def chatbot(message, history):
37
- global chat_history
38
- # Build context with system prompt and previous conversation
39
  context = system_prompt
40
  for user, bot in history:
41
  context += f"\nUser: {user}\nGompei: {bot}"
@@ -50,9 +59,6 @@ def chatbot(message, history):
50
  top_p=0.9
51
  )
52
  reply = response[0]["generated_text"].split("Gompei:")[-1].strip()
53
-
54
- # Update history
55
- chat_history.append((message, reply))
56
  return reply
57
 
58
  # Gradio interface
@@ -63,5 +69,4 @@ demo = gr.ChatInterface(
63
  )
64
 
65
  if __name__ == "__main__":
66
- # Disable SSR for faster build
67
  demo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)
 
1
  import gradio as gr
2
+ from transformers import (
3
+ AutoModelForCausalLM,
4
+ AutoTokenizer,
5
+ pipeline,
6
+ BitsAndBytesConfig
7
+ )
8
 
9
  MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
10
+ OFFLOAD_DIR = "./offload"
11
+
12
+ # Configure 8-bit quantization with CPU fallback
13
+ bnb_config = BitsAndBytesConfig(
14
+ load_in_8bit=True,
15
+ llm_int8_threshold=6.0,
16
+ llm_int8_has_fp16_weight=False,
17
+ llm_int8_enable_fp32_cpu_offload=True # ✅ allow CPU fallback
18
+ )
19
 
20
  # Load tokenizer
21
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
22
 
23
+ # Load model with quantization + offloading
24
  model = AutoModelForCausalLM.from_pretrained(
25
  MODEL_NAME,
26
+ device_map="auto", # spreads across CPU/GPU automatically
27
+ quantization_config=bnb_config,
28
+ offload_folder=OFFLOAD_DIR, # spill to disk if RAM full
29
+ dtype="auto"
30
  )
31
 
32
+ # Build pipeline
33
  generator = pipeline(
34
  "text-generation",
35
  model=model,
 
43
  Always provide interesting facts about WPI when asked questions, and stay in character as Gompei.
44
  """
45
 
 
 
 
46
  def chatbot(message, history):
47
+ # Build context with system prompt + conversation history
 
48
  context = system_prompt
49
  for user, bot in history:
50
  context += f"\nUser: {user}\nGompei: {bot}"
 
59
  top_p=0.9
60
  )
61
  reply = response[0]["generated_text"].split("Gompei:")[-1].strip()
 
 
 
62
  return reply
63
 
64
  # Gradio interface
 
69
  )
70
 
71
  if __name__ == "__main__":
 
72
  demo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)