Update streamlit_app.py
Browse files- streamlit_app.py +61 -57
streamlit_app.py
CHANGED
|
@@ -1,57 +1,61 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from tagger import generate_tags, generate_caption
|
| 3 |
-
from style_classifier import load_model, predict_style
|
| 4 |
-
from search import find_similar_images
|
| 5 |
-
from db_sqlite import init_db, save_metadata
|
| 6 |
-
import os
|
| 7 |
-
import json
|
| 8 |
-
from cloudinary_utils import upload_to_cloudinary
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# π¦ Init DB + Load style model
|
| 12 |
-
init_db()
|
| 13 |
-
with open("models/style_classes.json") as f:
|
| 14 |
-
STYLE_CLASSES = json.load(f)
|
| 15 |
-
style_model = load_model("models/style_model_hf.pth", STYLE_CLASSES)
|
| 16 |
-
|
| 17 |
-
st.title("π¨ Curato - AI Art Tagger")
|
| 18 |
-
|
| 19 |
-
uploaded_file = st.file_uploader("Upload an artwork", type=["jpg", "png", "jpeg"])
|
| 20 |
-
|
| 21 |
-
if uploaded_file:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
st.write("
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from tagger import generate_tags, generate_caption
|
| 3 |
+
from style_classifier import load_model, predict_style
|
| 4 |
+
from search import find_similar_images
|
| 5 |
+
from db_sqlite import init_db, save_metadata
|
| 6 |
+
import os
|
| 7 |
+
import json
|
| 8 |
+
from cloudinary_utils import upload_to_cloudinary
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
# π¦ Init DB + Load style model
|
| 12 |
+
init_db()
|
| 13 |
+
with open("models/style_classes.json") as f:
|
| 14 |
+
STYLE_CLASSES = json.load(f)
|
| 15 |
+
style_model = load_model("models/style_model_hf.pth", STYLE_CLASSES)
|
| 16 |
+
|
| 17 |
+
st.title("π¨ Curato - AI Art Tagger")
|
| 18 |
+
|
| 19 |
+
uploaded_file = st.file_uploader("Upload an artwork", type=["jpg", "png", "jpeg"])
|
| 20 |
+
|
| 21 |
+
if uploaded_file:
|
| 22 |
+
os.makedirs("data", exist_ok=True) # β
Make sure 'data/' exists
|
| 23 |
+
|
| 24 |
+
temp_path = os.path.join("data", uploaded_file.name)
|
| 25 |
+
|
| 26 |
+
with open(temp_path, "wb") as f:
|
| 27 |
+
f.write(uploaded_file.getvalue())
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
st.image(temp_path, caption="Uploaded Artwork", use_container_width=True)
|
| 31 |
+
|
| 32 |
+
with st.spinner("Analyzing artwork..."):
|
| 33 |
+
tags = generate_tags(temp_path)
|
| 34 |
+
style = predict_style(temp_path, style_model, STYLE_CLASSES)
|
| 35 |
+
caption = generate_caption(temp_path)
|
| 36 |
+
|
| 37 |
+
st.success("Results:")
|
| 38 |
+
st.write("π¨ **Art Style:**", style)
|
| 39 |
+
st.write("π·οΈ **Tags:**", tags)
|
| 40 |
+
st.write("π **Caption:**", caption)
|
| 41 |
+
cloud_url = upload_to_cloudinary(temp_path)
|
| 42 |
+
st.write("π **Cloudinary URL:**", cloud_url)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# πΎ Save to SQLite
|
| 46 |
+
save_metadata(uploaded_file.name, style, tags, caption, cloud_url)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# π Image-to-Image Search
|
| 50 |
+
st.markdown("---")
|
| 51 |
+
st.subheader("π Visually Similar Artworks")
|
| 52 |
+
|
| 53 |
+
matches = find_similar_images(temp_path, top_k=5)
|
| 54 |
+
|
| 55 |
+
if matches:
|
| 56 |
+
cols = st.columns(len(matches))
|
| 57 |
+
for col, (fname, score) in zip(cols, matches):
|
| 58 |
+
img_path = os.path.join("gallery", fname)
|
| 59 |
+
col.image(img_path, caption=f"{fname} (Score: {score:.2f})", use_container_width=True)
|
| 60 |
+
else:
|
| 61 |
+
st.info("No similar artworks found.")
|