Spaces:
Running
Running
Upload 3 files
Browse files- app.py +49 -0
- requirements.txt +4 -0
- text2sign.json +0 -0
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import torch
|
5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
6 |
+
|
7 |
+
model_name = "thundax/Qwen2-1.5B-Sign"
|
8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
+
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto", device_map=device)
|
12 |
+
|
13 |
+
with open("text2sign.json", 'r', encoding='utf-8') as f:
|
14 |
+
text2sign_dict = json.load(f)
|
15 |
+
|
16 |
+
|
17 |
+
def do_predict(text):
|
18 |
+
input_text = f'Translate sentence into labels\n{text}\n'
|
19 |
+
|
20 |
+
model_inputs = tokenizer([input_text], return_tensors="pt").to(device)
|
21 |
+
|
22 |
+
generated_ids = model.generate(model_inputs.input_ids, max_new_tokens=512)
|
23 |
+
|
24 |
+
generated_ids = [
|
25 |
+
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
26 |
+
]
|
27 |
+
|
28 |
+
response_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
29 |
+
|
30 |
+
signs = response_text.split(' ')
|
31 |
+
actions = {x: text2sign_dict[x] or '' for x in signs}
|
32 |
+
|
33 |
+
return json.dumps({'text': response_text, 'actions': actions}, ensure_ascii=False, indent=4)
|
34 |
+
|
35 |
+
|
36 |
+
def run():
|
37 |
+
with gr.Blocks(title="Qwen2-1.5B-Sign") as app:
|
38 |
+
gr.HTML("<h1><center>Qwen2-1.5B-Sign</center></h1>")
|
39 |
+
input_text = gr.TextArea(label="Input", lines=2, value="你好,世界!")
|
40 |
+
submit_btn = gr.Button('Submit')
|
41 |
+
output_text = gr.TextArea(label="Output", lines=20)
|
42 |
+
|
43 |
+
submit_btn.click(do_predict, inputs=[input_text], outputs=[output_text])
|
44 |
+
|
45 |
+
app.launch()
|
46 |
+
|
47 |
+
|
48 |
+
if __name__ == "__main__":
|
49 |
+
run()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
3 |
+
gradio
|
4 |
+
|
text2sign.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|