Download app.py from fubrix/test2: direct link, hf CLI and curl.
- Browser
- Download file 848 Bytes
-
https://huggingface.co/spaces/fubrix/test2/resolve/main/app.py
- Command line
-
hf download hf://spaces/fubrix/test2/app.py
-
curl -L -o app.py https://huggingface.co/spaces/fubrix/test2/resolve/main/app.py
848 Bytes
| import gradio as gr | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| checkpoint = "HuggingFaceTB/SmolLM2-135M-Instruct" | |
| device = "cpu" # "cuda" or "cpu" | |
| tokenizer = AutoTokenizer.from_pretrained(checkpoint) | |
| model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device) | |
| def predict(message, history): | |
| messages = history + [{"role": "user", "content": message}] | |
| input_text = tokenizer.apply_chat_template(messages, tokenize=False) | |
| inputs = tokenizer.encode(input_text, return_tensors="pt").to(device) | |
| outputs = model.generate(inputs, max_new_tokens=100, temperature=0.2, top_p=0.9, do_sample=True) | |
| decoded = tokenizer.decode(outputs[0]) | |
| response = decoded.split("<|im_start|>assistant\n")[-1].split("<|im_end|>")[0] | |
| return response | |
| demo = gr.ChatInterface(predict, api_name="chat") | |
| demo.launch() |