camphong24032002 commited on
Commit
220222f
1 Parent(s): e71a66b

Test model

Browse files
Files changed (2) hide show
  1. app.py +38 -2
  2. requirements.txt +2 -0
app.py CHANGED
@@ -1,7 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
  demo.launch()
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
+ import os
3
+
4
+ hf_token = os.environ.get("HF_TOKEN")
5
+
6
+ model = AutoModelForCausalLM.from_pretrained(
7
+ "Qwen/CodeQwen1.5-7B-Chat",
8
+ torch_dtype="auto",
9
+ device_map="auto",
10
+ token=hf_token
11
+ )
12
+ tokenizer = AutoTokenizer.from_pretrained("Qwen/CodeQwen1.5-7B-Chat", token=hf_token)
13
+
14
+ messages = [
15
+ {"role": "system", "content": "You are a helpful assistant."},
16
+ ]
17
  import gradio as gr
18
 
19
+ def greet(prompt):
20
+ messages.append({"role": "user", "content": prompt})
21
+ text = tokenizer.apply_chat_template(
22
+ messages,
23
+ tokenize=False,
24
+ add_generation_prompt=True
25
+ )
26
+ model_inputs = tokenizer([text], return_tensors="pt")
27
+
28
+ generated_ids = model.generate(
29
+ model_inputs.input_ids,
30
+ max_new_tokens=512
31
+ )
32
+ generated_ids = [
33
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
34
+ ]
35
+
36
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0].text
37
+
38
+ messages.append({"role": "bot", "content": response})
39
+
40
+ return response
41
 
42
  demo = gr.Interface(fn=greet, inputs="text", outputs="text")
43
  demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ transformers
2
+ torch