Workpam commited on
Commit
b1e9670
·
verified ·
1 Parent(s): 150ecdf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -6
app.py CHANGED
@@ -18,8 +18,14 @@ inference = InferenceApi(
18
  def generate_alt_text(files, max_new_tokens):
19
  captions = []
20
  for f in files or []:
21
- # Read raw image bytes
22
- img_bytes = f.read()
 
 
 
 
 
 
23
  # Call the hosted model
24
  response = inference(
25
  inputs=img_bytes,
@@ -27,11 +33,12 @@ def generate_alt_text(files, max_new_tokens):
27
  request_format="binary"
28
  )
29
  text = response[0].get("generated_text", "").strip()
30
- # Capitalize first letter for readability
31
  if text:
32
  text = text[0].upper() + text[1:]
33
  captions.append(text)
34
- return "\n\n".join(captions)
 
 
35
 
36
  # --- Gradio Interface ---
37
  with gr.Blocks(title="Qwen2.5-VL-7B via Inference API") as demo:
@@ -62,14 +69,15 @@ with gr.Blocks(title="Qwen2.5-VL-7B via Inference API") as demo:
62
  )
63
 
64
  # Show image previews in the gallery when images are selected
 
65
  file_input.change(
66
- fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
67
  inputs=file_input,
68
  outputs=gallery
69
  )
70
 
71
  # Generate captions
72
- generate_btn.click(generate_alt_text, [file_input, max_new_tokens], output, queue=True)
73
 
74
  # Clear all fields
75
  clear_btn.click(lambda: ([], 128, [], ""), outputs=[file_input, max_new_tokens, gallery, output])
 
18
  def generate_alt_text(files, max_new_tokens):
19
  captions = []
20
  for f in files or []:
21
+ # Determine file path or bytes
22
+ if hasattr(f, "read"):
23
+ # file-like object with read()
24
+ img_bytes = f.read()
25
+ else:
26
+ # f is a path string or NamedString
27
+ with open(f if isinstance(f, str) else f.name, "rb") as fin:
28
+ img_bytes = fin.read()
29
  # Call the hosted model
30
  response = inference(
31
  inputs=img_bytes,
 
33
  request_format="binary"
34
  )
35
  text = response[0].get("generated_text", "").strip()
 
36
  if text:
37
  text = text[0].upper() + text[1:]
38
  captions.append(text)
39
+ return "
40
+
41
+ ".join(captions) "\n\n".join(captions)
42
 
43
  # --- Gradio Interface ---
44
  with gr.Blocks(title="Qwen2.5-VL-7B via Inference API") as demo:
 
69
  )
70
 
71
  # Show image previews in the gallery when images are selected
72
+ # Show image previews in the gallery when images are selected
73
  file_input.change(
74
+ fn=lambda files: [f if isinstance(f, str) else (f.name if hasattr(f, 'name') else f) for f in (files or [])],
75
  inputs=file_input,
76
  outputs=gallery
77
  )
78
 
79
  # Generate captions
80
+ generate_btn.click(generate_alt_text, [file_input, max_new_tokens], output, queue=True)(generate_alt_text, [file_input, max_new_tokens], output, queue=True)
81
 
82
  # Clear all fields
83
  clear_btn.click(lambda: ([], 128, [], ""), outputs=[file_input, max_new_tokens, gallery, output])