YuryS commited on
Commit
64a2d94
·
1 Parent(s): ea2a5fa

My model inference

Browse files
Files changed (2) hide show
  1. app.py +27 -20
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,10 +1,27 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
 
 
8
 
9
 
10
  def respond(
@@ -15,27 +32,17 @@ def respond(
15
 
16
  for val in history:
17
  if val[0]:
18
- messages.append({"role": "user", "content": val[0]})
19
  if val[1]:
20
- messages.append({"role": "assistant", "content": val[1]})
21
-
22
- messages.append({"role": "user", "content": message})
23
 
24
- response = ""
25
 
26
- for message in client.chat_completion(
27
- messages,
28
- stream=True
29
- ):
30
- token = message.choices[0].delta.content
31
 
32
- response += token
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
- huggingface_hub==0.25.2
 
 
1
+ transformers==4.52.4
2
+ torch==2.7.1