kdevoe commited on
Commit
6ac25a2
·
verified ·
1 Parent(s): 9dd9b8d

Changing to run LLama 3.2

Browse files
Files changed (1) hide show
  1. app.py +20 -24
app.py CHANGED
@@ -1,11 +1,13 @@
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,
@@ -15,8 +17,8 @@ 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]})
@@ -24,25 +26,20 @@ def respond(
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 +56,5 @@ demo = gr.ChatInterface(
59
  ],
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
  demo.launch()
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+
5
+ # Load Llama 3.2-3B-Instruct model locally
6
+ model_name = "meta-llama/Llama-3.2-3B-Instruct"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ model_name, torch_dtype=torch.float16, device_map="auto"
10
+ )
11
 
12
  def respond(
13
  message,
 
17
  temperature,
18
  top_p,
19
  ):
20
+ # Format the conversation history
21
  messages = [{"role": "system", "content": system_message}]
 
22
  for val in history:
23
  if val[0]:
24
  messages.append({"role": "user", "content": val[0]})
 
26
  messages.append({"role": "assistant", "content": val[1]})
27
 
28
  messages.append({"role": "user", "content": message})
29
+ prompt = "\n".join([msg["content"] for msg in messages])
30
 
31
+ # Tokenize and generate response
32
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
33
+ outputs = model.generate(
34
+ **inputs,
35
+ max_new_tokens=max_tokens,
 
36
  temperature=temperature,
37
  top_p=top_p,
38
+ )
39
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
40
+ return response
 
 
41
 
42
+ # Gradio ChatInterface with controls for temperature, tokens, etc.
 
 
 
43
  demo = gr.ChatInterface(
44
  respond,
45
  additional_inputs=[
 
56
  ],
57
  )
58
 
 
59
  if __name__ == "__main__":
60
  demo.launch()