Spaces:
Sleeping
Sleeping
File size: 1,721 Bytes
b0d4a6b 29448ff d536b28 b0d4a6b 7a800a0 3c58ac1 bcd5c93 d536b28 7a800a0 bcd5c93 d536b28 bcd5c93 a6893c8 eeec3be bcd5c93 7a800a0 b0d4a6b 4389305 eeec3be b0d4a6b eeec3be 7a800a0 eeec3be 4389305 eeec3be 7a800a0 eeec3be b0d4a6b 7a800a0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, BitsAndBytesConfig
# Model name
MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
bnb_config = BitsAndBytesConfig(
load_in_4bit=True, # quantize to 4-bit
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
)
# Load tokenizer and model for CPU
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForCausalLM.from_pretrained(
MODEL_NAME,
device_map="auto",
torch_dtype=torch.float16
)
# Build pipeline
generator = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
device_map="cpu"
)
system_prompt = """
You are Gompei the Goat, the beloved mascot of Worcester Polytechnic Institute (WPI).
Speak in a friendly, school-spirited, and enthusiastic tone. Respond in 1-2 sentences for greetings.
Provide detailed WPI facts only when asked a question.
"""
def chatbot(message, history):
# Build context with system prompt
context = system_prompt
for user, bot in history:
context += f"\nUser: {user}\nGompei: {bot}"
context += f"\nUser: {message}\nGompei:"
# Generate response
response = generator(
context,
max_new_tokens=140,
do_sample=True,
temperature=0.7,
top_p=0.9
)
reply = response[0]["generated_text"].split("Gompei:")[-1].strip()
return reply
demo = gr.ChatInterface(
fn=chatbot,
title="Chat with Gompei the Goat 🐐",
description="Ask Gompei questions about WPI!",
type="messages" # ✅ avoids the deprecation warning
)
if __name__ == "__main__":
demo.launch()
|