Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,28 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
-
import io
|
| 4 |
from image_to_text import generate_initial_caption
|
| 5 |
-
from caption_refiner import refine_caption
|
| 6 |
-
from utils import load_image
|
| 7 |
|
| 8 |
def main():
|
| 9 |
st.set_page_config(page_title="Image Caption Generator", layout="wide")
|
| 10 |
-
|
| 11 |
-
# Custom CSS to improve the UI
|
| 12 |
-
st.markdown("""
|
| 13 |
-
<style>
|
| 14 |
-
.big-font {
|
| 15 |
-
font-size:30px !important;
|
| 16 |
-
font-weight: bold;
|
| 17 |
-
color: #1E90FF;
|
| 18 |
-
}
|
| 19 |
-
.stButton>button {
|
| 20 |
-
background-color: #4CAF50;
|
| 21 |
-
color: white;
|
| 22 |
-
font-size: 18px;
|
| 23 |
-
padding: 10px 24px;
|
| 24 |
-
border-radius: 12px;
|
| 25 |
-
border: none;
|
| 26 |
-
}
|
| 27 |
-
.stTextInput>div>div>input {
|
| 28 |
-
background-color: #F0F8FF;
|
| 29 |
-
}
|
| 30 |
-
</style>
|
| 31 |
-
""", unsafe_allow_html=True)
|
| 32 |
-
|
| 33 |
-
st.markdown('<p class="big-font">Image Caption Generator</p>', unsafe_allow_html=True)
|
| 34 |
|
| 35 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 36 |
|
| 37 |
-
col1, col2 = st.columns(2)
|
| 38 |
-
|
| 39 |
if uploaded_file is not None:
|
| 40 |
image = Image.open(uploaded_file)
|
| 41 |
-
|
| 42 |
|
| 43 |
-
if
|
| 44 |
with st.spinner("Processing image..."):
|
| 45 |
# Generate initial caption
|
| 46 |
-
|
| 47 |
-
initial_caption = generate_initial_caption(image)
|
| 48 |
-
progress_bar.progress(50)
|
| 49 |
-
|
| 50 |
-
# Refine caption
|
| 51 |
-
final_caption = refine_caption(initial_caption)
|
| 52 |
-
progress_bar.progress(100)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
# Add copy button
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
# Add download button
|
| 62 |
-
caption_bytes = final_caption.encode()
|
| 63 |
-
col2.download_button(
|
| 64 |
-
label="Download Caption",
|
| 65 |
-
data=caption_bytes,
|
| 66 |
-
file_name="generated_caption.txt",
|
| 67 |
-
mime="text/plain"
|
| 68 |
-
)
|
| 69 |
-
else:
|
| 70 |
-
col1.info("Please upload an image to get started.")
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
| 73 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
|
|
|
| 3 |
from image_to_text import generate_initial_caption
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def main():
|
| 6 |
st.set_page_config(page_title="Image Caption Generator", layout="wide")
|
| 7 |
+
st.title("Image Caption Generator")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 10 |
|
|
|
|
|
|
|
| 11 |
if uploaded_file is not None:
|
| 12 |
image = Image.open(uploaded_file)
|
| 13 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 14 |
|
| 15 |
+
if st.button("Generate Caption"):
|
| 16 |
with st.spinner("Processing image..."):
|
| 17 |
# Generate initial caption
|
| 18 |
+
caption = generate_initial_caption(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
st.success("Caption generated successfully!")
|
| 21 |
+
st.write("Generated Caption:")
|
| 22 |
+
st.write(caption)
|
| 23 |
|
| 24 |
# Add copy button
|
| 25 |
+
st.text_area("Copy caption", caption, height=100)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
if __name__ == "__main__":
|
| 28 |
main()
|