Fanu2 commited on
Commit
b6d6a51
ยท
verified ยท
1 Parent(s): a018a8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py CHANGED
@@ -12,6 +12,74 @@ st.title("๐Ÿ–ผ๏ธ Anime & Cartoon Stylizer with OpenVINO")
12
  persona = st.radio("Choose your vibe:", ["Classic", "ChatBoy ๐Ÿ’˜", "Poetic ๐ŸŒธ"], horizontal=True)
13
  style = st.selectbox("Choose your style:", ["AnimeGAN", "CartoonGAN"])
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  @st.cache_resource
16
  def load_model(style_name):
17
  ie = Core()
 
12
  persona = st.radio("Choose your vibe:", ["Classic", "ChatBoy ๐Ÿ’˜", "Poetic ๐ŸŒธ"], horizontal=True)
13
  style = st.selectbox("Choose your style:", ["AnimeGAN", "CartoonGAN"])
14
 
15
+ @st.cache_resource
16
+ def load_model(style_name):
17
+ ie = Core()
18
+ model_path = "animegan.xml" if style_name == "AnimeGAN" else "cartoongan.xml"
19
+ model = ie.read_model(model=model_path)
20
+ compiled_model = ie.compile_model(model=model, device_name="CPU")
21
+ return compiled_model, compiled_model.input(0), compiled_model.output(0)
22
+
23
+ def preprocess(image: Image.Image):
24
+ img = np.array(image.resize((256, 256))).astype(np.float32)
25
+ img = img / 127.5 - 1.0
26
+ img = np.transpose(img, (2, 0, 1))
27
+ return np.expand_dims(img, axis=0)
28
+
29
+ def postprocess(output):
30
+ result = output.squeeze().transpose(1, 2, 0)
31
+ result = (result + 1.0) * 127.5
32
+ return np.clip(result, 0, 255).astype(np.uint8)
33
+
34
+ def get_compliment():
35
+ return random.choice([
36
+ "โœจ You look like the protagonist of a dreamy anime romance.",
37
+ "๐Ÿ’˜ That transformation? Utterly magical.",
38
+ "๐ŸŒธ Your photo just bloomed into a masterpiece.",
39
+ "๐ŸŽ€ If Studio Ghibli saw this, they'd cast you instantly.",
40
+ "๐Ÿซถ This anime version of you? It's giving main character energy."
41
+ ])
42
+
43
+ uploaded_file = st.file_uploader("Upload a photo", type=["jpg", "jpeg", "png"])
44
+ if uploaded_file:
45
+ image = Image.open(uploaded_file).convert("RGB")
46
+ st.subheader("๐Ÿ“ธ Original vs Stylized")
47
+ col1, col2 = st.columns(2)
48
+ col1.image(image, caption="Original", use_column_width=True)
49
+
50
+ if st.button("Generate Style"):
51
+ compiled_model, input_layer, output_layer = load_model(style)
52
+ input_tensor = preprocess(image)
53
+
54
+ # โœ… Correct inference method with list call
55
+ result = compiled_model([input_tensor])[output_layer]
56
+
57
+ result = postprocess(result)
58
+ result_pil = Image.fromarray(result)
59
+ col2.image(result_pil, caption=f"{style} Style", use_column_width=True)
60
+
61
+ buf = BytesIO()
62
+ result_pil.save(buf, format="PNG")
63
+ st.download_button("Download Stylized Image", data=buf.getvalue(), file_name=f"{style.lower()}_style.png", mime="image/png")
64
+
65
+ if persona == "ChatBoy ๐Ÿ’˜":
66
+ st.markdown(f"**{get_compliment()}**")
67
+ elif persona == "Poetic ๐ŸŒธ":
68
+ st.markdown("๐ŸŒ™ *Your image now dances in the moonlight of a painted dream.*")
69
+ import streamlit as st
70
+ import cv2
71
+ import numpy as np
72
+ from PIL import Image
73
+ from io import BytesIO
74
+ from openvino.runtime import Core
75
+ import random
76
+
77
+ st.set_page_config(page_title="Anime & Cartoon Stylizer", layout="wide")
78
+ st.title("๐Ÿ–ผ๏ธ Anime & Cartoon Stylizer with OpenVINO")
79
+
80
+ persona = st.radio("Choose your vibe:", ["Classic", "ChatBoy ๐Ÿ’˜", "Poetic ๐ŸŒธ"], horizontal=True)
81
+ style = st.selectbox("Choose your style:", ["AnimeGAN", "CartoonGAN"])
82
+
83
  @st.cache_resource
84
  def load_model(style_name):
85
  ie = Core()