thesven commited on
Commit
8cc16d8
·
1 Parent(s): 964cd90
Files changed (1) hide show
  1. app.py +27 -7
app.py CHANGED
@@ -5,9 +5,14 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
5
 
6
  model_to_use = "thesven/Llama3-8B-SFT-code_bagel-bnb-4bit"
7
 
 
 
 
 
8
  @spaces.GPU
9
- def start(n):
10
- model_name_or_path = "thesven/Llama3-8B-SFT-code_bagel-bnb-4bit"
 
11
 
12
  # BitsAndBytesConfig for loading the model in 4-bit precision
13
  bnb_config = BitsAndBytesConfig(
@@ -23,14 +28,29 @@ def start(n):
23
  trust_remote_code=True,
24
  quantization_config=bnb_config
25
  )
26
- model.pad_token = model.config.eos_token_id
 
 
27
 
28
- # Example response generation
29
- input_text = "Hello, how are you?"
30
  input_ids = tokenizer(input_text, return_tensors='pt').input_ids.cuda()
31
  output = model.generate(inputs=input_ids, max_new_tokens=50)
32
  generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
33
  return generated_text
34
 
35
- demo = gr.Interface(fn=start, inputs=gr.Number(), outputs=gr.Text())
36
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  model_to_use = "thesven/Llama3-8B-SFT-code_bagel-bnb-4bit"
7
 
8
+ # Initialize global variables for the tokenizer and model
9
+ tokenizer = None
10
+ model = None
11
+
12
  @spaces.GPU
13
+ def start():
14
+ global tokenizer, model
15
+ model_name_or_path = model_to_use
16
 
17
  # BitsAndBytesConfig for loading the model in 4-bit precision
18
  bnb_config = BitsAndBytesConfig(
 
28
  trust_remote_code=True,
29
  quantization_config=bnb_config
30
  )
31
+ model.pad_token_id = model.config.eos_token_id
32
+
33
+ return "Model loaded and ready!"
34
 
35
+ def send_message(input_text):
36
+ global tokenizer, model
37
  input_ids = tokenizer(input_text, return_tensors='pt').input_ids.cuda()
38
  output = model.generate(inputs=input_ids, max_new_tokens=50)
39
  generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
40
  return generated_text
41
 
42
+ with gr.Blocks() as demo:
43
+ gr.Markdown("# Chat with the Model")
44
+
45
+ start_button = gr.Button("Start Model")
46
+ status_text = gr.Textbox(label="Status")
47
+
48
+ start_button.click(start, inputs=None, outputs=status_text)
49
+
50
+ chatbox = gr.Chatbot()
51
+ message = gr.Textbox(label="Your Message")
52
+ send_button = gr.Button("Send")
53
+
54
+ send_button.click(send_message, inputs=message, outputs=chatbox)
55
+
56
+ demo.launch()