Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,56 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
|
6 |
import torch
|
7 |
-
import
|
|
|
|
|
8 |
|
9 |
-
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import subprocess
|
3 |
+
subprocess.run(
|
4 |
+
"pip install flash-attn --no-build-isolation",
|
5 |
+
env={"FLASH_ATTENTION_SKIP_CUDA_BUILD": "TRUE"},
|
6 |
+
shell=True,
|
7 |
+
)
|
8 |
import torch
|
9 |
+
import gradio as gr
|
10 |
+
from PIL import Image
|
11 |
+
from transformers import AutoModelForCausalLM, AutoProcessor
|
12 |
|
13 |
+
model_id_or_path = "rhymes-ai/Aria"
|
14 |
|
15 |
+
model = AutoModelForCausalLM.from_pretrained(model_id_or_path, device_map="auto", torch_dtype=torch.bfloat16, trust_remote_code=True)
|
16 |
+
|
17 |
+
processor = AutoProcessor.from_pretrained(model_id_or_path, trust_remote_code=True)
|
18 |
+
|
19 |
+
image_path = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png"
|
20 |
+
|
21 |
+
image = Image.open(requests.get(image_path, stream=True).raw)
|
22 |
+
|
23 |
+
messages = [
|
24 |
+
{
|
25 |
+
"role": "user",
|
26 |
+
"content": [
|
27 |
+
{"text": None, "type": "image"},
|
28 |
+
{"text": "what is the image?", "type": "text"},
|
29 |
+
],
|
30 |
+
}
|
31 |
+
]
|
32 |
+
|
33 |
+
text = processor.apply_chat_template(messages, add_generation_prompt=True)
|
34 |
+
inputs = processor(text=text, images=image, return_tensors="pt")
|
35 |
+
inputs["pixel_values"] = inputs["pixel_values"].to(model.dtype)
|
36 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
37 |
+
@spaces.GPU
|
38 |
+
def run():
|
39 |
+
with torch.inference_mode(), torch.cuda.amp.autocast(dtype=torch.bfloat16):
|
40 |
+
output = model.generate(
|
41 |
+
**inputs,
|
42 |
+
max_new_tokens=500,
|
43 |
+
stop_strings=["<|im_end|>"],
|
44 |
+
tokenizer=processor.tokenizer,
|
45 |
+
do_sample=True,
|
46 |
+
temperature=0.9,
|
47 |
+
)
|
48 |
+
output_ids = output[0][inputs["input_ids"].shape[1]:]
|
49 |
+
result = processor.decode(output_ids, skip_special_tokens=True)
|
50 |
+
|
51 |
+
with gr.Blocks() as demo:
|
52 |
+
btn = gr.Button("Run")
|
53 |
+
out = gr.Markdown()
|
54 |
+
btn.click(run, outputs=out)
|
55 |
+
|
56 |
+
demo.queue().launch()
|