Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,16 @@
|
|
| 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="
|
| 27 |
-
|
| 28 |
-
offload_folder=OFFLOAD_DIR, # spill to disk if RAM full
|
| 29 |
-
dtype="auto"
|
| 30 |
)
|
| 31 |
|
| 32 |
# Build pipeline
|
|
@@ -34,17 +18,16 @@ generator = pipeline(
|
|
| 34 |
"text-generation",
|
| 35 |
model=model,
|
| 36 |
tokenizer=tokenizer,
|
| 37 |
-
device_map="
|
| 38 |
)
|
| 39 |
|
| 40 |
-
# System prompt for Gompei
|
| 41 |
system_prompt = """You are Gompei the Goat, the beloved mascot of Worcester Polytechnic Institute (WPI).
|
| 42 |
Speak in a friendly, school-spirited, and enthusiastic tone.
|
| 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
|
| 48 |
context = system_prompt
|
| 49 |
for user, bot in history:
|
| 50 |
context += f"\nUser: {user}\nGompei: {bot}"
|
|
@@ -61,12 +44,12 @@ def chatbot(message, history):
|
|
| 61 |
reply = response[0]["generated_text"].split("Gompei:")[-1].strip()
|
| 62 |
return reply
|
| 63 |
|
| 64 |
-
# Gradio interface
|
| 65 |
demo = gr.ChatInterface(
|
| 66 |
fn=chatbot,
|
| 67 |
title="Chat with Gompei the Goat 🐐",
|
| 68 |
-
description="Ask Gompei questions about WPI!"
|
|
|
|
| 69 |
)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
-
demo.launch(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Model name
|
| 5 |
MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Load tokenizer and model for CPU
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
|
|
|
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
MODEL_NAME,
|
| 12 |
+
device_map="cpu", # ✅ force CPU
|
| 13 |
+
torch_dtype="float32" # ✅ safe on CPU
|
|
|
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
# Build pipeline
|
|
|
|
| 18 |
"text-generation",
|
| 19 |
model=model,
|
| 20 |
tokenizer=tokenizer,
|
| 21 |
+
device_map="cpu"
|
| 22 |
)
|
| 23 |
|
|
|
|
| 24 |
system_prompt = """You are Gompei the Goat, the beloved mascot of Worcester Polytechnic Institute (WPI).
|
| 25 |
Speak in a friendly, school-spirited, and enthusiastic tone.
|
| 26 |
Always provide interesting facts about WPI when asked questions, and stay in character as Gompei.
|
| 27 |
"""
|
| 28 |
|
| 29 |
def chatbot(message, history):
|
| 30 |
+
# Build context with system prompt
|
| 31 |
context = system_prompt
|
| 32 |
for user, bot in history:
|
| 33 |
context += f"\nUser: {user}\nGompei: {bot}"
|
|
|
|
| 44 |
reply = response[0]["generated_text"].split("Gompei:")[-1].strip()
|
| 45 |
return reply
|
| 46 |
|
|
|
|
| 47 |
demo = gr.ChatInterface(
|
| 48 |
fn=chatbot,
|
| 49 |
title="Chat with Gompei the Goat 🐐",
|
| 50 |
+
description="Ask Gompei questions about WPI!",
|
| 51 |
+
type="messages" # ✅ avoids the deprecation warning
|
| 52 |
)
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|
| 55 |
+
demo.launch()
|