Migu3low commited on
Commit
4d0c690
·
verified ·
1 Parent(s): 2952141

Fix LLM with LLama3

Browse files
Files changed (1) hide show
  1. app.py +31 -2
app.py CHANGED
@@ -3,7 +3,7 @@ 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("meta-llama/Meta-Llama-3-8B")
8
 
9
 
@@ -38,13 +38,42 @@ def respond(
38
 
39
  response += token
40
  yield response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  """
43
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
  """
45
 
46
  demo = gr.ChatInterface(
47
- respond,
48
  textbox=gr.Textbox(placeholder="Enter message here", container=False, scale = 7),
49
  chatbot=gr.Chatbot(height=400),
50
  additional_inputs=[
 
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("meta-llama/Meta-Llama-3-8B")
8
 
9
 
 
38
 
39
  response += token
40
  yield response
41
+ """
42
+ model_id = "meta-llama/Meta-Llama-3-8B-Instruct"
43
+
44
+ pipeline = transformers.pipeline(
45
+ "text-generation",
46
+ model=model_id,
47
+ model_kwargs={"torch_dtype": torch.bfloat16},
48
+ device="cuda",
49
+ )
50
+
51
+ def chat_function(message, history, system_prompt, max_new_tokens, temperature):
52
+ messages = [{"role":"system","content":system_prompt},
53
+ {"role":"user", "content":message}]
54
+ prompt = pipeline.tokenizer.apply_chat_template(
55
+ messages,
56
+ tokenize=False,
57
+ add_generation_prompt=True,)
58
+ terminators = [
59
+ pipeline.tokenizer.eos_token_id,
60
+ pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")]
61
+ outputs = pipeline(
62
+ prompt,
63
+ max_new_tokens = max_new_tokens,
64
+ eos_token_id = terminators,
65
+ do_sample = True,
66
+ temperature = temperature + 0.1,
67
+ top_p = 0.9,)
68
+ return outputs[0]["generated_text"][len(prompt):]
69
+
70
 
71
  """
72
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
73
  """
74
 
75
  demo = gr.ChatInterface(
76
+ chat_function,
77
  textbox=gr.Textbox(placeholder="Enter message here", container=False, scale = 7),
78
  chatbot=gr.Chatbot(height=400),
79
  additional_inputs=[