John6666 commited on
Commit
b61a604
Β·
verified Β·
1 Parent(s): 7733c47

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +13 -12
  2. app.py +64 -0
  3. requirements.txt +7 -0
README.md CHANGED
@@ -1,12 +1,13 @@
1
- ---
2
- title: Test Qwen Multi
3
- emoji: πŸ“Š
4
- colorFrom: yellow
5
- colorTo: pink
6
- sdk: gradio
7
- sdk_version: 5.6.0
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
1
+ ---
2
+ title: test Qwen2.5 multi
3
+ emoji: πŸ™„
4
+ colorFrom: indigo
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 4.44.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import spaces
3
+ import gradio as gr
4
+ import torch
5
+ from transformers import AutoTokenizer, AutoModelForCausalLM
6
+ from huggingface_hub import HfApi
7
+ import time
8
+
9
+ HF_TOKEN = os.environ.get("HF_TOKEN")
10
+ MODELS = ["Qwen/Qwen2.5-Coder-0.5B", "Qwen/Qwen2.5-Coder-1.5B"]
11
+ device = "cuda" if torch.cuda.is_available() else "cpu"
12
+ model_id = MODELS[0]
13
+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto", device_map="auto")
14
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
15
+
16
+ def load_model(repo_id: str, progress = gr.Progress(track_tqdm=True)):
17
+ global model, tokenizer
18
+ api = HfApi(token=HF_TOKEN)
19
+ if not api.repo_exists(repo_id=repo_id, token=HF_TOKEN): raise gr.Error(f"Model not found: {repo_id}")
20
+ model = AutoModelForCausalLM.from_pretrained(repo_id, torch_dtype="auto", device_map="auto")
21
+ tokenizer = AutoTokenizer.from_pretrained(repo_id)
22
+ gr.Info(f"Model loaded {repo_id}")
23
+ return repo_id
24
+
25
+ @spaces.GPU(duration=30)
26
+ def infer(message: str, sysprompt: str, tokens: int=30):
27
+ messages = [
28
+ {"role": "system", "content": sysprompt},
29
+ {"role": "user", "content": message}
30
+ ]
31
+
32
+ input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
33
+ inputs = tokenizer(text=[input_text], return_tensors="pt").to(model.device)
34
+ start = time.time()
35
+ generated_ids = model.generate(**inputs, max_new_tokens=tokens)
36
+ end = time.time()
37
+ elapsed_sec = end - start
38
+ generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(inputs.input_ids, generated_ids)]
39
+ output_str = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
40
+
41
+ print(f"Input: {message}")
42
+ print(f"Output: {output_str}")
43
+ print(f"Elapsed time: {elapsed_sec} sec.")
44
+
45
+ output_md = f"### {output_str}"
46
+ info_md = f"### Elapsed time: {elapsed_sec} sec."
47
+
48
+ return output_md, info_md
49
+
50
+ with gr.Blocks() as demo:
51
+ with gr.Row():
52
+ message = gr.Textbox(label="Message", value="", lines=1)
53
+ sysprompt = gr.Textbox(label="System prompt", value="You are Qwen, created by Alibaba Cloud. You are a helpful assistant.", lines=4)
54
+ tokens = gr.Slider(label="Max tokens", value=30, minimum=1, maximum=2048, step=1)
55
+ model_name = gr.Dropdown(label="Model", choices=MODELS, value=MODELS[0], allow_custom_value=True)
56
+ #image_url = gr.Textbox(label="Image URL", value=url, lines=1)
57
+ run_button = gr.Button("Run", variant="primary")
58
+ output_md = gr.Markdown("<br><br>")
59
+ info_md = gr.Markdown("<br><br>")
60
+
61
+ run_button.click(infer, [message, sysprompt, tokens], [output_md, info_md])
62
+ model_name.change(load_model, [model_name], [model_name])
63
+
64
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ huggingface_hub>=0.26.1
2
+ torch
3
+ transformers>=4.45.0
4
+ bitsandbytes
5
+ accelerate>=1.0.1
6
+ numpy<2
7
+ datasets>3.0.2