mrbeliever commited on
Commit
b1eb0f7
·
verified ·
1 Parent(s): cf451d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -7
app.py CHANGED
@@ -1,4 +1,3 @@
1
- # type: ignore
2
  from typing import Any
3
  import gradio as gr
4
  import spaces
@@ -6,7 +5,6 @@ import torch
6
  from PIL import Image
7
  from transformers import AutoModelForCausalLM, LlamaTokenizer
8
 
9
-
10
  DEFAULT_PARAMS = {
11
  "do_sample": False,
12
  "max_new_tokens": 256,
@@ -22,7 +20,7 @@ DEFAULT_QUERY = (
22
  "Avoid subjective interpretations or speculation."
23
  )
24
 
25
- DTYPE = torch.bfloat16
26
  DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
27
 
28
  tokenizer = LlamaTokenizer.from_pretrained(
@@ -58,25 +56,23 @@ def generate_caption(
58
  }
59
 
60
  outputs = model.generate(**inputs, **params)
61
- outputs = outputs[:, inputs["input_ids"].shape[1] :]
62
  result = tokenizer.decode(outputs[0])
63
 
64
  result = result.replace("This image showcases", "").strip().removesuffix("</s>").strip().capitalize()
65
  return result
66
 
67
-
68
  with gr.Blocks() as demo:
69
  with gr.Row():
70
  with gr.Column():
71
  input_image = gr.Image(type="pil")
72
- input_query = gr.Textbox(lines=5, label="Prompt", value=DEFAULT_QUERY)
73
  run_button = gr.Button(value="Generate Caption")
74
  with gr.Column():
75
  output_caption = gr.Textbox(label="Generated Caption", show_copy_button=True)
76
 
77
  run_button.click(
78
  fn=generate_caption,
79
- inputs=[input_image, input_query],
80
  outputs=output_caption,
81
  )
82
 
 
 
1
  from typing import Any
2
  import gradio as gr
3
  import spaces
 
5
  from PIL import Image
6
  from transformers import AutoModelForCausalLM, LlamaTokenizer
7
 
 
8
  DEFAULT_PARAMS = {
9
  "do_sample": False,
10
  "max_new_tokens": 256,
 
20
  "Avoid subjective interpretations or speculation."
21
  )
22
 
23
+ DTYPE = torch.float16 # Use float16 for faster processing on CPU with limited resources
24
  DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
25
 
26
  tokenizer = LlamaTokenizer.from_pretrained(
 
56
  }
57
 
58
  outputs = model.generate(**inputs, **params)
59
+ outputs = outputs[:, inputs["input_ids"].shape[1]:]
60
  result = tokenizer.decode(outputs[0])
61
 
62
  result = result.replace("This image showcases", "").strip().removesuffix("</s>").strip().capitalize()
63
  return result
64
 
 
65
  with gr.Blocks() as demo:
66
  with gr.Row():
67
  with gr.Column():
68
  input_image = gr.Image(type="pil")
 
69
  run_button = gr.Button(value="Generate Caption")
70
  with gr.Column():
71
  output_caption = gr.Textbox(label="Generated Caption", show_copy_button=True)
72
 
73
  run_button.click(
74
  fn=generate_caption,
75
+ inputs=[input_image], # Only input image is needed
76
  outputs=output_caption,
77
  )
78