Spaces:
Sleeping
Sleeping
File size: 2,565 Bytes
516a88a fe64548 516a88a fe64548 516a88a fe64548 516a88a fe64548 516a88a fe64548 516a88a fe64548 516a88a fe64548 516a88a fe64548 516a88a fe64548 516a88a fe64548 516a88a fe64548 516a88a fe64548 516a88a fe64548 516a88a fe64548 516a88a fe64548 516a88a fe64548 516a88a fe64548 516a88a fe64548 516a88a fe64548 f9db3c2 516a88a fe64548 516a88a fe64548 516a88a fe64548 f9db3c2 | 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
import gradio as gr
import torch
import spaces
from transformers import AutoTokenizer, AutoModelForCausalLM
# --------------------------------------------------
# Model configuration
# --------------------------------------------------
REPO = "vermarjun/chad"
SUBFOLDER = "transformers/dpo450"
print("Loading Chad tokenizer...")
tokenizer = AutoTokenizer.from_pretrained(
REPO,
subfolder=SUBFOLDER,
)
print("Loading Chad model...")
model = AutoModelForCausalLM.from_pretrained(
REPO,
subfolder=SUBFOLDER,
torch_dtype=torch.float32,
)
model.eval()
print("Chad model loaded successfully!")
# --------------------------------------------------
# Chat response function
# --------------------------------------------------
@spaces.GPU
def respond(message, history):
if not message or not message.strip():
return "Bhai, kuch message toh likho!"
# Build conversation prompt
prompt = ""
for item in history or []:
if not isinstance(item, dict):
continue
role = item.get("role")
content = item.get("content", "")
# Ignore non-text messages
if not isinstance(content, str):
continue
if role == "user":
prompt += f"<|user|>{content}<|assistant|>"
elif role == "assistant":
prompt += f"{content}<|endoftext|>"
prompt += f"<|user|>{message.strip()}<|assistant|>"
# Tokenize input
inputs = tokenizer(
prompt,
return_tensors="pt",
add_special_tokens=False,
)
# Generate response on the GPU allocated by ZeroGPU
with torch.no_grad():
output = model.generate(
**inputs,
max_new_tokens=120,
do_sample=True,
temperature=0.8,
top_p=0.9,
repetition_penalty=1.3,
eos_token_id=0,
)
# Decode only newly generated tokens
input_length = inputs["input_ids"].shape[1]
new_tokens = output[0][input_length:]
answer = tokenizer.decode(
new_tokens,
skip_special_tokens=True,
).strip()
return answer or "Bhai, ek baar phir try karo."
# --------------------------------------------------
# Gradio interface
# --------------------------------------------------
demo = gr.ChatInterface(
fn=respond,
title="⚡ Chad AI",
description="A Hinglish conversational AI chatbot",
textbox=gr.Textbox(
placeholder="Bol bhai, kya scene hai?",
container=False,
),
)
if __name__ == "__main__":
demo.launch()
|