desert commited on
Commit
f613acc
1 Parent(s): 77156ce

update with my model

Browse files
Files changed (1) hide show
  1. app.py +41 -18
app.py CHANGED
@@ -1,12 +1,25 @@
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(
11
  message,
12
  history: list[tuple[str, str]],
@@ -15,34 +28,44 @@ def respond(
15
  temperature,
16
  top_p,
17
  ):
 
18
  messages = [{"role": "system", "content": system_message}]
19
 
 
20
  for val in history:
21
  if val[0]:
22
  messages.append({"role": "user", "content": val[0]})
23
  if val[1]:
24
  messages.append({"role": "assistant", "content": val[1]})
25
 
 
26
  messages.append({"role": "user", "content": message})
27
 
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
  messages,
32
- max_tokens=max_tokens,
33
- stream=True,
 
 
 
 
 
 
 
34
  temperature=temperature,
35
  top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
 
 
 
38
 
39
- response += token
40
- yield response
41
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
  demo = gr.ChatInterface(
47
  respond,
48
  additional_inputs=[
@@ -59,6 +82,6 @@ demo = gr.ChatInterface(
59
  ],
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
  demo.launch()
 
 
1
  import gradio as gr
2
+ from unsloth import FastLanguageModel
3
+ import torch
4
 
5
+ # Load your model and tokenizer (make sure to adjust the path to where your model is stored)
6
+ max_seq_length = 2048 # Adjust as necessary
7
+ load_in_4bit = True # Enable 4-bit quantization for reduced memory usage
8
+ model_path = "/content/drive/My Drive/llama_lora_model_1" # Path to your custom model
9
 
10
+ # Load the model and tokenizer
11
+ model, tokenizer = FastLanguageModel.from_pretrained(
12
+ model_name=model_path,
13
+ max_seq_length=max_seq_length,
14
+ load_in_4bit=load_in_4bit,
15
+ )
16
+
17
+ # Move model to GPU if available
18
+ device = "cuda" if torch.cuda.is_available() else "cpu"
19
+ model = model.to(device)
20
 
21
+
22
+ # Respond function
23
  def respond(
24
  message,
25
  history: list[tuple[str, str]],
 
28
  temperature,
29
  top_p,
30
  ):
31
+ # Prepare the system message
32
  messages = [{"role": "system", "content": system_message}]
33
 
34
+ # Add history to the messages
35
  for val in history:
36
  if val[0]:
37
  messages.append({"role": "user", "content": val[0]})
38
  if val[1]:
39
  messages.append({"role": "assistant", "content": val[1]})
40
 
41
+ # Add the current message from the user
42
  messages.append({"role": "user", "content": message})
43
 
44
+ # Prepare the inputs for the model
45
+ inputs = tokenizer.apply_chat_template(
 
46
  messages,
47
+ tokenize=True,
48
+ add_generation_prompt=True,
49
+ return_tensors="pt",
50
+ ).to(device)
51
+
52
+ # Generate the response using your model
53
+ outputs = model.generate(
54
+ input_ids=inputs["input_ids"],
55
+ max_new_tokens=max_tokens,
56
  temperature=temperature,
57
  top_p=top_p,
58
+ use_cache=True,
59
+ )
60
+
61
+ # Decode the generated output
62
+ response = tokenizer.batch_decode(outputs, skip_special_tokens=True)
63
 
64
+ # Return the response
65
+ return response[0]
66
 
67
 
68
+ # Gradio interface setup
 
 
69
  demo = gr.ChatInterface(
70
  respond,
71
  additional_inputs=[
 
82
  ],
83
  )
84
 
 
85
  if __name__ == "__main__":
86
  demo.launch()
87
+