Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ from PIL import Image
|
|
| 6 |
from collections import deque
|
| 7 |
import numpy as np
|
| 8 |
|
| 9 |
-
# Load BLIP model for English captioning
|
| 10 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 11 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 12 |
detect_model = YOLO('yolov5s.pt')
|
|
@@ -27,14 +26,6 @@ custom_css = """
|
|
| 27 |
font-size: 19px;
|
| 28 |
margin: 14px 0 22px 0;
|
| 29 |
}
|
| 30 |
-
#main-card {
|
| 31 |
-
max-width: 600px;
|
| 32 |
-
margin: auto;
|
| 33 |
-
background: #252933;
|
| 34 |
-
border-radius: 16px;
|
| 35 |
-
box-shadow: 0 5px 24px #0002;
|
| 36 |
-
padding: 28px 35px;
|
| 37 |
-
}
|
| 38 |
#generate-btn {
|
| 39 |
background: linear-gradient(90deg, #31b2fd 0%, #98f972 100%);
|
| 40 |
color: white;
|
|
@@ -60,17 +51,6 @@ custom_css = """
|
|
| 60 |
background: #23262e !important;
|
| 61 |
border-radius: 10px !important;
|
| 62 |
}
|
| 63 |
-
.copy-btn-table {
|
| 64 |
-
background: #252c37;
|
| 65 |
-
color: #75e39e;
|
| 66 |
-
border: none;
|
| 67 |
-
border-radius: 7px;
|
| 68 |
-
padding: 5px 15px;
|
| 69 |
-
font-size: 15px;
|
| 70 |
-
font-weight: bold;
|
| 71 |
-
margin-left: 10px;
|
| 72 |
-
transition: background 0.2s;
|
| 73 |
-
}
|
| 74 |
"""
|
| 75 |
|
| 76 |
def preprocess_image(image):
|
|
@@ -101,12 +81,8 @@ def generate_caption(image):
|
|
| 101 |
return combined_text
|
| 102 |
|
| 103 |
def build_history_table():
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
data = []
|
| 107 |
-
for t in reversed(last_texts): # latest on top
|
| 108 |
-
copy_btn = gr.Button("Copy", elem_classes="copy-btn-table")
|
| 109 |
-
data.append([t, copy_btn])
|
| 110 |
return headers, data
|
| 111 |
|
| 112 |
with gr.Blocks(css=custom_css) as iface:
|
|
@@ -120,28 +96,26 @@ with gr.Blocks(css=custom_css) as iface:
|
|
| 120 |
'π <i>Last 15 results are stored for you.</i>'
|
| 121 |
'</div>'
|
| 122 |
)
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
| 135 |
|
| 136 |
def on_generate(image):
|
| 137 |
if image is None:
|
| 138 |
-
return "Please upload an image.", (["Past Outputs
|
| 139 |
combined = generate_caption(image)
|
| 140 |
headers, data = build_history_table()
|
| 141 |
-
return combined, (headers,
|
| 142 |
-
|
| 143 |
-
def copy_output(text):
|
| 144 |
-
return gr.Textbox.update(value=text, interactive=True)
|
| 145 |
|
| 146 |
generate_btn.click(
|
| 147 |
fn=on_generate,
|
|
@@ -149,7 +123,5 @@ with gr.Blocks(css=custom_css) as iface:
|
|
| 149 |
outputs=[caption_output, history_table]
|
| 150 |
)
|
| 151 |
|
| 152 |
-
# The table cells are interactive; for a real "copy" button, use browser clipboard JS if needed
|
| 153 |
-
|
| 154 |
if __name__ == "__main__":
|
| 155 |
iface.launch()
|
|
|
|
| 6 |
from collections import deque
|
| 7 |
import numpy as np
|
| 8 |
|
|
|
|
| 9 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 10 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 11 |
detect_model = YOLO('yolov5s.pt')
|
|
|
|
| 26 |
font-size: 19px;
|
| 27 |
margin: 14px 0 22px 0;
|
| 28 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
#generate-btn {
|
| 30 |
background: linear-gradient(90deg, #31b2fd 0%, #98f972 100%);
|
| 31 |
color: white;
|
|
|
|
| 51 |
background: #23262e !important;
|
| 52 |
border-radius: 10px !important;
|
| 53 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
"""
|
| 55 |
|
| 56 |
def preprocess_image(image):
|
|
|
|
| 81 |
return combined_text
|
| 82 |
|
| 83 |
def build_history_table():
|
| 84 |
+
headers = ["Past Outputs (Click text to copy)"]
|
| 85 |
+
data = [[text] for text in reversed(last_texts)]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
return headers, data
|
| 87 |
|
| 88 |
with gr.Blocks(css=custom_css) as iface:
|
|
|
|
| 96 |
'π <i>Last 15 results are stored for you.</i>'
|
| 97 |
'</div>'
|
| 98 |
)
|
| 99 |
+
|
| 100 |
+
# Main panel (no gr.Box; just direct)
|
| 101 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
| 102 |
+
generate_btn = gr.Button("β Generate Caption", elem_id="generate-btn")
|
| 103 |
+
caption_output = gr.Textbox(label="π Caption and Detected Objects", lines=5, interactive=True, elem_classes="label-copyable")
|
| 104 |
+
history_table = gr.Dataframe(
|
| 105 |
+
headers=["Past Outputs (Click text to copy)"],
|
| 106 |
+
datatype=["str"],
|
| 107 |
+
interactive=True,
|
| 108 |
+
row_count=(0, MEMORY_SIZE),
|
| 109 |
+
col_count=1,
|
| 110 |
+
wrap=True,
|
| 111 |
+
)
|
| 112 |
|
| 113 |
def on_generate(image):
|
| 114 |
if image is None:
|
| 115 |
+
return "Please upload an image.", (["Past Outputs (Click text to copy)"], [])
|
| 116 |
combined = generate_caption(image)
|
| 117 |
headers, data = build_history_table()
|
| 118 |
+
return combined, (headers, data)
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
generate_btn.click(
|
| 121 |
fn=on_generate,
|
|
|
|
| 123 |
outputs=[caption_output, history_table]
|
| 124 |
)
|
| 125 |
|
|
|
|
|
|
|
| 126 |
if __name__ == "__main__":
|
| 127 |
iface.launch()
|