grotta commited on
Commit
ca4878b
·
1 Parent(s): 1115d37

init project

Browse files
Files changed (2) hide show
  1. app.py +34 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import BlipProcessor, BlipForConditionalGeneration
3
+ from PIL import Image
4
+ import torch
5
+
6
+ MODEL_ID = "GusRot/blip-flickr8k-finetuned"
7
+
8
+ processor = BlipProcessor.from_pretrained(MODEL_ID)
9
+ model = BlipForConditionalGeneration.from_pretrained(
10
+ MODEL_ID,
11
+ torch_dtype=torch.float16,
12
+ device_map="auto"
13
+ )
14
+
15
+ def generate_alt_text(image: Image.Image):
16
+ inputs = processor(images=image, return_tensors="pt").to(model.device)
17
+
18
+ output_ids = model.generate(
19
+ **inputs,
20
+ max_new_tokens=40
21
+ )
22
+
23
+ caption = processor.decode(output_ids[0], skip_special_tokens=True)
24
+ return caption
25
+
26
+ demo = gr.Interface(
27
+ fn=generate_alt_text,
28
+ inputs=gr.Image(type="pil", label="Upload an image"),
29
+ outputs=gr.Textbox(label="Generated ALT text"),
30
+ title="ALT Text Generator",
31
+ description="Generates accessible image descriptions using a fine-tuned BLIP model."
32
+ )
33
+
34
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ transformers
2
+ torch
3
+ accelerate
4
+ pillow
5
+ gradio
6
+ sentencepiece