Atharva Prashant Pawar commited on
Commit
b95f6dc
1 Parent(s): aded65f
Files changed (3) hide show
  1. app.py +33 -2
  2. extra.txt +61 -0
  3. flaskapp.py +24 -0
app.py CHANGED
@@ -1,4 +1,35 @@
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
1
  import streamlit as st
2
+ import torch
3
+ from peft import PeftModel
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
+ import transformers
6
+
7
+ # Define the Streamlit app
8
+ st.title("Mistral Model Integration")
9
+
10
+ # Create a text input for the user to enter their prompt
11
+ instruction = st.text_area("Enter your prompt:")
12
+
13
+ # Function to interact with Mistral Model
14
+ def mistral_model(prompt, token_limit):
15
+ # Your model loading and inference code here (from the code you provided)
16
+ # ...
17
+
18
+ return responses
19
+
20
+ # Check if the user entered a prompt
21
+ if instruction:
22
+ # Add a slider for selecting the token limit
23
+ token_limit = st.slider("Select token limit", min_value=10, max_value=500, value=250)
24
+
25
+ # Create a button to trigger model inference
26
+ if st.button("Generate Response"):
27
+ responses = mistral_model(instruction, token_limit)
28
+ st.write("Generated Responses:")
29
+ for response in responses:
30
+ st.write(response)
31
+
32
+ # Finally, run the Streamlit app
33
+ if __name__ == "__main__":
34
+ st.run()
35
 
 
 
extra.txt ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ x = st.slider('Select a value')
4
+ st.write(x, 'squared is', x * x)
5
+
6
+
7
+ '''
8
+
9
+ !pip install git+https://github.com/huggingface/transformers
10
+ ! pip install -q peft accelerate bitsandbytes safetensors
11
+ import torch
12
+ from peft import PeftModel
13
+ from transformers import AutoModelForCausalLM, AutoTokenizer
14
+ import transformers
15
+ adapters_name = "atharvapawar/flaskCodemistral-7b-mj-finetuned"
16
+ # model_name = "bn22/Mistral-7B-Instruct-v0.1-sharded" #"mistralai/Mistral-7B-Instruct-v0.1"
17
+ model_name = "bn22/Mistral-7B-Instruct-v0.1-sharded"
18
+
19
+
20
+ device = "cuda" # the device to load the model onto
21
+ bnb_config = transformers.BitsAndBytesConfig(
22
+ load_in_4bit=True,
23
+ bnb_4bit_use_double_quant=True,
24
+ bnb_4bit_quant_type="nf4",
25
+ bnb_4bit_compute_dtype=torch.bfloat16
26
+ )
27
+ model = AutoModelForCausalLM.from_pretrained(
28
+ model_name,
29
+ load_in_4bit=True,
30
+ torch_dtype=torch.bfloat16,
31
+ quantization_config=bnb_config,
32
+ device_map='auto'
33
+ )
34
+ model = PeftModel.from_pretrained(model, adapters_name)
35
+ #model = model.merge_and_unload()
36
+
37
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
38
+ tokenizer.bos_token_id = 1
39
+ stop_token_ids = [0]
40
+ print(f"Successfully loaded the model {model_name} into memory")
41
+
42
+ def MistralModel(prompt, tokenLimit):
43
+ # text = "Identify the changes made to the given code, Common Weakness Enumeration (CWE) associated with the code, and the severity level of the CWE."
44
+ # "task": "Translate","source_language": "English","target_language": "French","text_to_translate": "Hello, how are you?"
45
+
46
+ text = "[INST]" + prompt + "[/INST]"
47
+
48
+ # text = "[INST] find code vulnerability [cwe] analysis of following code " + text + "[/INST]"
49
+
50
+ encoded = tokenizer(text, return_tensors="pt", add_special_tokens=False)
51
+ model_input = encoded
52
+ model.to(device)
53
+ generated_ids = model.generate(**model_input, max_new_tokens=tokenLimit, do_sample=True)
54
+ decoded = tokenizer.batch_decode(generated_ids)
55
+ # print(decoded[0])
56
+ return decoded[0]
57
+
58
+ responses = MistralModel(instruction, 250)
59
+ print(responses)
60
+
61
+ '''
flaskapp.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import torch
3
+ from peft import PeftModel
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
+ import transformers
6
+
7
+ app = Flask(__name)
8
+
9
+ @app.route('/api/generate_response', methods=['POST'])
10
+ def generate_response():
11
+ data = request.get_json()
12
+ prompt = data.get('prompt')
13
+ token_limit = data.get('token_limit')
14
+
15
+ # Your model loading and inference code here (from the code you provided)
16
+ # ...
17
+
18
+ responses = mistral_model(prompt, token_limit)
19
+
20
+ return jsonify({"responses": responses})
21
+
22
+ if __name__ == "__main__":
23
+ app.run()
24
+