Spaces:
Sleeping
Sleeping
Update app.py
Browse files19 unmodified lines2021222324252627282930313233343536373839404142434419 unmodified lines@spaces.GPU(duration=120)def chat(message, history):msgs = [{"role": h["role"], "content": to_text(h["content"])} for h in history]msgs.append({"role": "user", "content": to_text(message)})ids = tokenizer.apply_chat_template(msgs, add_generation_prompt=True, return_tensors="pt",return_dict=True, enable_thinking=False,).to("cuda")streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)Thread(target=model.generate, kwargs=dict(**ids, streamer=streamer, max_new_tokens=1024,do_sample=True, temperature=0.7, top_p=0.95,)).start()text = ""for tok in streamer:text += tokyield textgr.ChatInterface(chat, title="Private Coding Assistant").launch()19 unmodified lines2021222324252627282930313233343536373819 unmodified lines@spaces.GPU(duration=120)def generate(messages_json: str, max_tokens: int = 1024, temperature: float = 0.7) -> str:msgs = json.loads(messages_json)ids = tokenizer.apply_chat_template(msgs, add_generation_prompt=True, return_tensors="pt",return_dict=True, enable_thinking=False,).to("cuda")out = model.generate(**ids, max_new_tokens=int(max_tokens),do_sample=True, temperature=float(temperature), top_p=0.95)return tokenizer.decode(out[0][ids["input_ids"].shape[1]:], skip_special_tokens=True)with gr.Blocks() as demo:gr.ChatInterface(chat, title="Private Coding Assistant")gr.api(generate, api_name="generate")demo.launch()
app.py+9-15