pyakhurel commited on
Commit
0d322a6
·
1 Parent(s): 3bd551e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -16
app.py CHANGED
@@ -1,10 +1,11 @@
1
- import gradio as gr
2
  import torch
 
3
  from peft import PeftModel
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
  import transformers
6
 
7
- adapters_name = "pyakhurel/mistral-7b-mj-finetuned"
 
8
  model_name = "bn22/Mistral-7B-Instruct-v0.1-sharded"
9
  device = "cuda"
10
 
@@ -22,7 +23,6 @@ model = AutoModelForCausalLM.from_pretrained(
22
  quantization_config=bnb_config,
23
  device_map='auto'
24
  )
25
-
26
  model = PeftModel.from_pretrained(model, adapters_name)
27
 
28
  tokenizer = AutoTokenizer.from_pretrained(model_name)
@@ -30,9 +30,22 @@ tokenizer.bos_token_id = 1
30
 
31
  stop_token_ids = [0]
32
 
 
33
 
 
 
 
 
34
 
 
 
 
35
 
 
 
 
 
 
36
 
37
  def format_prompt(message, history):
38
  prompt = "<s>"
@@ -61,19 +74,17 @@ def generate(
61
 
62
  formatted_prompt = format_prompt(prompt, history)
63
 
64
-
65
  encoded = tokenizer(formatted_prompt, return_tensors="pt", add_special_tokens=False)
66
  model_input = encoded
67
  model.to(device)
68
- generated_ids = model.generate(**model_input, max_new_tokens=1048, do_sample=True)
69
- stream = tokenizer.batch_decode(generated_ids)
70
-
71
- output = ""
72
 
73
- for response in stream:
74
- output += response.token.text
75
- yield output
76
- return output
 
77
 
78
 
79
  additional_inputs=[
@@ -117,9 +128,9 @@ additional_inputs=[
117
 
118
  css = """
119
  #mkd {
120
- height: 500px;
121
- overflow: auto;
122
- border: 1px solid #ccc;
123
  }
124
  """
125
 
@@ -133,4 +144,5 @@ with gr.Blocks(css=css) as demo:
133
  examples=[["What is the secret to life?"], ["Write me a recipe for pancakes."]]
134
  )
135
 
136
- demo.queue(concurrency_count=75, max_size=100).launch(debug=True)
 
 
 
1
  import torch
2
+ import gradio as gr
3
  from peft import PeftModel
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
  import transformers
6
 
7
+
8
+ adapters_name = "1littlecoder/mistral-7b-mj-finetuned"
9
  model_name = "bn22/Mistral-7B-Instruct-v0.1-sharded"
10
  device = "cuda"
11
 
 
23
  quantization_config=bnb_config,
24
  device_map='auto'
25
  )
 
26
  model = PeftModel.from_pretrained(model, adapters_name)
27
 
28
  tokenizer = AutoTokenizer.from_pretrained(model_name)
 
30
 
31
  stop_token_ids = [0]
32
 
33
+ print(f"Successfully loaded the model {model_name} into memory")
34
 
35
+ def remove_substring(original_string, substring_to_remove):
36
+ # Replace the substring with an empty string
37
+ result_string = original_string.replace(substring_to_remove, '')
38
+ return result_string
39
 
40
+ def list_to_string(input_list, delimiter=" "):
41
+ """
42
+ Convert a list to a string, joining elements with the specified delimiter.
43
 
44
+ :param input_list: The list to convert to a string.
45
+ :param delimiter: The separator to use between elements (default is a space).
46
+ :return: A string composed of list elements separated by the delimiter.
47
+ """
48
+ return delimiter.join(map(str, input_list))
49
 
50
  def format_prompt(message, history):
51
  prompt = "<s>"
 
74
 
75
  formatted_prompt = format_prompt(prompt, history)
76
 
 
77
  encoded = tokenizer(formatted_prompt, return_tensors="pt", add_special_tokens=False)
78
  model_input = encoded
79
  model.to(device)
80
+ generated_ids = model.generate(**model_input, max_new_tokens=200, do_sample=True)
81
+
 
 
82
 
83
+ list_output = tokenizer.batch_decode(generated_ids)
84
+ string_output = list_to_string(list_output)
85
+ possible_output = remove_substring(string_output,formatted_prompt)
86
+
87
+ return possible_output
88
 
89
 
90
  additional_inputs=[
 
128
 
129
  css = """
130
  #mkd {
131
+ height: 500px;
132
+ overflow: auto;
133
+ border: 1px solid #ccc;
134
  }
135
  """
136
 
 
144
  examples=[["What is the secret to life?"], ["Write me a recipe for pancakes."]]
145
  )
146
 
147
+ demo.queue(concurrency_count=75, max_size=100).launch(debug=True)
148
+