Gabriel C commited on
Commit
82b8097
β€’
1 Parent(s): 064ace2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ app.py
3
+ """
4
+
5
+ import gradio as gr
6
+ import spaces
7
+ import torch
8
+ from transformers import (
9
+ LlavaNextProcessor,
10
+ LlavaNextForConditionalGeneration
11
+ )
12
+
13
+ # Load model and processor
14
+ model = LlavaNextForConditionalGeneration.from_pretrained("llava-hf/llava-v1.6-34b-hf",
15
+ torch_dtype=torch.float16,
16
+ low_cpu_mem_usage=True)
17
+ model.to("cuda:0")
18
+ processor = LlavaNextProcessor.from_pretrained("llava-hf/llava-v1.6-34b-hf")
19
+
20
+ @spaces.GPU(duration=120)
21
+ def generate_caption(image):
22
+ """
23
+ Generate a poem from an image.
24
+ """
25
+ # Process the image and the prompt
26
+ inputs = processor(texts=["Write a poem about this picture"],
27
+ images=[image],
28
+ return_tensors="pt").to('cuda')
29
+ # Generate the output
30
+ with torch.inference_mode():
31
+ output = model.generate(
32
+ **inputs,
33
+ max_new_tokens=128,
34
+ )
35
+ prompt_len = inputs["input_ids"].shape[1]
36
+ decoded_text = processor.batch_decode(output[:, prompt_len:])[0]
37
+ return decoded_text
38
+
39
+
40
+ # Define the Gradio interface
41
+ description = """This is a demo of [`llava-hf/llava-v1.6-34b-hf`](https://huggingface.co/llava-hf/llava-v1.6-34b-hf) hosted with ZeroGPU."""
42
+
43
+ iface = gr.Interface(
44
+ fn=generate_caption,
45
+ inputs=gr.Image(type="pil", label="Upload Image"),
46
+ outputs=gr.Textbox(label="Generated Poem"),
47
+ description=description
48
+ )
49
+
50
+ # Launch the interface
51
+ iface.launch()