MadT21 commited on
Commit
bcd5c93
·
verified ·
1 Parent(s): c92815e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -5
app.py CHANGED
@@ -1,20 +1,41 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
 
4
- # Load Zephyr (smaller instruct model for Hugging Face hosting)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  generator = pipeline(
6
  "text-generation",
7
- model="HuggingFaceH4/zephyr-7b-beta",
 
8
  device_map="auto"
9
  )
10
 
 
11
  system_prompt = """You are Gompei the Goat, the beloved mascot of Worcester Polytechnic Institute (WPI).
12
  Speak in a friendly, school-spirited, and enthusiastic tone.
13
  Always provide interesting facts about WPI when asked questions, and stay in character as Gompei.
14
  """
15
 
 
 
 
16
  def chatbot(message, history):
17
- # Build context with system prompt
 
18
  context = system_prompt
19
  for user, bot in history:
20
  context += f"\nUser: {user}\nGompei: {bot}"
@@ -29,8 +50,12 @@ def chatbot(message, history):
29
  top_p=0.9
30
  )
31
  reply = response[0]["generated_text"].split("Gompei:")[-1].strip()
 
 
 
32
  return reply
33
 
 
34
  demo = gr.ChatInterface(
35
  fn=chatbot,
36
  title="Chat with Gompei the Goat 🐐",
@@ -38,4 +63,5 @@ demo = gr.ChatInterface(
38
  )
39
 
40
  if __name__ == "__main__":
41
- demo.launch()
 
 
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,
23
+ tokenizer=tokenizer,
24
  device_map="auto"
25
  )
26
 
27
+ # System prompt for Gompei
28
  system_prompt = """You are Gompei the Goat, the beloved mascot of Worcester Polytechnic Institute (WPI).
29
  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
  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
59
  demo = gr.ChatInterface(
60
  fn=chatbot,
61
  title="Chat with Gompei the Goat 🐐",
 
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)