AItool's picture
temp_a, temp_b
d4d18b0 verified
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)