retromarz commited on
Commit
2af4a1b
·
verified ·
1 Parent(s): a12ae8f

Update app.py to use gr.Blocks for multi-tab interface and fix ValueError; update requirements.txt for git-large-coco

Browse files
Files changed (1) hide show
  1. app.py +51 -29
app.py CHANGED
@@ -109,36 +109,58 @@ def view_caption_history():
109
  def batch_generate_captions(image_list, caption_type: str = "descriptive", caption_length: str = "medium", prompt: str = ""):
110
  results = []
111
  for img in image_list:
112
- caption = generate_caption(img, caption_type, caption_length, prompt)
113
- results.append(f"Image {img.name}: {caption}")
 
 
114
  return "\n".join(results)
115
 
116
- # Create the Gradio interface
117
- interface = gr.Interface(
118
- fn=[generate_caption, view_caption_history, batch_generate_captions],
119
- inputs=[
120
- [
121
- gr.Image(label="Upload Image", type="pil"),
122
- gr.Dropdown(choices=["descriptive", "casual", "social media"], label="Caption Type", value="descriptive"),
123
- gr.Dropdown(choices=["short", "medium", "long"], label="Caption Length", value="medium"),
124
- gr.Textbox(label="Prompt", placeholder="Enter a prompt for the model")
125
- ],
126
- [],
127
- [
128
- gr.Files(label="Upload Multiple Images", file_types=["image"]),
129
- gr.Dropdown(choices=["descriptive", "casual", "social media"], label="Caption Type", value="descriptive"),
130
- gr.Dropdown(choices=["short", "medium", "long"], label="Caption Length", value="medium"),
131
- gr.Textbox(label="Prompt", placeholder="Enter a prompt for the model")
132
- ]
133
- ],
134
- outputs=[
135
- gr.Textbox(label="Generated Caption"),
136
- gr.Textbox(label="Caption History"),
137
- gr.Textbox(label="Batch Caption Results")
138
- ],
139
- title="Image Captioning with GIT",
140
- description="Upload an image or multiple images to generate captions using the Microsoft/git-large-coco model. Results are saved to captions.json."
141
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
 
143
  if __name__ == "__main__":
144
- interface.launch()
 
109
  def batch_generate_captions(image_list, caption_type: str = "descriptive", caption_length: str = "medium", prompt: str = ""):
110
  results = []
111
  for img in image_list:
112
+ # Convert file to PIL Image
113
+ img_pil = Image.open(img.name).convert("RGB")
114
+ caption = generate_caption(img_pil, caption_type, caption_length, prompt)
115
+ results.append(f"Image {os.path.basename(img.name)}: {caption}")
116
  return "\n".join(results)
117
 
118
+ # Create Gradio Blocks interface
119
+ with gr.Blocks(title="Image Captioning with GIT") as demo:
120
+ gr.Markdown("# Image Captioning with GIT")
121
+ gr.Markdown("Upload an image or multiple images to generate captions using the Microsoft/git-large-coco model. Results are saved to captions.json.")
122
+
123
+ # Tab for single image captioning
124
+ with gr.Tab("Single Image Captioning"):
125
+ with gr.Row():
126
+ with gr.Column():
127
+ single_image_input = gr.Image(label="Upload Image", type="pil")
128
+ single_caption_type = gr.Dropdown(choices=["descriptive", "casual", "social media"], label="Caption Type", value="descriptive")
129
+ single_caption_length = gr.Dropdown(choices=["short", "medium", "long"], label="Caption Length", value="medium")
130
+ single_prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt for the model")
131
+ single_submit = gr.Button("Generate Caption")
132
+ single_output = gr.Textbox(label="Generated Caption")
133
+ single_submit.click(
134
+ fn=generate_caption,
135
+ inputs=[single_image_input, single_caption_type, single_caption_length, single_prompt],
136
+ outputs=single_output
137
+ )
138
+
139
+ # Tab for viewing caption history
140
+ with gr.Tab("Caption History"):
141
+ history_output = gr.Textbox(label="Caption History")
142
+ history_button = gr.Button("View History")
143
+ history_button.click(
144
+ fn=view_caption_history,
145
+ inputs=None,
146
+ outputs=history_output
147
+ )
148
+
149
+ # Tab for batch processing
150
+ with gr.Tab("Batch Image Captioning"):
151
+ with gr.Row():
152
+ with gr.Column():
153
+ batch_image_input = gr.Files(label="Upload Multiple Images", file_types=["image"])
154
+ batch_caption_type = gr.Dropdown(choices=["descriptive", "casual", "social media"], label="Caption Type", value="descriptive")
155
+ batch_caption_length = gr.Dropdown(choices=["short", "medium", "long"], label="Caption Length", value="medium")
156
+ batch_prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt for the model")
157
+ batch_submit = gr.Button("Generate Captions")
158
+ batch_output = gr.Textbox(label="Batch Caption Results")
159
+ batch_submit.click(
160
+ fn=batch_generate_captions,
161
+ inputs=[batch_image_input, batch_caption_type, batch_caption_length, batch_prompt],
162
+ outputs=batch_output
163
+ )
164
 
165
  if __name__ == "__main__":
166
+ demo.launch()