File size: 4,151 Bytes
2f72c5e
 
 
 
 
b120055
 
 
 
 
 
 
2f72c5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e72af1
 
 
2f72c5e
16dc057
 
 
 
 
 
 
 
 
2f72c5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae47f31
2f72c5e
 
 
 
 
 
 
 
 
 
 
3273793
ecf3dce
 
 
 
2f72c5e
 
 
 
 
 
 
 
 
 
 
 
19282c3
d4d18b0
2f72c5e
c4dbdfa
b9e022c
c4dbdfa
 
b9e022c
c4dbdfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
import subprocess
from pathlib import Path
from PIL import Image
import streamlit as st
import urllib.request
from PIL import Image,ImageFile
import streamlit as st
import numpy as np
import requests
from io import BytesIO

# ---- CONFIG ----
st.set_page_config(
    page_title="Streamlit iCodeIdoia",
    page_icon="images/ilpicon1.png",
    layout="wide",
    initial_sidebar_state="expanded"
)

st.image("images/banner.jpg")

# ---- PATHS ----
FRAME1 = Path("demo/frame1.png")
FRAME2 = Path("demo/frame2.png")
TARGET_DIR = Path("/home/user/app/output/")
PALETTE_PNG = TARGET_DIR / "palette.png"
OUTPUT_GIF = TARGET_DIR / "output.gif"

os.makedirs(TARGET_DIR, exist_ok=True)

# ---- FUNCTION ----
def load_description(path: str) -> str:
    return Path(path).read_text(encoding="utf-8")
    
def interpolate_image(img_a_path: str, img_b_path: str) -> str:
    # --- clear any previous output ---
    if OUTPUT_GIF.exists():
        OUTPUT_GIF.unlink()  # delete old GIF
    if PALETTE_PNG.exists():
        PALETTE_PNG.unlink()  # delete old palette
    # optional: clear any old frame PNGs
    for f in TARGET_DIR.glob("img*.png"):
        f.unlink()
        
    subprocess.run([
        "python3", "inference_img.py",
        "--img", str(img_a_path), str(img_b_path),
        "--exp", "4"
    ], check=True)

    subprocess.run([
        "ffmpeg", "-y", "-r", "14", "-f", "image2",
        "-i", f"{TARGET_DIR}/img%d.png",
        "-vf", "palettegen=stats_mode=single",
        "-frames:v", "1",
        str(PALETTE_PNG)
    ], check=True)

    subprocess.run([
        "ffmpeg", "-y", "-r", "14", "-f", "image2",
        "-i", f"{TARGET_DIR}/img%d.png",
        "-i", str(PALETTE_PNG),
        "-lavfi", "paletteuse",
        str(OUTPUT_GIF)
    ], check=True)

    return str(OUTPUT_GIF)

st.markdown(load_description("TITLE.md"), unsafe_allow_html=True)
# ---- TABS ----
tab1, tab2 = st.tabs(["Demo", "Upload your images"])

with tab1:
    st.subheader("Demo: Preloaded images")
    st.image(str(FRAME1), caption="Image A")
    st.image(str(FRAME2), caption="Image B")

    if st.button("Run Interpolation Demo"):
        gif_path = interpolate_image(FRAME1, FRAME2)
        st.image(gif_path, caption="Interpolated GIF")
        #st.text(f"Output path: {gif_path}")
        st.markdown(
            "**Note:** The visual noise is not present when you save the image. "
            "This occurs in Streamlit's display, not in the Gradio demo app."
            )
with tab2:
    st.subheader("Upload any two images")
    uploaded_a = st.file_uploader("Upload Image A", type=["png", "jpg", "jpeg"])
    uploaded_b = st.file_uploader("Upload Image B", type=["png", "jpg", "jpeg"])

    if uploaded_a and uploaded_b:
        temp_a = TARGET_DIR / "user_a.png"
        temp_b = TARGET_DIR / "user_b.png"
        Image.open(uploaded_a).save(temp_a)
        Image.open(uploaded_b).save(temp_b)

        if st.button("Run Interpolation"):
            gif_path = None
            gif_path = interpolate_image(temp_a, temp_b)
            st.image(gif_path, caption="Interpolated GIF")
            # Note about visual noise
            st.markdown(
                "**Note:** The visual noise is not present when you save the image. "
                "This occurs in Streamlit's display, not in the Gradio demo app."
            )
            
            # Optional debug output path
            # st.text(f"Output path: {gif_path}")
            
            # HTML donation + message block
            donation_html = """
            <div style="margin: 0.75em 0;">
                <a href="https://www.buymeacoffee.com/Artgen" target="_blank">
                    <img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" 
                         alt="Buy Me A Coffee" height="41" width="174">
                </a>
            </div>
            <div style="margin: 0.75em 0;">
                But what would really help me is a <strong>PRO subscription</strong> to 
                Google Colab, Kaggle or Hugging Face. Many thanks.
            </div>
            """
            st.markdown(donation_html, unsafe_allow_html=True)