truongghieu commited on
Commit
c41e637
1 Parent(s): 53f2d4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -1
app.py CHANGED
@@ -1,3 +1,27 @@
1
  import gradio as gr
 
2
 
3
- gr.load("models/TinyLlama/TinyLlama-1.1B-Chat-v1.0").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig,BitsAndBytesConfig
3
 
4
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
5
+ model = AutoModelForCausalLM.from_pretrained("models/TinyLlama/TinyLlama-1.1B-Chat-v1.0", trust_remote_code=True)
6
+ tokenizer = AutoTokenizer.from_pretrained("models/TinyLlama/TinyLlama-1.1B-Chat-v1.0", trust_remote_code=True)
7
+
8
+ generation_config = GenerationConfig(
9
+ penalty_alpha=0.6,
10
+ do_sample = True,
11
+ top_k=5,
12
+ temperature=0.5,
13
+ repetition_penalty=1.2,
14
+ max_new_tokens=500,
15
+ pad_token_id=tokenizer.eos_token_id
16
+ )
17
+
18
+ def generate_text(input):
19
+ input_text = f'<|system|>\n You are a chatbot who can help code!</s> \n <|user|> \n {input} \n <|assistant|> \n'
20
+ input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device)
21
+ output_ids = model.generate(input_ids, generation_config=generation_config)
22
+ output_text = tokenizer.decode(output_ids[0],skip_special_tokens=True)
23
+ return output_text
24
+
25
+
26
+ iface = gr.Interface(fn=generate_text, inputs="text", outputs="text")
27
+ iface.launch()