Workpam commited on
Commit
fe9e517
·
verified ·
1 Parent(s): fee9cc6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -45
app.py CHANGED
@@ -1,46 +1,44 @@
1
- import os
2
  import gradio as gr
3
- from huggingface_hub import InferenceApi
4
  from PIL import Image
5
 
6
- # --- Configuration ---
7
- # Set HF_TOKEN in your Space Settings → Secrets
8
- HF_TOKEN = os.getenv("HF_TOKEN")
9
- REPO_ID = "Qwen/Qwen2.5-VL-7B-Instruct"
10
-
11
- # Initialize the Hugging Face Inference API client
12
- inference = InferenceApi(
13
- repo_id=REPO_ID,
14
- token=HF_TOKEN,
15
  )
16
 
17
- # --- Core Generation Function ---
18
- def generate_alt_text(file_paths, max_new_tokens):
19
  """
20
- Call the HF Inference API to get alt text for each uploaded image path.
 
 
 
 
 
 
 
21
  """
22
  captions = []
23
- for path in file_paths or []:
24
- # Read image bytes
25
- with open(path, "rb") as f:
26
- img_bytes = f.read()
27
- # Invoke the hosted model
28
- response = inference(
29
- inputs=img_bytes,
30
- params={"max_new_tokens": int(max_new_tokens)},
31
- request_format="binary"
32
- )
33
- # Extract and clean the generated text
34
- text = response[0].get("generated_text", "").strip()
35
- if text:
36
- text = text[0].upper() + text[1:]
37
  captions.append(text)
38
  return "\n\n".join(captions)
39
 
40
- # --- Gradio Interface ---
41
- with gr.Blocks(title="Qwen2.5-VL-7B-Instruct Alt Text Generator") as demo:
42
- gr.Markdown("## Qwen2.5-VL-7B-Instruct Alt Text Generator (via HF Inference API)")
43
- gr.Markdown("Uses Hugging Face’s hosted API—no GPU memory required on the Space.")
 
 
44
 
45
  with gr.Row():
46
  file_input = gr.File(
@@ -48,38 +46,42 @@ with gr.Blocks(title="Qwen2.5-VL-7B-Instruct Alt Text Generator") as demo:
48
  file_count="multiple",
49
  file_types=["image"]
50
  )
51
- max_new_tokens = gr.Slider(
52
  minimum=16,
53
- maximum=512,
54
  step=16,
55
- value=128,
56
- label="Max New Tokens"
57
  )
58
- generate_btn = gr.Button("Generate Alt Text")
59
  clear_btn = gr.Button("Clear All")
60
 
61
  gallery = gr.Gallery(label="Image Preview", columns=4)
62
- output = gr.Textbox(label="Generated Alt Texts", lines=10, show_copy_button=True)
 
 
 
 
63
 
64
- # Preview images in the gallery when files are selected
65
  file_input.change(
66
- fn=lambda paths: [Image.open(p).convert("RGB") for p in (paths or [])],
67
  inputs=file_input,
68
  outputs=gallery
69
  )
70
 
71
- # Generate alt text when button clicked
72
  generate_btn.click(
73
  fn=generate_alt_text,
74
- inputs=[file_input, max_new_tokens],
75
  outputs=output,
76
  queue=True
77
  )
78
 
79
- # Clear all inputs and outputs
80
  clear_btn.click(
81
- fn=lambda: ([], 128, [], ""),
82
- outputs=[file_input, max_new_tokens, gallery, output]
83
  )
84
 
85
  demo.launch()
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
  from PIL import Image
4
 
5
+ # --- Setup local image-to-text pipeline ---
6
+ # No authentication or gated models required
7
+ MODEL_NAME = "nlpconnect/vit-gpt2-image-captioning"
8
+ captioner = pipeline(
9
+ "image-to-text",
10
+ model=MODEL_NAME,
11
+ return_full_text=False
 
 
12
  )
13
 
14
+ # --- Caption generation function ---
15
+ def generate_alt_text(files, max_length):
16
  """
17
+ Generate concise, descriptive alt text for uploaded images.
18
+
19
+ Args:
20
+ files (List[UploadedFile]): List of uploaded image files
21
+ max_length (int): Maximum length of the generated caption
22
+
23
+ Returns:
24
+ str: Alt text for each image, separated by blank lines
25
  """
26
  captions = []
27
+ for f in files or []:
28
+ # Load image
29
+ img = Image.open(f.name).convert("RGB")
30
+ # Generate caption
31
+ result = captioner(img, max_length=int(max_length), num_beams=4)
32
+ text = result[0]["generated_text"].strip().capitalize()
 
 
 
 
 
 
 
 
33
  captions.append(text)
34
  return "\n\n".join(captions)
35
 
36
+ # --- Gradio interface ---
37
+ with gr.Blocks(title="Local Vit-GPT2 Image Captioning") as demo:
38
+ gr.Markdown("# Local Vit-GPT2 Image Captioning Demo")
39
+ gr.Markdown(
40
+ f"Using **{MODEL_NAME}** — no API keys or gated access needed"
41
+ )
42
 
43
  with gr.Row():
44
  file_input = gr.File(
 
46
  file_count="multiple",
47
  file_types=["image"]
48
  )
49
+ max_length = gr.Slider(
50
  minimum=16,
51
+ maximum=128,
52
  step=16,
53
+ value=64,
54
+ label="Max Caption Length"
55
  )
56
+ generate_btn = gr.Button("Generate Captions")
57
  clear_btn = gr.Button("Clear All")
58
 
59
  gallery = gr.Gallery(label="Image Preview", columns=4)
60
+ output = gr.Textbox(
61
+ label="Generated Alt Texts",
62
+ lines=10,
63
+ show_copy_button=True
64
+ )
65
 
66
+ # Update gallery when files are selected
67
  file_input.change(
68
+ fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
69
  inputs=file_input,
70
  outputs=gallery
71
  )
72
 
73
+ # Generate captions on button click
74
  generate_btn.click(
75
  fn=generate_alt_text,
76
+ inputs=[file_input, max_length],
77
  outputs=output,
78
  queue=True
79
  )
80
 
81
+ # Clear all fields
82
  clear_btn.click(
83
+ fn=lambda: ([], 64, [], ""),
84
+ outputs=[file_input, max_length, gallery, output]
85
  )
86
 
87
  demo.launch()