eromanova115 commited on
Commit
a8df421
·
verified ·
1 Parent(s): 5e1e950

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ from peft import PeftModel
4
+ import torch
5
+ import os
6
+
7
+ token = "" # hugging face token
8
+ @st.cache_resource
9
+ def load_model(base_model_path) :
10
+ """
11
+ Load the base model and apply the adapter.
12
+ """
13
+
14
+ print('START OF THE APP')
15
+ # Load the base model and tokenizer
16
+ token = ''
17
+ tokenizer = AutoTokenizer.from_pretrained('meta-llama/Llama-3.2-3B-Instruct', token=token) # meta-llama/Llama-3.2-1B
18
+ base_model = AutoModelForCausalLM.from_pretrained('meta-llama/Llama-3.2-3B-Instruct', token=token,device_map="auto", low_cpu_mem_usage=True,trust_remote_code=True,torch_dtype=torch.float16)
19
+ print('Loaded the BASE MODEL AND TOKENIZER ')
20
+ print(f"Base Model Path: {base_model_path}")
21
+ print(f"Adapter Path: {adapter_path}")
22
+ # Load the adapter
23
+ model = PeftModel.from_pretrained(base_model,'eromanova115/CyberSecurityAIAssistant',token=token)
24
+ # adapter_config_path = os.path.dirname('CyberSecurityAssistant/adapter_config.json')
25
+ # print(f"Adapter Config Path: {adapter_config_path}")
26
+ # print('type of adapter config path ',type(adapter_config_path))
27
+ # model = PeftModel.from_pretrained(
28
+ # base_model,
29
+ # adapter_path,
30
+ # config=adapter_config_path,
31
+ # torch_dtype='auto'
32
+ # )
33
+ # model = PeftModel.from_pretrained(base_model,adapter_path)
34
+ model = model.merge_and_unload()
35
+ print('Model is merged successful')
36
+ return model, tokenizer
37
+
38
+
39
+ # Streamlit UI
40
+ st.title("Cybersecurity AI ASSISTANT LLM Security")
41
+
42
+
43
+ # Sidebar inputs for model paths
44
+ base_model_path = st.sidebar.text_input("Base Model Path from HF", 'meta-llama/Llama-3.2-3B')
45
+ adapter_path = st.sidebar.text_input("Adapter Safetensors Path", 'CyberSecurityAssistant')
46
+ adapter_config_path = st.sidebar.text_input("Adapter Config Path", 'CyberSecurityAssistant/adapter_config.json') # CyberSecurityAssistant\adapter_config.json
47
+ print(f"{base_model_path=}")
48
+
49
+ # Temperature slider
50
+ temperature = st.sidebar.slider("Temperature", 0.0, 2.0, 0.7, step=0.1)
51
+
52
+ # Load the model
53
+ if base_model_path and adapter_path and adapter_config_path:
54
+ try:
55
+ with st.spinner("Loading model..."):
56
+ model, tokenizer = load_model(base_model_path)
57
+ st.sidebar.success("Model loaded successfully!")
58
+ except Exception as e:
59
+ st.sidebar.error(f"Error loading model: {e}")
60
+ model, tokenizer = None, None
61
+ else:
62
+ st.warning("Please provide paths to the model and adapter files in the sidebar.")
63
+
64
+
65
+ # SYSTEM PROMPT
66
+
67
+ # GLOBAL VARIABLE INSTRUCTION
68
+ instruction= 'You are a Cybersecurity AI Assistant, will be glad to answer your questions related to Cybersecurity, particularly LLM Security.'
69
+
70
+
71
+ # Chat Interface
72
+ if model and tokenizer:
73
+ user_input = st.text_input("Your message", "")
74
+ user_input= f'{instruction} \n\nUser: {user_input}\nAI'
75
+ if user_input:
76
+ with st.spinner("Generating response..."):
77
+ try:
78
+ # Tokenize input
79
+ input_ids = tokenizer.encode(user_input, return_tensors="pt").to(model.device)
80
+ # Generate response
81
+ outputs = model.generate(input_ids, max_new_tokens=512, temperature=temperature)
82
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
83
+ st.write(f"**Response:** {response}")
84
+ except Exception as e:
85
+ st.error(f"Error generating response: {e}")
86
+
87
+
88
+ # streamlit run app.py