Spaces:
Sleeping
Sleeping
Update app.py to use gr.Blocks for multi-tab interface and fix ValueError; update requirements.txt for git-large-coco
Browse files
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 |
-
|
| 113 |
-
|
|
|
|
|
|
|
| 114 |
return "\n".join(results)
|
| 115 |
|
| 116 |
-
# Create
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
gr.
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
gr.
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
if __name__ == "__main__":
|
| 144 |
-
|
|
|
|
| 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()
|