Spaces:
Sleeping
Sleeping
| import torch | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| import streamlit as st | |
| # Load base + LoRA model | |
| base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" | |
| adapter_path = "./lora_adapter" # your uploaded LoRA adapter | |
| tokenizer = AutoTokenizer.from_pretrained(base_model) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| base_model, | |
| device_map="auto", | |
| torch_dtype=torch.float32 | |
| ) | |
| model.load_adapter(adapter_path) | |
| model.eval() | |
| # ---- Prompt template ---- | |
| def format_prompt(user_input): | |
| return f"""You are PythonGPT, an expert tutor that ONLY answers questions about Python programming. | |
| If the user asks anything unrelated to Python (like greetings, jokes, math problems, or general trivia), respond strictly with: | |
| "Sorry, I can only answer Python-related questions." | |
| Examples: | |
| Q: What is a function in Python? | |
| A: In Python, a function is a block of reusable code that performs a specific task... | |
| Q: Hello! | |
| A: Sorry, I can only answer Python-related questions. | |
| Q: What is numpy? | |
| A: NumPy is a library in Python used for numerical computations... | |
| Q: What's your name? | |
| A: Sorry, I can only answer Python-related questions. | |
| Q: {user_input} | |
| A:""" | |
| # ---- Chat generation ---- | |
| def get_response(user_input): | |
| prompt = format_prompt(user_input) | |
| inputs = tokenizer(prompt, return_tensors="pt").to(model.device) | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=200, | |
| do_sample=True, | |
| temperature=0.7, | |
| pad_token_id=tokenizer.eos_token_id | |
| ) | |
| response = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| return response.split("A:")[-1].strip() | |
| # ---- Streamlit App ---- | |
| st.set_page_config(page_title="π§βπ« Python Tutor Chatbot") | |
| st.title("π§βπ« Python Tutor Chatbot") | |
| st.write("Ask me anything about Python programming!") | |
| user_query = st.text_input("Your Question", "") | |
| if user_query: | |
| with st.spinner("Thinking..."): | |
| response = get_response(user_query) | |
| st.markdown(f"π‘ **Answer:**\n\n{response}") | |