krisha06 commited on
Commit
5baf039
·
verified ·
1 Parent(s): e299194

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -31
app.py CHANGED
@@ -1,43 +1,61 @@
1
  import torch
 
2
  from peft import PeftModel
3
- from transformers import AutoModelForCausalLM, AutoTokenizer
4
- from transformers import pipeline
5
  import streamlit as st
6
- import os
7
 
8
- # Load tokenizer
9
- tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
 
10
 
11
- # Load base model
12
- base_model = AutoModelForCausalLM.from_pretrained(
13
- "TinyLLaMA/TinyLLaMA-1.1B-Chat-v1.0", # or your exact model name
14
- device_map="auto"
15
- )
 
16
 
17
- # Load LoRA adapter
18
- model = PeftModel.from_pretrained(
19
- base_model,
20
- "lora_adapter", # folder name
21
- device_map="auto"
22
  )
23
 
24
- # Load pipeline
25
- pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
 
 
 
 
 
 
 
 
 
26
 
27
- # Streamlit UI
28
- st.title("🧠 TinyLLaMA Python Tutor (LoRA)")
29
- st.write("Ask me any Python programming question:")
30
 
31
- user_input = st.text_input("Your question")
 
32
 
33
  if user_input:
34
- if "python" in user_input.lower() or "list" in user_input.lower() or "tuple" in user_input.lower() or "def " in user_input.lower() or "class" in user_input.lower():
35
- prompt = f"""You are a helpful and friendly Python tutor. Only answer Python programming questions. Be clear and concise.
36
-
37
- Question: {user_input}
38
- Answer:"""
39
- response = pipe(prompt, max_new_tokens=256, temperature=0.7, do_sample=True)[0]["generated_text"]
40
- answer = response.split("Answer:")[-1].strip()
41
- st.markdown(f"💬 **Answer:**\n\n{answer}")
42
- else:
43
- st.warning("❌ Sorry, I can only answer Python programming questions.")
 
 
 
 
 
 
 
 
 
 
1
  import torch
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
  from peft import PeftModel
 
 
4
  import streamlit as st
 
5
 
6
+ # Load tokenizer and model (CPU)
7
+ base_model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
8
+ adapter_path = "lora_adapter" # Your LoRA adapter folder path
9
 
10
+ # Force CPU usage
11
+ device = torch.device("cpu")
12
+
13
+ tokenizer = AutoTokenizer.from_pretrained(base_model_name, use_fast=True)
14
+ base_model = AutoModelForCausalLM.from_pretrained(base_model_name).to(device)
15
+ model = PeftModel.from_pretrained(base_model, adapter_path).to(device)
16
 
17
+ # Streamlit UI setup
18
+ st.set_page_config(
19
+ page_title="Python Tutor Chatbot",
20
+ page_icon="🐍",
21
+ layout="centered"
22
  )
23
 
24
+ st.title("🐍 Python Tutor Chatbot")
25
+ st.markdown("Ask me anything about Python programming!")
26
+
27
+ # Prompt template
28
+ def create_prompt(user_input):
29
+ return f"""
30
+ You are a helpful and knowledgeable AI Python Tutor. Your job is to answer only Python-related programming questions.
31
+ If the question is unrelated to Python, kindly respond with: "Sorry, I can only answer Python programming questions."
32
+
33
+ ### Instruction:
34
+ {user_input}
35
 
36
+ ### Response:
37
+ """
 
38
 
39
+ # Chat interface
40
+ user_input = st.text_input("Your Python Question:")
41
 
42
  if user_input:
43
+ with st.spinner("Generating response..."):
44
+ prompt = create_prompt(user_input)
45
+ inputs = tokenizer(prompt, return_tensors="pt").to(device)
46
+
47
+ with torch.no_grad():
48
+ output = model.generate(
49
+ **inputs,
50
+ max_new_tokens=200,
51
+ temperature=0.7,
52
+ do_sample=True,
53
+ top_p=0.9,
54
+ top_k=50
55
+ )
56
+
57
+ response = tokenizer.decode(output[0], skip_special_tokens=True)
58
+ final_response = response.split("### Response:")[-1].strip()
59
+
60
+ st.markdown("**Answer:**")
61
+ st.write(final_response)