abhijit1620/python-coding-assistant
0
1"""2Hugging Face Space app.3Loads the base model + your uploaded LoRA adapter from the Hub (not local disk)4so it works when deployed on Spaces.5 6IMPORTANT: Replace ADAPTER_REPO below with your own repo id7(e.g. "abhijeetsharma/qwen2.5-0.5b-python-assistant") after running upload_to_hub.py.8"""9 10import torch11import gradio as gr12import spaces13from transformers import AutoModelForCausalLM, AutoTokenizer14from peft import PeftModel15 16BASE_MODEL = "Qwen/Qwen2.5-0.5B-Instruct"17ADAPTER_REPO = "abhijit1620/qwen2.5-0.5b-python-assistant"18 19device = "cuda" if torch.cuda.is_available() else "cpu"20 21tokenizer = AutoTokenizer.from_pretrained(ADAPTER_REPO)22base_model = AutoModelForCausalLM.from_pretrained(BASE_MODEL, torch_dtype=torch.float32)23model = PeftModel.from_pretrained(base_model, ADAPTER_REPO)24model.eval()25 26 27@spaces.GPU28def generate(instruction: str, max_new_tokens: int = 256) -> str:29 model.to(device)30 prompt = f"### Instruction:\n{instruction}\n\n### Response:\n"31 inputs = tokenizer(prompt, return_tensors="pt").to(device)32 33 with torch.no_grad():34 output = model.generate(35 **inputs,36 max_new_tokens=max_new_tokens,37 temperature=0.7,38 do_sample=True,39 top_p=0.9,40 pad_token_id=tokenizer.eos_token_id,41 )42 43 text = tokenizer.decode(output[0], skip_special_tokens=True)44 return text.split("### Response:\n")[-1].strip()45 46 47demo = gr.Interface(48 fn=generate,49 inputs=gr.Textbox(label="Ask a Python coding question", lines=3,50 placeholder="e.g. Write a function to find duplicates in a list"),51 outputs=gr.Textbox(label="Response", lines=10),52 title="Fine-Tuned Python Coding Assistant (Qwen2.5-0.5B + LoRA)",53 description="A small language model fine-tuned with LoRA on Python instruction data. "54 "Built as a portfolio project — see the full training code on GitHub.",55)56 57if __name__ == "__main__":58 demo.launch()59 