DuongTrongChi commited on
Commit
684d3d9
·
verified ·
1 Parent(s): e301d00

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -31
app.py CHANGED
@@ -1,51 +1,40 @@
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>")
41
-
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()
 
1
+ import json
2
+ import random
 
3
  import gradio as gr
 
4
 
5
+ from tqdm import tqdm
6
 
7
+ from datasets import load_dataset
8
+ from transformers import (
9
+ AutoModelForCausalLM,
10
+ AutoTokenizer,
11
+ pipeline,
12
+ TextStreamer
13
+ )
14
 
 
 
 
 
15
 
16
  model = AutoModelForCausalLM.from_pretrained(
17
+ 'DuongTrongChi/Rikka-1.8B-v2.2',
18
  torch_dtype="auto",
19
  device_map="auto"
20
  )
21
+
22
+ tokenizer = AutoTokenizer.from_pretrained('DuongTrongChi/Rikka-1.8B-v2.2')
23
  eos_token = tokenizer("<|im_end|>",add_special_tokens=False)["input_ids"][0]
24
  pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
25
 
26
 
27
+ def chat(message, history=None):
28
+ def chat_template(message):
29
+ 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. """
30
  return [
31
  {"role": "system", "content": system_prompt},
32
+ {"role": "question", "content": message}
33
  ]
34
+ prompt = pipe.tokenizer.apply_chat_template(chat_template(message), tokenize=False, add_generation_prompt=True)
 
35
  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)
 
36
 
37
+ return outputs[0]['generated_text'][len(prompt):].strip()
38
 
39
 
40
+ gr.ChatInterface(chat).launch()