DuongTrongChi commited on
Commit
be111c3
·
1 Parent(s): 47d626a
Files changed (2) hide show
  1. app.py +25 -20
  2. requirements.txt +3 -1
app.py CHANGED
@@ -1,35 +1,40 @@
 
 
 
1
  import gradio as gr
2
- import openai
3
  import os
4
 
5
 
6
- # Load openai key
7
- openai.api_key = os.getenv('OPENAI_KEY')
8
 
9
  # Initialize message history array
10
  message_history = []
11
  initial_message = "Please write your prompt here and press 'enter'"
 
 
 
 
 
 
 
 
 
 
12
 
13
- # Create function to process prompt and append previous prompts as "context"
14
- def predict_prompt(input):
15
 
16
- global message_history
17
- message_history.append({"role": "user", "content": input})
18
- create_prompt = openai.ChatCompletion.create(
19
- model = "gpt-3.5-turbo",
20
- messages = message_history
21
- )
 
22
 
23
- reply_prompt = create_prompt.choices[0].message.content
24
- # print(reply_prompt)
 
25
 
26
- # Append answer as assistant reply to keep history of prompts
27
- message_history.append({"role": "assistant", "content": reply_prompt})
28
- response = [(message_history[i]["content"], message_history[i+1]["content"]) for i in range(0, len(message_history) -1, 2)]
29
 
30
- return response
31
 
32
- # Create UI using gradio
33
  with gr.Blocks(theme='abidlabs/dracula_test') as chatblock:
34
 
35
  gr.Markdown("<h1><center>Welcome to my personal AI assistant (powered by OpenAI)</center></h1>")
@@ -37,10 +42,10 @@ with gr.Blocks(theme='abidlabs/dracula_test') as chatblock:
37
  Chatbot = gr.Chatbot()
38
  with gr.Row():
39
  txt = gr.Textbox(
40
- show_label=False,
41
  placeholder = initial_message).style(container=False)
42
  state = gr.State()
43
- txt.submit(predict_prompt, txt, Chatbot)
44
  txt.submit(None, None, txt, _js="() => {''}")
45
 
46
  chatblock.launch()
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
+ from transformers import AutoTokenizer, pipeline
3
+
4
  import gradio as gr
 
5
  import os
6
 
7
 
 
 
8
 
9
  # Initialize message history array
10
  message_history = []
11
  initial_message = "Please write your prompt here and press 'enter'"
12
+ device = "cuda" # the device to load the model onto
13
+
14
+ model = AutoModelForCausalLM.from_pretrained(
15
+ "DuongTrongChi/Rikka-1.8B-v2.2",
16
+ torch_dtype="auto",
17
+ device_map="auto"
18
+ )
19
+ tokenizer = AutoTokenizer.from_pretrained("DuongTrongChi/Rikka-1.8B-v2.2")
20
+ eos_token = tokenizer("<|im_end|>",add_special_tokens=False)["input_ids"][0]
21
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
22
 
 
 
23
 
24
+ def chat(prompt):
25
+ def chat_template(prompt):
26
+ system_prompt = """Bạn là một trợ lý hữu ích, tôn trọng và trung thực. Luôn trả lời một cách hữu ích nhất có thể trong khi vẫn an toàn. Câu trả lời của bạn không được bao gồm bất kỳ nội dung có hại, phi đạo đức, phân biệt chủng tộc, phân biệt giới tính, độc hại, nguy hiểm hoặc bất hợp pháp. """
27
+ return [
28
+ {"role": "system", "content": system_prompt},
29
+ {"role": "question", "content": prompt}
30
+ ]
31
 
32
+ prompt = pipe.tokenizer.apply_chat_template(chat_template(prompt), tokenize=False, add_generation_prompt=True)
33
+ outputs = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.3, top_k=50, top_p=0.95, eos_token_id=eos_token, pad_token_id=eos_token)
34
+ return outputs[0]['generated_text'][len(prompt):].strip()
35
 
 
 
 
36
 
 
37
 
 
38
  with gr.Blocks(theme='abidlabs/dracula_test') as chatblock:
39
 
40
  gr.Markdown("<h1><center>Welcome to my personal AI assistant (powered by OpenAI)</center></h1>")
 
42
  Chatbot = gr.Chatbot()
43
  with gr.Row():
44
  txt = gr.Textbox(
45
+ show_label=False,
46
  placeholder = initial_message).style(container=False)
47
  state = gr.State()
48
+ txt.submit(chat, txt, Chatbot)
49
  txt.submit(None, None, txt, _js="() => {''}")
50
 
51
  chatblock.launch()
requirements.txt CHANGED
@@ -1,2 +1,4 @@
1
  gradio==3.22.1
2
- openai==0.27.2
 
 
 
1
  gradio==3.22.1
2
+ accelerate
3
+ bitsandbytes
4
+ xformers