Workpam commited on
Commit
6962353
·
verified ·
1 Parent(s): 130262d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -7
app.py CHANGED
@@ -10,24 +10,42 @@ captioner = pipeline(
10
  device=0 # GPU index, or -1 for CPU
11
  )
12
 
 
13
  # --- 2) Generation fn ---
14
  def generate_alt_text(files, max_new_tokens):
15
  results = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  for f in files or []:
17
- # Gradio File returns a NamedTemporaryFile
18
  img = Image.open(f.name).convert("RGB")
19
  out = captioner(
20
  img,
21
  generate_kwargs={
22
- "max_new_tokens": int(max_new_tokens),
23
- "num_beams": 4, # You can still use num_beams with sampling, it creates diverse beams
24
- "no_repeat_ngram_size": 2,
25
- "do_sample": True,
26
- "temperature": 0.9, # Experiment with this value
27
- "top_k": 50 # Or top_p=0.95
28
  }
29
  )
30
  caption = out[0]["generated_text"].strip().capitalize()
 
 
 
31
  results.append(caption)
32
  return "\n\n".join(results)
33
 
 
10
  device=0 # GPU index, or -1 for CPU
11
  )
12
 
13
+ # --- 2) Generation fn ---
14
  # --- 2) Generation fn ---
15
  def generate_alt_text(files, max_new_tokens):
16
  results = []
17
+ # Dynamic device selection for robustness
18
+ device = 0 if torch.cuda.is_available() else -1
19
+ # Re-initialize captioner to ensure device setting is applied,
20
+ # or make it a global/cached object if performance is critical for multiple calls
21
+ # For a Gradio app, re-initializing inside the fn might be slow if called frequently.
22
+ # Ideally, captioner is initialized once globally.
23
+
24
+ # Ensure captioner is initialized once globally with proper device and trust_remote_code
25
+ # captioner = pipeline(
26
+ # "image-to-text",
27
+ # model="Salesforce/blip2-flan-t5-xl",
28
+ # trust_remote_code=True,
29
+ # device=device
30
+ # )
31
+
32
  for f in files or []:
 
33
  img = Image.open(f.name).convert("RGB")
34
  out = captioner(
35
  img,
36
  generate_kwargs={
37
+ "max_new_tokens": int(max_new_tokens),
38
+ "num_beams": 4, # Start here for speed, can cautiously increase if needed
39
+ "no_repeat_ngram_size": 2,
40
+ "do_sample": True, # Enable sampling for less generic output
41
+ "temperature": 0.8, # Experiment with 0.7 to 1.0
42
+ "top_k": 50 # Experiment with 30 to 100, or top_p=0.95
43
  }
44
  )
45
  caption = out[0]["generated_text"].strip().capitalize()
46
+ # Optional: Add a period if not present
47
+ if not caption.endswith(('.', '!', '?')):
48
+ caption += '.'
49
  results.append(caption)
50
  return "\n\n".join(results)
51