krisha06 commited on
Commit
a8f1aa1
Β·
verified Β·
1 Parent(s): fdddd8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -45
app.py CHANGED
@@ -1,60 +1,46 @@
 
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 (on CPU)
7
- base_model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
8
- adapter_path = "lora_adapter"
9
 
10
- device = torch.device("cpu")
 
11
 
12
- tokenizer = AutoTokenizer.from_pretrained(base_model_name, use_fast=True)
13
- base_model = AutoModelForCausalLM.from_pretrained(base_model_name).to(device)
14
- model = PeftModel.from_pretrained(base_model, adapter_path).to(device)
15
-
16
- # Streamlit UI setup
17
- st.set_page_config(page_title="Python Tutor Chatbot", page_icon="🐍", layout="centered")
18
- st.title("🐍 Python Tutor Chatbot")
19
- st.markdown("Ask me anything about Python programming!")
20
 
21
- # πŸ” Prompt template with instruction to ignore unrelated queries
22
- def create_prompt(user_input):
23
- return f"""You are a helpful and expert AI Python tutor.
24
-
25
- Your job is to only answer questions strictly related to Python programming (syntax, concepts, libraries, frameworks, tools, errors, etc.).
26
-
27
- If the question is unrelated to Python, politely respond:
28
- "Sorry, I can only answer Python programming questions."
29
 
30
  ### Instruction:
31
- {user_input}
32
 
33
  ### Response:
34
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- # πŸ”Ž User Input
37
- user_input = st.text_input("Your Python Question:")
38
 
39
- # πŸ”„ Inference
40
  if user_input:
41
  with st.spinner("Generating response..."):
42
- prompt = create_prompt(user_input)
43
- inputs = tokenizer(prompt, return_tensors="pt").to(device)
44
-
45
- with torch.no_grad():
46
- outputs = model.generate(
47
- **inputs,
48
- max_new_tokens=200,
49
- temperature=0.7,
50
- top_p=0.9,
51
- top_k=50,
52
- do_sample=True,
53
- pad_token_id=tokenizer.eos_token_id
54
- )
55
-
56
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
57
- final_response = response.split("### Response:")[-1].strip()
58
-
59
  st.markdown("**Answer:**")
60
- st.write(final_response)
 
1
+ import streamlit as st
2
  import torch
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
 
 
 
 
 
 
4
 
5
+ # Load model and tokenizer
6
+ model_name = "path/to/your/lora_adapter_or_model" # Update this to your LoRA model path
7
 
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
 
 
 
 
 
 
10
 
11
+ # Chat function with prompt-based filtering
12
+ def chat(instruction):
13
+ prompt = f"""You are a helpful and expert Python programming tutor.
14
+ If the question is about Python, explain clearly with examples.
15
+ If the question is unrelated to Python, respond with "Sorry, I can only answer Python-related questions."
 
 
 
16
 
17
  ### Instruction:
18
+ {instruction}
19
 
20
  ### Response:
21
  """
22
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
23
+ with torch.no_grad():
24
+ outputs = model.generate(
25
+ **inputs,
26
+ max_new_tokens=150,
27
+ temperature=0.7,
28
+ top_p=0.95,
29
+ do_sample=True,
30
+ pad_token_id=tokenizer.eos_token_id
31
+ )
32
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
33
+ return response.split("### Response:")[-1].strip()
34
+
35
+ # Streamlit UI
36
+ st.set_page_config(page_title="Python Tutor Chatbot", page_icon="🐍")
37
+ st.title("🐍 Python Tutor Chatbot")
38
+ st.write("Ask me Python programming questions!")
39
 
40
+ user_input = st.text_input("Your question:")
 
41
 
 
42
  if user_input:
43
  with st.spinner("Generating response..."):
44
+ response = chat(user_input)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  st.markdown("**Answer:**")
46
+ st.markdown(response)