Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141551 unmodified lines676869707172736 unmodified lines808182838485868788899091929322 unmodified lines11611711811912012112212312412512612712812913013113213313413513613713813914014114214314414515 unmodified lines161162163164"""Estella Explainer — Hugging Face ZeroGPU SpaceA math reading coach for grades 3–8."""import osimport spacesimport gradio as grimport torchfrom peft import PeftModelfrom transformers import AutoModelForCausalLM, AutoTokenizerADAPTER_ID = "sgattup/EstellaExplainerLLM"51 unmodified lines)print("Moving combined model to CUDA.")model = model.to("cuda")model.eval()6 unmodified lines@spaces.GPU(duration=60)def ask_estella(message: str, history: list) -> str:"""Return one reading hint without solving the math problem."""if not message or not message.strip():return ("Please type a math word problem and I’ll help explain ""what it is asking!")messages = [{22 unmodified lines)input_device = next(model.parameters()).deviceinputs = {key: value.to(input_device)for key, value in inputs.items()}input_length = inputs["input_ids"].shape[1]with torch.no_grad():outputs = model.generate(**inputs,max_new_tokens=150,temperature=0.7,do_sample=True,repetition_penalty=1.1,pad_token_id=tokenizer.eos_token_id,)generated_tokens = outputs[0][input_length:]return tokenizer.decode(generated_tokens,skip_special_tokens=True,).strip()demo = gr.ChatInterface(15 unmodified linesif __name__ == "__main__":demo.launch()123456789101112131415161718192021222351 unmodified lines7576777879806 unmodified lines8788899091929394959697989910010122 unmodified lines12412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016115 unmodified lines177178179180"""Estella Explainer — streaming Gradio chat interfaceThis version keeps Gradio's default appearance and adds the two importantinteraction patterns used by the DeepSeek demo: token streaming and a queue."""import osfrom threading import Threadfrom typing import Iteratorimport gradio as grimport spacesimport torchfrom peft import PeftModelfrom transformers import (AutoModelForCausalLM,AutoTokenizer,TextIteratorStreamer,)ADAPTER_ID = "sgattup/EstellaExplainerLLM"51 unmodified lines)print("Moving combined model to CUDA.")model = model.to("cuda")model.eval()6 unmodified lines@spaces.GPU(duration=60)def ask_estella(message: str, history: list) -> Iterator[str]:"""Stream one reading hint without solving the math problem."""if not message or not message.strip():yield ("Please type a math word problem and I’ll help explain ""what it is asking!")returnmessages = [{22 unmodified lines)input_device = next(model.parameters()).deviceinputs = {key: value.to(input_device)for key, value in inputs.items()}streamer = TextIteratorStreamer(tokenizer,timeout=60.0,skip_prompt=True,skip_special_tokens=True,)generation_kwargs = {**inputs,"streamer": streamer,"max_new_tokens": 150,"temperature": 0.7,"do_sample": True,"repetition_penalty": 1.1,"pad_token_id": tokenizer.eos_token_id,}generation_thread = Thread(target=model.generate,kwargs=generation_kwargs,)generation_thread.start()partial_hint = ""for new_text in streamer:partial_hint += new_textyield partial_hint.strip()demo = gr.ChatInterface(15 unmodified linesif __name__ == "__main__":demo.queue(max_size=20).launch()
app.py+41-25