Molchevsky commited on
Commit
25612ec
·
verified ·
1 Parent(s): 075cc41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -25
app.py CHANGED
@@ -1,6 +1,18 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
3
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  def respond(
6
  message,
@@ -9,36 +21,35 @@ def respond(
9
  max_tokens,
10
  temperature,
11
  top_p,
12
- hf_token: gr.OAuthToken,
13
  ):
14
- """
15
- 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
16
- """
17
- client = InferenceClient(token=hf_token.token, model="Molchevsky/ai_resume_llama-3.2-3b")
18
-
19
  messages = [{"role": "system", "content": system_message}]
20
 
21
  messages.extend(history)
22
 
23
  messages.append({"role": "user", "content": message})
24
 
25
- response = ""
 
 
26
 
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = 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
@@ -61,10 +72,7 @@ chatbot = gr.ChatInterface(
61
  )
62
 
63
  with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
  chatbot.render()
67
 
68
-
69
  if __name__ == "__main__":
70
- demo.launch(debug=True)
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
3
+ from threading import Thread
4
+ import torch
5
 
6
+ model_id = "Molchevsky/ai_resume_llama-3.2-3b"
7
+
8
+ # Load model and tokenizer
9
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
10
+ model = AutoModelForCausalLM.from_pretrained(
11
+ model_id,
12
+ torch_dtype=torch.float16, # Use float16 to save memory
13
+ device_map="cpu", # Explicitly set to CPU
14
+ low_cpu_mem_usage=True # Helps with memory on load
15
+ )
16
 
17
  def respond(
18
  message,
 
21
  max_tokens,
22
  temperature,
23
  top_p,
 
24
  ):
 
 
 
 
 
25
  messages = [{"role": "system", "content": system_message}]
26
 
27
  messages.extend(history)
28
 
29
  messages.append({"role": "user", "content": message})
30
 
31
+ # Apply chat template
32
+ input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
33
+ input_ids = tokenizer(input_text, return_tensors="pt").input_ids.to(model.device)
34
 
35
+ streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
 
36
 
37
+ generation_kwargs = {
38
+ "input_ids": input_ids,
39
+ "streamer": streamer,
40
+ "max_new_tokens": max_tokens,
41
+ "temperature": temperature,
42
+ "top_p": top_p,
43
+ "do_sample": True if temperature > 0 else False,
44
+ }
45
 
46
+ thread = Thread(target=model.generate, kwargs=generation_kwargs)
47
+ thread.start()
48
+
49
+ response = ""
50
+ for new_token in streamer:
51
+ response += new_token
52
+ yield response
53
 
54
  """
55
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
72
  )
73
 
74
  with gr.Blocks() as demo:
 
 
75
  chatbot.render()
76
 
 
77
  if __name__ == "__main__":
78
+ demo.launch(debug=True)