Download app.py from jezv/cmd: direct link, hf CLI and curl.
- Browser
- Download file 1.22 kB
-
https://huggingface.co/spaces/jezv/cmd/resolve/main/app.py
- Command line
-
hf download hf://spaces/jezv/cmd/app.py
-
curl -L -o app.py https://huggingface.co/spaces/jezv/cmd/resolve/main/app.py
1.22 kB
| import gradio as gr | |
| from transformers import ( | |
| AutoModelForCausalLM, | |
| AutoTokenizer, | |
| pipeline, | |
| logging, | |
| ) | |
| from peft import PeftModel | |
| # Reload model in FP16 and merge it with LoRA weights | |
| model = AutoModelForCausalLM.from_pretrained( | |
| 'TinyLlama/TinyLlama-1.1B-Chat-v1.0' | |
| ) | |
| model = PeftModel.from_pretrained(model, 'jezv/TinyLlama-1.1b-cmd') | |
| model = model.merge_and_unload() | |
| # Reload tokenizer to save it | |
| tokenizer = AutoTokenizer.from_pretrained('TinyLlama/TinyLlama-1.1B-Chat-v1.0', trust_remote_code=True) | |
| tokenizer.pad_token = tokenizer.eos_token | |
| tokenizer.padding_side = "right" | |
| def greet(prompt): | |
| # Try question-answering | |
| pipe = pipeline(task="text-generation", model=model, tokenizer=tokenizer, max_new_tokens=20) | |
| messages = [ | |
| { | |
| "role": "system", | |
| "content": "You are a chatbot assistant who helps with writing console commands.", | |
| }, | |
| {"role": "user", "content": prompt}, | |
| ] | |
| text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) | |
| result = pipe(text) | |
| return result[0]['generated_text'] | |
| demo = gr.Interface( | |
| fn=greet, | |
| inputs=["text"], | |
| outputs=["text"], | |
| ) | |
| demo.launch() |