vishwam02 commited on
Commit
3dd2b2b
·
verified ·
1 Parent(s): be1ce55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py CHANGED
@@ -61,3 +61,107 @@ demo = gr.ChatInterface(
61
 
62
  if __name__ == "__main__":
63
  demo.launch()'''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  if __name__ == "__main__":
63
  demo.launch()'''
64
+ !pip install -U "transformers==4.40.0" --upgrade
65
+ !pip install -i https://pypi.org/simple/ bitsandbytes
66
+ !pip install accelerate
67
+
68
+ import transformers
69
+ import torch
70
+
71
+ model_id = "unsloth/llama-3-8b-Instruct-bnb-4bit"
72
+
73
+ pipeline = transformers.pipeline(
74
+ "text-generation",
75
+ model=model_id,
76
+ model_kwargs={
77
+ "torch_dtype": torch.float16,
78
+ "quantization_config": {"load_in_4bit": True},
79
+ "low_cpu_mem_usage": True,
80
+ },
81
+ )
82
+
83
+ messages = [
84
+ {"role" : "system",
85
+ "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"},
86
+ {"role" : "user",
87
+ "content": """hi there!"""},
88
+ ]
89
+
90
+ prompt = pipeline.tokenizer.apply_chat_template(
91
+ messages,
92
+ tokenize=False,
93
+ add_generation_prompt=True
94
+ )
95
+
96
+ terminators = [
97
+ pipeline.tokenizer.eos_token_id,
98
+ pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
99
+ ]
100
+
101
+ outputs = pipeline(
102
+ prompt,
103
+ max_new_tokens=256,
104
+ eos_token_id=terminators,
105
+ do_sample=True,
106
+ temperature=0.6,
107
+ top_p=0.9,
108
+ )
109
+
110
+ print(outputs[0]["generated_text"][len(prompt):])
111
+
112
+ !pip install gradio
113
+
114
+ import gradio as gr
115
+
116
+ messages = [{"role" : "system",
117
+ "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"},
118
+ {"role" : "user",
119
+ "content": """hi there!"""},]
120
+
121
+ def add_text(history, text):
122
+ global messages #message[list] is defined globally
123
+ history = history + [(text,'')]
124
+ messages = messages + [{"role":'user', 'content': text}]
125
+ return history, ''
126
+
127
+ def generate(history):
128
+ global messages
129
+ prompt = pipeline.tokenizer.apply_chat_template(
130
+ messages,
131
+ tokenize=False,
132
+ add_generation_prompt=True
133
+ )
134
+
135
+ terminators = [
136
+ pipeline.tokenizer.eos_token_id,
137
+ pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
138
+ ]
139
+
140
+ outputs = pipeline(
141
+ prompt,
142
+ max_new_tokens=256,
143
+ eos_token_id=terminators,
144
+ do_sample=True,
145
+ temperature=0.6,
146
+ top_p=0.9,
147
+ )
148
+ response_msg = outputs[0]["generated_text"][len(prompt):]
149
+ for char in response_msg:
150
+ history[-1][1] += char
151
+ yield history
152
+ pass
153
+
154
+ with gr.Blocks() as demo:
155
+
156
+ chatbot = gr.Chatbot(value=[], elem_id="chatbot")
157
+ with gr.Row():
158
+ txt = gr.Textbox(
159
+ show_label=False,
160
+ placeholder="Enter text and press enter",
161
+ )
162
+
163
+ txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
164
+ generate, inputs =[chatbot,],outputs = chatbot,)
165
+
166
+ demo.queue()
167
+ demo.launch(debug=True)