My model inference
Browse files- app.py +27 -20
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -1,10 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
""
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
def respond(
|
|
@@ -15,27 +32,17 @@ def respond(
|
|
| 15 |
|
| 16 |
for val in history:
|
| 17 |
if val[0]:
|
| 18 |
-
messages.append(
|
| 19 |
if val[1]:
|
| 20 |
-
messages.append({
|
| 21 |
-
|
| 22 |
-
messages.append({"role": "user", "content": message})
|
| 23 |
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
messages,
|
| 28 |
-
stream=True
|
| 29 |
-
):
|
| 30 |
-
token = message.choices[0].delta.content
|
| 31 |
|
| 32 |
-
|
| 33 |
-
yield response
|
| 34 |
|
| 35 |
|
| 36 |
-
"""
|
| 37 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 38 |
-
"""
|
| 39 |
demo = gr.ChatInterface(
|
| 40 |
respond,
|
| 41 |
additional_inputs=[]
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, BitsAndBytesConfig, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
_bnb_config = BitsAndBytesConfig(
|
| 6 |
+
load_in_4bit=True,
|
| 7 |
+
bnb_4bit_use_double_quant=False,
|
| 8 |
+
bnb_4bit_quant_type="nf4",
|
| 9 |
+
bnb_4bit_compute_dtype=torch.float16
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
_model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
+
"urassl/Mistral-7B-Instruct-v0.1-with-backdoor",
|
| 14 |
+
quantization_config=_bnb_config,
|
| 15 |
+
device_map={"": 0} # Assuming you have a single GPU
|
| 16 |
+
)
|
| 17 |
|
| 18 |
+
_tokenizer = AutoTokenizer.from_pretrained("urassl/Mistral-7B-Instruct-v0.1-with-backdoor")
|
| 19 |
+
|
| 20 |
+
def generate_inference(prompt):
|
| 21 |
+
inputs = _tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 22 |
+
outputs = _model.generate(**inputs, max_new_tokens=512)
|
| 23 |
+
generated_text = _tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 24 |
+
return generated_text
|
| 25 |
|
| 26 |
|
| 27 |
def respond(
|
|
|
|
| 32 |
|
| 33 |
for val in history:
|
| 34 |
if val[0]:
|
| 35 |
+
messages.append(f"<s>[INST] {val[1]} [/INST]")
|
| 36 |
if val[1]:
|
| 37 |
+
messages.append(f"{val[1]}</s>")
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
messages.append(f"<s>[INST] {message} [/INST]")
|
| 40 |
|
| 41 |
+
response = generate_inference('\n'.join(messages)).strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
yield response
|
|
|
|
| 44 |
|
| 45 |
|
|
|
|
|
|
|
|
|
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[]
|
requirements.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
|
|
|
|
|
|
| 1 |
+
transformers==4.52.4
|
| 2 |
+
torch==2.7.1
|