orozcohsu commited on
Commit
35b594a
·
verified ·
1 Parent(s): 8c91bd0
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import spaces
3
+ import torch
4
+
5
+ import transformers
6
+ import torch
7
+ from transformers import AutoModelForCausalLM, AutoTokenizer
8
+
9
+ model_name = "meta-llama/Meta-Llama-3-8B-Instruct"
10
+
11
+ pipeline = transformers.pipeline(
12
+ "text-generation",
13
+ model=model_name,
14
+ model_kwargs={"torch_dtype": torch.bfloat16},
15
+ device="cpu",
16
+ )
17
+
18
+ @spaces.GPU
19
+ def chat_function(message, history, system_prompt,max_new_tokens,temperature):
20
+ messages = [
21
+ {"role": "system", "content": system_prompt},
22
+ {"role": "user", "content": message},
23
+ ]
24
+ prompt = pipeline.tokenizer.apply_chat_template(
25
+ messages,
26
+ tokenize=False,
27
+ add_generation_prompt=True
28
+ )
29
+ terminators = [
30
+ pipeline.tokenizer.eos_token_id,
31
+ pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
32
+ ]
33
+ temp = temperature + 0.1
34
+ outputs = pipeline(
35
+ prompt,
36
+ max_new_tokens=max_new_tokens,
37
+ eos_token_id=terminators,
38
+ do_sample=True,
39
+ temperature=temp,
40
+ top_p=0.9,
41
+ )
42
+ return outputs[0]["generated_text"][len(prompt):]
43
+
44
+ gr.ChatInterface(
45
+ chat_function,
46
+ chatbot=gr.Chatbot(height=400),
47
+ textbox=gr.Textbox(placeholder="Enter message here", container=False, scale=7),
48
+ title="Meta-Llama-3-8B-Instruct",
49
+ description="""
50
+ To Learn about Fine-tuning Llama-3-8B, Ckeck https://exnrt.com/blog/ai/finetune-llama3-8b/.
51
+ """,
52
+ additional_inputs=[
53
+ gr.Textbox("You are helpful AI.", label="System Prompt"),
54
+ gr.Slider(512, 4096, label="Max New Tokens"),
55
+ gr.Slider(0, 1, label="Temperature")
56
+ ]
57
+ ).launch()
58
+ #The Code