Spaces:
Sleeping
Sleeping
Download app.py from Chuxia-sys/lora_peft: direct link, hf CLI and curl.
- Browser
- Download file 1.19 kB
-
https://huggingface.co/spaces/Chuxia-sys/lora_peft/resolve/main/app.py
- Command line
-
hf download hf://spaces/Chuxia-sys/lora_peft/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Chuxia-sys/lora_peft/resolve/main/app.py
1.19 kB
| from transformers import AutoTokenizer, AutoModelForSeq2SeqLM | |
| from peft import PeftModel | |
| import torch | |
| import gradio as gr | |
| # Base model | |
| base_model_name = "t5-small" # must match training | |
| model_base = AutoModelForSeq2SeqLM.from_pretrained(base_model_name) | |
| # Load LoRA adapter | |
| peft_model = PeftModel.from_pretrained(model_base, "Chuxia-sys/travel-planner-peft") | |
| peft_model.eval() # set to eval mode | |
| # Tokenizer | |
| tokenizer = AutoTokenizer.from_pretrained("Chuxia-sys/travel-planner-peft") | |
| # Generate function | |
| def generate_answer(question): | |
| input_ids = tokenizer( | |
| f"Answer the following question: {question} Answer:", | |
| return_tensors="pt" | |
| ).input_ids | |
| with torch.no_grad(): | |
| # Use the base model with LoRA applied | |
| output_ids = peft_model.base_model.generate(input_ids, max_new_tokens=150) | |
| return tokenizer.decode(output_ids[0], skip_special_tokens=True) | |
| # Gradio interface | |
| iface = gr.Interface( | |
| fn=generate_answer, | |
| inputs=gr.Textbox(lines=2, placeholder="Enter your question here..."), | |
| outputs="text", | |
| title="Travel Planner Bot", | |
| description="Ask for travel suggestions!" | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |