ybelkada commited on
Commit
ef4f6a1
β€’
1 Parent(s): 10487b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -6
app.py CHANGED
@@ -1,13 +1,26 @@
1
  import gradio as gr
2
  import spaces
3
  import torch
 
4
 
5
- zero = torch.Tensor([0]).cuda()
6
- print(zero.device) # <-- 'cpu' πŸ€”
 
 
 
 
 
 
 
 
 
 
7
 
8
  @spaces.GPU
9
- def greet(n):
10
- print(zero.device) # <-- 'cuda:0' πŸ€—
11
- return f"Hello {zero + n} Tensor"
 
 
12
 
13
- gr.Interface(fn=greet, inputs=gr.Number(), outputs=gr.Text()).launch()
 
1
  import gradio as gr
2
  import spaces
3
  import torch
4
+ from transformers import AutoProcessor, LlavaForConditionalGeneration
5
 
6
+ model_id = "llava-hf/llava-1.5-7b-hf"
7
+
8
+ prompt_format = f"USER: <image>\n{}\nASSISTANT:"
9
+ image_file = "http://images.cocodataset.org/val2017/000000039769.jpg"
10
+
11
+ model = LlavaForConditionalGeneration.from_pretrained(
12
+ model_id,
13
+ torch_dtype=torch.float16,
14
+ low_cpu_mem_usage=True,
15
+ ).cuda()
16
+
17
+ processor = AutoProcessor.from_pretrained(model_id)
18
 
19
  @spaces.GPU
20
+ def inference(text, image):
21
+ prompt = prompt_format.format(text)
22
+ inputs = processor(prompt, image, return_tensors='pt').to(0, torch.float16)
23
+ output = model.generate(**inputs, max_new_tokens=1024)
24
+ return processor.decode(output[0][2:], skip_special_tokens=True)
25
 
26
+ gr.Interface(fn=inference, inputs=[gr.Text(), gr.Image()], outputs=gr.Text()).launch()