import torch from transformers import AutoTokenizer, AutoModelForCausalLM import gradio as gr title = """Welcome to Tonic's Lite Llama On-Device Chat!""" description = """ You can use this Space to test out the current model [ahxt/LiteLlama-460M-1T](https://huggingface.co/ahxt/LiteLlama-460M-1T) You can also use Lite Llama On-Device Chat by cloning this space. Simply click here: Duplicate Space Join us : TeamTonic is always making cool demos! Join our active builder's community on Discord: [Discord](https://discord.gg/nXx5wbX9) On Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) On Github: [Polytonic](https://github.com/tonic-ai) & contribute to [PolyGPT](https://github.com/tonic-ai/polygpt-alpha) """ model_path = 'ahxt/LiteLlama-460M-1T' model = AutoModelForCausalLM.from_pretrained(model_path) tokenizer = AutoTokenizer.from_pretrained(model_path) model.eval() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.to(device) def generate_text(prompt): input_ids = tokenizer(prompt, return_tensors="pt").input_ids tokens = model.generate(input_ids, max_length=20) return tokenizer.decode(tokens[0].tolist(), skip_special_tokens=True) iface = gr.Interface( fn=generate_text, inputs=gr.inputs.Textbox(lines=2, placeholder="Enter your prompt here..."), outputs="text", title=title, description=description ) iface.launch()