Spaces:
Sleeping
Sleeping
File size: 13,147 Bytes
9e7e648 6482d6b 9e7e648 6482d6b 9e7e648 84b2d51 9e7e648 f6b9677 9e7e648 84b2d51 9e7e648 84b2d51 9e7e648 f6b9677 9e7e648 f6b9677 9e7e648 f6b9677 9e7e648 84b2d51 9e7e648 84b2d51 f6b9677 84b2d51 9e7e648 84b2d51 9e7e648 84b2d51 9e7e648 f6b9677 9e7e648 7592cae 9e7e648 84b2d51 9e7e648 84b2d51 9e7e648 84b2d51 9e7e648 6482d6b 9e7e648 6482d6b 9e7e648 f6b9677 9e7e648 f6b9677 9e7e648 f6b9677 9e7e648 f6b9677 9e7e648 f6b9677 9e7e648 f6b9677 9e7e648 f6b9677 9e7e648 f6b9677 9e7e648 f6b9677 9e7e648 f6b9677 9e7e648 f6b9677 9e7e648 f6b9677 9e7e648 f6b9677 9e7e648 f6b9677 9e7e648 f6b9677 9e7e648 f6b9677 9e7e648 f6b9677 9e7e648 | 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | import torch
import gradio as gr
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
from threading import Thread
from fastapi import FastAPI
from fastapi.responses import StreamingResponse, RedirectResponse
from pydantic import BaseModel
import json
from typing import List, Literal, Optional
import os
import uuid
import time
# Use more CPU threads for faster inference
torch.set_num_threads(4)
HF_TOKEN = os.getenv("HF_TOKEN")
MODEL = "meta-llama/Llama-3.2-1B-Instruct"
app = FastAPI()
# base model for finetuned (LoRA) inference
finetuned_base = AutoModelForCausalLM.from_pretrained(
MODEL,
token=HF_TOKEN,
dtype=torch.bfloat16, # faster than float32, matches GPU training
device_map="cpu",
low_cpu_mem_usage=True,
attn_implementation="sdpa", # PyTorch optimized attention
)
finetuned_base.config.use_cache = True
# separate base model for comparison (no LoRA)
base_model = AutoModelForCausalLM.from_pretrained(
MODEL,
token=HF_TOKEN,
dtype=torch.bfloat16,
device_map="cpu",
low_cpu_mem_usage=True,
attn_implementation="sdpa",
)
base_model.config.use_cache = True
base_model.eval()
tokenizer = AutoTokenizer.from_pretrained(MODEL, token=HF_TOKEN)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
# lora adapters
adapter_paths = {
"English": "./models/English",
"Spanish": "./models/Spanish",
"Korean": "./models/Korean"
}
languages = list(adapter_paths.keys())
# Create PeftModel with first adapter
peft_model = PeftModel.from_pretrained(
finetuned_base,
adapter_paths[languages[0]],
adapter_name=languages[0],
is_trainable=False
)
# Load remaining adapters
for lang in languages[1:]:
peft_model.load_adapter(adapter_paths[lang], adapter_name=lang)
peft_model.eval()
print("All adapters ready.")
# base model generation (no LoRA)
def generate_base_model_stream(messages, max_length=256, temperature=0.7):
print(f"Base model (no LoRA)")
print(f"Messages: {messages}")
inputs = tokenizer.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_tensors="pt",
return_dict=True
).to(base_model.device)
streamer = TextIteratorStreamer(
tokenizer,
skip_prompt=True,
skip_special_tokens=True)
generation_kwargs = {
**inputs,
"max_new_tokens": max_length,
"temperature": temperature,
"do_sample": True,
"pad_token_id": tokenizer.eos_token_id,
"streamer": streamer,
"num_beams": 1,
"use_cache": True,
}
thread = Thread(target=base_model.generate, kwargs=generation_kwargs)
thread.start()
for text in streamer:
yield text
thread.join()
# the input will be a list of messages that include system, user, and assistant prompts
def generate_text_stream(messages, language, max_length=256, temperature=0.7):
print(f"Language adapter: {language}")
print(f"Messages: {messages}")
if language not in adapter_paths:
yield f"Error: Language '{language}' not supported. Choose from: {list(adapter_paths.keys())}"
return
peft_model.set_adapter(language)
inputs = tokenizer.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True, # provides assistant: so that it can start generating
return_tensors="pt",
return_dict=True
).to(peft_model.device)
streamer = TextIteratorStreamer(
tokenizer,
skip_prompt=True,
skip_special_tokens=True)
generation_kwargs = {
**inputs, # the key-value pairs in inputs are applied to this new dictinary
"max_new_tokens": max_length,
"temperature": temperature,
"do_sample": True, # to stop greedy selection
"pad_token_id": tokenizer.eos_token_id,
"streamer": streamer,
"num_beams": 1, # keep only 1 sequence till the end
"use_cache": True, #KV caching
}
thread = Thread(target=peft_model.generate, kwargs=generation_kwargs)
thread.start()
for text in streamer:
yield text
thread.join()
# using pydantic to ensure data schemas
class Message(BaseModel):
role: Literal["system", "user", "assistant"]
content: str
class GenerateRequest(BaseModel):
messages: List[Message]
language: str
max_length: int = 256
temperature: float = 0.7
# OpenAI-compatible request format for InferenceClient
class ChatCompletionRequest(BaseModel):
model: str = "default"
messages: List[Message]
max_tokens: Optional[int] = 256
temperature: Optional[float] = 0.7
stream: Optional[bool] = True
# fastAPI endpoints
# return information about the API
@app.get("/api")
def read_api():
return {
"message": "Multi-language Chatbot API",
"languages": list(adapter_paths.keys()),
"device": "CPU 16GB in Huggingface Space",
"endpoints": {
"POST /api/generate": "Generate with streaming",
"GET /api/languages": "List available languages"
}
}
# return information about the langauge of the model
@app.get("/api/languages")
def get_languages():
return {
"languages": list(adapter_paths.keys()),
}
# providing a response through a stream
@app.post("/api/generate")
async def generate_stream_api(request: GenerateRequest):
# because pydantic uses Message class
# this needs to be converted again to plain dictionary
messages_dicts = [{"role": msg.role, "content": msg.content} for msg in request.messages]
def event_generator():
try:
for token in generate_text_stream(
messages_dicts,
request.language,
request.max_length,
request.temperature
):
yield f"data: {json.dumps({'token': token})}\n\n"
yield f"data: [DONE]\n\n"
except Exception as e:
yield f"data: {json.dumps({'error': str(e)})}\n\n"
# SSE is implemeted
return StreamingResponse(
event_generator(),
media_type="text/event-stream", # SSE content type
headers={
"Cache-Control": "no-cache", # Don't cache streaming responses
"Connection": "keep-alive", # Keep connection open
"X-Accel-Buffering": "no",
}
)
# OpenAI-compatible endpoint for HuggingFace InferenceClient
# Pass language via the `model` field (e.g., "English", "Spanish", "Korean")
@app.post("/v1/chat/completions")
async def chat_completions(request: ChatCompletionRequest):
messages_dicts = [{"role": msg.role, "content": msg.content} for msg in request.messages]
# Use model field as language selector, default to English if invalid
language = request.model if request.model in adapter_paths else "English"
chat_id = f"chatcmpl-{uuid.uuid4().hex[:8]}"
created = int(time.time())
def event_generator():
try:
for token in generate_text_stream(
messages_dicts,
language,
request.max_tokens or 256,
request.temperature or 0.7
):
chunk = {
"id": chat_id,
"object": "chat.completion.chunk",
"created": created,
"model": language,
"choices": [{
"index": 0,
"delta": {"content": token},
"finish_reason": None
}]
}
yield f"data: {json.dumps(chunk)}\n\n"
# Final chunk with finish_reason
final_chunk = {
"id": chat_id,
"object": "chat.completion.chunk",
"created": created,
"model": language,
"choices": [{
"index": 0,
"delta": {},
"finish_reason": "stop"
}]
}
yield f"data: {json.dumps(final_chunk)}\n\n"
yield "data: [DONE]\n\n"
except Exception as e:
error_chunk = {"error": {"message": str(e), "type": "server_error"}}
yield f"data: {json.dumps(error_chunk)}\n\n"
return StreamingResponse(
event_generator(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"X-Accel-Buffering": "no",
}
)
def chat_base_model(message, history, system_prompt, max_length, temperature):
messages = []
if system_prompt:
messages.append({"role": "system", "content": system_prompt})
messages.extend(history[-10:])
user_msg = {"role": "user", "content": message}
messages.append(user_msg)
assistant_msg = {"role": "assistant", "content": ""}
for token in generate_base_model_stream(
messages,
max_length,
temperature
):
assistant_msg["content"] += token
yield history + [user_msg, assistant_msg]
def chat_finetuned(message, history, language, system_prompt, max_length, temperature):
messages = []
if system_prompt:
messages.append({"role": "system", "content": system_prompt})
messages.extend(history[-10:])
user_msg = {"role": "user", "content": message}
messages.append(user_msg)
assistant_msg = {"role": "assistant", "content": ""}
for token in generate_text_stream(
messages,
language,
max_length,
temperature
):
assistant_msg["content"] += token
yield history + [user_msg, assistant_msg]
with gr.Blocks(
title="Language Learning Chatbot",
theme=gr.themes.Soft()
) as demo:
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### Base Model (No LoRA)")
chatbot_base = gr.Chatbot(
label="Base Model",
height=400,
type="messages"
)
with gr.Column(scale=1):
gr.Markdown("### Finetuned Model (LoRA)")
chatbot_finetuned = gr.Chatbot(
label="Finetuned Model",
height=400,
type="messages"
)
with gr.Row():
msg = gr.Textbox(
label="Your message",
placeholder="Type your message here and press Enter...",
lines=2,
scale=4
)
with gr.Row():
submit_btn = gr.Button("Send", variant="primary", scale=1)
clear_btn = gr.Button("Clear Both Chats", scale=1)
with gr.Row():
with gr.Column():
gr.Markdown("### Settings")
language_dropdown = gr.Dropdown(
choices=list(adapter_paths.keys()),
label="Language (for Finetuned Model)",
value=list(adapter_paths.keys())[0],
info="Select the language adapter to use"
)
system_prompt_input = gr.Textbox(
label="System Prompt (Optional)",
placeholder="e.g., You are a helpful assistant...",
lines=3,
info="Shared between both models"
)
max_length_slider = gr.Slider(
minimum=50,
maximum=512,
value=256,
step=1,
label="Max Length (tokens)",
info="Maximum tokens to generate"
)
temperature_slider = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.7,
step=0.05,
label="Temperature",
info="Higher = more creative"
)
# handling enter key in textbox - send to both models
msg.submit(
fn=chat_base_model,
inputs=[msg, chatbot_base, system_prompt_input, max_length_slider, temperature_slider],
outputs=[chatbot_base],
)
msg.submit(
fn=chat_finetuned,
inputs=[msg, chatbot_finetuned, language_dropdown, system_prompt_input, max_length_slider, temperature_slider],
outputs=[chatbot_finetuned],
).then(
fn=lambda: gr.update(value=""),
outputs=[msg]
)
# Handle button click - send to both models
submit_btn.click(
fn=chat_base_model,
inputs=[msg, chatbot_base, system_prompt_input, max_length_slider, temperature_slider],
outputs=[chatbot_base],
)
submit_btn.click(
fn=chat_finetuned,
inputs=[msg, chatbot_finetuned, language_dropdown, system_prompt_input, max_length_slider, temperature_slider],
outputs=[chatbot_finetuned],
).then(
fn=lambda: gr.update(value=""),
outputs=[msg]
)
# Clear both chats
clear_btn.click(
fn=lambda: (None, None),
outputs=[chatbot_base, chatbot_finetuned],
queue=False
)
demo.queue(False)
app = gr.mount_gradio_app(app, demo, path="/") |