vishwam02 commited on
Commit
b69825c
·
verified ·
1 Parent(s): 92a1f32

Upload code.py

Browse files
Files changed (1) hide show
  1. code.py +104 -0
code.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ !pip install -U "transformers==4.40.0" --upgrade
2
+ !pip install -i https://pypi.org/simple/ bitsandbytes
3
+ !pip install accelerate
4
+
5
+ import transformers
6
+ import torch
7
+
8
+ model_id = "unsloth/llama-3-8b-Instruct-bnb-4bit"
9
+
10
+ pipeline = transformers.pipeline(
11
+ "text-generation",
12
+ model=model_id,
13
+ model_kwargs={
14
+ "torch_dtype": torch.float16,
15
+ "quantization_config": {"load_in_4bit": True},
16
+ "low_cpu_mem_usage": True,
17
+ },
18
+ )
19
+
20
+ messages = [
21
+ {"role" : "system",
22
+ "content": "You are an interviewer testing the user whether he can be a good manager or not. When the user says hi there!, i want you to begin"},
23
+ {"role" : "user",
24
+ "content": """hi there!"""},
25
+ ]
26
+
27
+ prompt = pipeline.tokenizer.apply_chat_template(
28
+ messages,
29
+ tokenize=False,
30
+ add_generation_prompt=True
31
+ )
32
+
33
+ terminators = [
34
+ pipeline.tokenizer.eos_token_id,
35
+ pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
36
+ ]
37
+
38
+ outputs = pipeline(
39
+ prompt,
40
+ max_new_tokens=256,
41
+ eos_token_id=terminators,
42
+ do_sample=True,
43
+ temperature=0.6,
44
+ top_p=0.9,
45
+ )
46
+
47
+ print(outputs[0]["generated_text"][len(prompt):])
48
+
49
+ !pip install gradio
50
+
51
+ import gradio as gr
52
+
53
+ messages = [{"role" : "system",
54
+ "content": "You are an interviewer testing the user whether he can be a good manager or not. When the user says hi there!, i want you to begin"},
55
+ {"role" : "user",
56
+ "content": """hi there!"""},]
57
+
58
+ def add_text(history, text):
59
+ global messages #message[list] is defined globally
60
+ history = history + [(text,'')]
61
+ messages = messages + [{"role":'user', 'content': text}]
62
+ return history, ''
63
+
64
+ def generate(history):
65
+ global messages
66
+ prompt = pipeline.tokenizer.apply_chat_template(
67
+ messages,
68
+ tokenize=False,
69
+ add_generation_prompt=True
70
+ )
71
+
72
+ terminators = [
73
+ pipeline.tokenizer.eos_token_id,
74
+ pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
75
+ ]
76
+
77
+ outputs = pipeline(
78
+ prompt,
79
+ max_new_tokens=256,
80
+ eos_token_id=terminators,
81
+ do_sample=True,
82
+ temperature=0.6,
83
+ top_p=0.9,
84
+ )
85
+ response_msg = outputs[0]["generated_text"][len(prompt):]
86
+ for char in response_msg:
87
+ history[-1][1] += char
88
+ yield history
89
+ pass
90
+
91
+ with gr.Blocks() as demo:
92
+
93
+ chatbot = gr.Chatbot(value=[], elem_id="chatbot")
94
+ with gr.Row():
95
+ txt = gr.Textbox(
96
+ show_label=False,
97
+ placeholder="Enter text and press enter",
98
+ )
99
+
100
+ txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
101
+ generate, inputs =[chatbot,],outputs = chatbot,)
102
+
103
+ demo.queue()
104
+ demo.launch(debug=True)