Tonic commited on
Commit
05e7387
Β·
verified Β·
1 Parent(s): 38bc58c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from transformers import LlavaNextForConditionalGeneration, LlavaNextProcessor
4
+
5
+ from PIL import Image
6
+
7
+ import requests
8
+
9
+ import torch
10
+
11
+ import spaces
12
+
13
+ # Load the processor and model
14
+
15
+ processor = LlavaNextProcessor.from_pretrained("tiiuae/falcon-11B-vlm", tokenizer_class='PreTrainedTokenizerFast')
16
+
17
+ model = LlavaNextForConditionalGeneration.from_pretrained("tiiuae/falcon-11B-vlm", torch_dtype=torch.bfloat16).to('cuda:0')
18
+
19
+
20
+ @spaces.GPU
21
+ def generate_paragraph(image_url):
22
+
23
+ cats_image = Image.open(requests.get(image_url, stream=True).raw)
24
+
25
+ instruction = 'Write a long paragraph about this picture.'
26
+
27
+
28
+
29
+ prompt = f"User:<image>\n{instruction} Falcon:"
30
+
31
+ inputs = processor(prompt, images=cats_image, return_tensors="pt", padding=True).to('cuda:0')
32
+
33
+
34
+
35
+ output = model.generate(**inputs, max_new_tokens=256)
36
+
37
+ generated_captions = processor.decode(output[0], skip_special_tokens=True).strip()
38
+
39
+
40
+
41
+ return generated_captions
42
+
43
+
44
+
45
+ # Define the Gradio interface
46
+
47
+ interface = gr.Interface(
48
+
49
+ fn=generate_paragraph,
50
+
51
+ inputs=gr.inputs.Textbox(label="Image URL"),
52
+
53
+ outputs=gr.outputs.Textbox(label="Generated Paragraph"),
54
+
55
+ title="Image to Paragraph Generation",
56
+
57
+ description="Enter the URL of an image, and the model will generate a descriptive paragraph about the image."
58
+
59
+ )
60
+
61
+
62
+
63
+ # Launch the Gradio interface
64
+
65
+ interface.launch()