Grandediw commited on
Commit
724692c
·
1 Parent(s): c757a2d
Files changed (2) hide show
  1. app.py +18 -8
  2. requirements.txt +4 -3
app.py CHANGED
@@ -1,14 +1,24 @@
1
  import streamlit as st
2
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
 
3
 
4
  st.set_page_config(page_title="Hugging Face Chatbot", layout="centered")
5
- st.title("Hugging Face Chatbot")
6
 
7
  @st.cache_resource
8
  def load_model():
9
- # Load tokenizer and model from Hugging Face
10
- tokenizer = AutoTokenizer.from_pretrained("Grandediw/lora_model_finetuned", use_fast=True)
11
- model = AutoModelForCausalLM.from_pretrained("Grandediw/lora_model_finetuned", device_map="auto", trust_remote_code=True)
 
 
 
 
 
 
 
 
 
12
  chat_pipeline = pipeline(
13
  "text-generation",
14
  model=model,
@@ -33,19 +43,19 @@ for message in st.session_state.messages:
33
 
34
  # User input
35
  if prompt := st.chat_input("Ask me anything:"):
36
- # Display user message and store it
37
  st.chat_message("user").markdown(prompt)
38
  st.session_state.messages.append({"role": "user", "content": prompt})
39
 
40
  # Generate response
41
  with st.spinner("Thinking..."):
 
42
  response = chat_pipeline(prompt)[0]["generated_text"]
43
- # The model might return the prompt + response together.
44
- # If that's the case, remove the prompt from the start.
45
  if response.startswith(prompt):
46
  response = response[len(prompt):].strip()
47
 
48
- # Display and store assistant response
49
  with st.chat_message("assistant"):
50
  st.markdown(response)
51
  st.session_state.messages.append({"role": "assistant", "content": response})
 
1
  import streamlit as st
2
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
+ from peft import PeftModel
4
 
5
  st.set_page_config(page_title="Hugging Face Chatbot", layout="centered")
6
+ st.title("Hugging Face Chatbot with LoRA")
7
 
8
  @st.cache_resource
9
  def load_model():
10
+ # Replace this with the actual base model used during LoRA fine-tuning
11
+ base_model_name = "unsloth/Llama-3.2-1B-Instruct"
12
+
13
+ # Load the base model and tokenizer
14
+ tokenizer = AutoTokenizer.from_pretrained(base_model_name, use_fast=False)
15
+ base_model = AutoModelForCausalLM.from_pretrained(base_model_name, device_map="auto", trust_remote_code=True)
16
+
17
+ # Load the LoRA adapter weights
18
+ # Replace "Grandediw/lora_model_finetuned" with your actual LoRA model repo
19
+ model = PeftModel.from_pretrained(base_model, "Grandediw/lora_model_finetuned")
20
+
21
+ # Create a pipeline for text generation
22
  chat_pipeline = pipeline(
23
  "text-generation",
24
  model=model,
 
43
 
44
  # User input
45
  if prompt := st.chat_input("Ask me anything:"):
46
+ # Display user message
47
  st.chat_message("user").markdown(prompt)
48
  st.session_state.messages.append({"role": "user", "content": prompt})
49
 
50
  # Generate response
51
  with st.spinner("Thinking..."):
52
+ # Generate text with the pipeline
53
  response = chat_pipeline(prompt)[0]["generated_text"]
54
+ # Remove the prompt from the start if it's included
 
55
  if response.startswith(prompt):
56
  response = response[len(prompt):].strip()
57
 
58
+ # Display assistant response
59
  with st.chat_message("assistant"):
60
  st.markdown(response)
61
  st.session_state.messages.append({"role": "assistant", "content": response})
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
- streamlit
2
- transformers
3
- torch
 
 
1
+ streamlit==1.25.0
2
+ transformers==4.34.0
3
+ torch==2.0.1
4
+ peft==0.7.0