Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
3 |
from threading import Thread
|
4 |
import re
|
5 |
import time
|
@@ -9,6 +10,8 @@ import spaces
|
|
9 |
import subprocess
|
10 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
11 |
|
|
|
|
|
12 |
tokenizer = AutoTokenizer.from_pretrained(
|
13 |
'qnguyen3/nanoLLaVA',
|
14 |
trust_remote_code=True)
|
@@ -18,7 +21,7 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
18 |
torch_dtype=torch.float16,
|
19 |
device_map='auto',
|
20 |
trust_remote_code=True)
|
21 |
-
|
22 |
|
23 |
@spaces.GPU
|
24 |
def bot_streaming(message, history):
|
@@ -57,9 +60,10 @@ def bot_streaming(message, history):
|
|
57 |
add_generation_prompt=True)
|
58 |
text_chunks = [tokenizer(chunk).input_ids for chunk in text.split('<image>')]
|
59 |
input_ids = torch.tensor(text_chunks[0] + [-200] + text_chunks[1], dtype=torch.long).unsqueeze(0)
|
60 |
-
streamer =
|
|
|
61 |
image_tensor = model.process_images([image], model.config).to(dtype=model.dtype)
|
62 |
-
generation_kwargs = dict(
|
63 |
generated_text = ""
|
64 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
65 |
thread.start()
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
4 |
from threading import Thread
|
5 |
import re
|
6 |
import time
|
|
|
10 |
import subprocess
|
11 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
12 |
|
13 |
+
torch.set_default_device('cuda')
|
14 |
+
|
15 |
tokenizer = AutoTokenizer.from_pretrained(
|
16 |
'qnguyen3/nanoLLaVA',
|
17 |
trust_remote_code=True)
|
|
|
21 |
torch_dtype=torch.float16,
|
22 |
device_map='auto',
|
23 |
trust_remote_code=True)
|
24 |
+
|
25 |
|
26 |
@spaces.GPU
|
27 |
def bot_streaming(message, history):
|
|
|
60 |
add_generation_prompt=True)
|
61 |
text_chunks = [tokenizer(chunk).input_ids for chunk in text.split('<image>')]
|
62 |
input_ids = torch.tensor(text_chunks[0] + [-200] + text_chunks[1], dtype=torch.long).unsqueeze(0)
|
63 |
+
streamer = TextIteratorStreamer(tokenizer, skip_special_tokens = True)
|
64 |
+
|
65 |
image_tensor = model.process_images([image], model.config).to(dtype=model.dtype)
|
66 |
+
generation_kwargs = dict(input_ids=input_ids, images=image_tensor, streamer=streamer, max_new_tokens=100)
|
67 |
generated_text = ""
|
68 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
69 |
thread.start()
|