MarioCerulo commited on
Commit
3cbf55f
·
verified ·
1 Parent(s): 5a775d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -1,22 +1,30 @@
1
  import os
 
 
2
  import streamlit as st
 
3
  from dotenv import load_dotenv
4
  from peft import PeftModel, PeftConfig
5
  from chromadb import HttpClient
6
  from utils.embedding_utils import CustomEmbeddingFunction
7
  from transformers import AutoModelForCausalLM, AutoTokenizer
8
 
9
- st.title("FormulAI Q&A")
 
 
10
 
 
 
11
  model_name = "unsloth/Llama-3.2-1B"
12
 
13
- model = AutoModelForCausalLM.from_pretrained(model_name)
 
14
  tokenizer = AutoTokenizer.from_pretrained(model_name)
15
 
 
16
  adapter_name = "FormulAI/FormuLLaMa-3.2-1B-LoRA"
17
  peft_config = PeftConfig.from_pretrained(adapter_name)
18
-
19
- model = PeftModel(model, peft_config)
20
 
21
  template = """Answer the following QUESTION based on the CONTEXT given.
22
  If you do not know the answer and the CONTEXT doesn't contain the answer truthfully say "I don't know".
@@ -37,7 +45,7 @@ if 'past' not in st.session_state:
37
  st.session_state['past'] = []
38
 
39
  def get_text():
40
- input_text = st.text_input("Ask your question: ", "", key="input")
41
  return input_text
42
 
43
  load_dotenv("chroma.env")
@@ -57,12 +65,11 @@ if question:
57
  context = " ".join(response['documents'][0])
58
 
59
  input_text = template.replace("{context}", context).replace("{question}", question)
60
- input_ids = tokenizer.encode(input_text, return_tensors="pt")
61
 
62
  output = model.generate(input_ids, max_new_tokens=200, early_stopping=True)
63
  answer = tokenizer.decode(output[0], skip_special_tokens=True).split("ANSWER:")[1]
64
 
65
-
66
  st.session_state.past.append(question)
67
  st.session_state.generated.append(answer)
68
 
 
1
  import os
2
+ import torch
3
+
4
  import streamlit as st
5
+
6
  from dotenv import load_dotenv
7
  from peft import PeftModel, PeftConfig
8
  from chromadb import HttpClient
9
  from utils.embedding_utils import CustomEmbeddingFunction
10
  from transformers import AutoModelForCausalLM, AutoTokenizer
11
 
12
+ st.title("FormulAI")
13
+ st.write("Benvenuto FormulaAI il Chatbot riguardante la Formula Uno! Chiedimi ciò che vuoi a riguardo!")
14
+ st.write("I am a chatbot that has been fine-tuned on the FormuLLaMa-3.2-1B dataset.")
15
 
16
+ # Device and model configuration
17
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
18
  model_name = "unsloth/Llama-3.2-1B"
19
 
20
+ # Load pretrained model and tokenizer
21
+ model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
22
  tokenizer = AutoTokenizer.from_pretrained(model_name)
23
 
24
+ # Load PEFT configuration and apply to model on device
25
  adapter_name = "FormulAI/FormuLLaMa-3.2-1B-LoRA"
26
  peft_config = PeftConfig.from_pretrained(adapter_name)
27
+ model = PeftModel(model, peft_config).to(device)
 
28
 
29
  template = """Answer the following QUESTION based on the CONTEXT given.
30
  If you do not know the answer and the CONTEXT doesn't contain the answer truthfully say "I don't know".
 
45
  st.session_state['past'] = []
46
 
47
  def get_text():
48
+ input_text = st.text_input("Chiedi qualcosa: ", "", key="input")
49
  return input_text
50
 
51
  load_dotenv("chroma.env")
 
65
  context = " ".join(response['documents'][0])
66
 
67
  input_text = template.replace("{context}", context).replace("{question}", question)
68
+ input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device)
69
 
70
  output = model.generate(input_ids, max_new_tokens=200, early_stopping=True)
71
  answer = tokenizer.decode(output[0], skip_special_tokens=True).split("ANSWER:")[1]
72
 
 
73
  st.session_state.past.append(question)
74
  st.session_state.generated.append(answer)
75