Update app.py
Browse files
app.py
CHANGED
|
@@ -2,30 +2,75 @@ import streamlit as st
|
|
| 2 |
from diffusers import StableDiffusionPipeline
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
| 6 |
@st.cache_resource
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
pipe = StableDiffusionPipeline.from_pretrained(
|
| 10 |
-
model_id,
|
| 11 |
-
torch_dtype=torch.float32,
|
| 12 |
-
use_safetensors=True
|
| 13 |
-
)
|
| 14 |
-
return pipe.to("cpu") # if you're using CPU on Hugging Face Spaces
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
st.set_page_config(page_title="Love Text to Image", layout="centered")
|
| 19 |
-
st.title("
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
st.
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
if st.button("Generate Image"):
|
| 26 |
-
if
|
| 27 |
-
st.warning("Please
|
| 28 |
else:
|
| 29 |
with st.spinner("Generating image..."):
|
| 30 |
-
image =
|
| 31 |
-
st.image(image, caption="Generated
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from diffusers import StableDiffusionPipeline
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
+
import tempfile
|
| 6 |
+
import whisper
|
| 7 |
+
import os
|
| 8 |
+
from realesrgan import RealESRGAN
|
| 9 |
|
| 10 |
+
# Load Whisper for voice-to-text
|
| 11 |
@st.cache_resource
|
| 12 |
+
def load_whisper():
|
| 13 |
+
return whisper.load_model("base")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Load Stable Diffusion models
|
| 16 |
+
@st.cache_resource
|
| 17 |
+
def load_pipelines():
|
| 18 |
+
pipelines = {
|
| 19 |
+
"Realistic": StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float32),
|
| 20 |
+
"Anime": StableDiffusionPipeline.from_pretrained("andite/anything-v4.0", torch_dtype=torch.float32),
|
| 21 |
+
"Ghibli": StableDiffusionPipeline.from_pretrained("nitrosocke/Ghibli-Diffusion", torch_dtype=torch.float32)
|
| 22 |
+
}
|
| 23 |
+
for key in pipelines:
|
| 24 |
+
pipelines[key] = pipelines[key].to("cpu")
|
| 25 |
+
return pipelines
|
| 26 |
+
|
| 27 |
+
# Load Real-ESRGAN for enhancement
|
| 28 |
+
@st.cache_resource
|
| 29 |
+
def load_enhancer():
|
| 30 |
+
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 31 |
+
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64,
|
| 32 |
+
num_block=23, num_grow_ch=32, scale=4)
|
| 33 |
+
return RealESRGAN(device=torch.device("cpu"), scale=4).load_weights(model)
|
| 34 |
+
|
| 35 |
+
whisper_model = load_whisper()
|
| 36 |
+
pipes = load_pipelines()
|
| 37 |
+
enhancer = load_enhancer()
|
| 38 |
|
| 39 |
st.set_page_config(page_title="Love Text to Image", layout="centered")
|
| 40 |
+
st.title("π Love Text to Image Generator")
|
| 41 |
+
|
| 42 |
+
st.markdown("Write or speak a romantic poem and turn it into beautiful art β¨")
|
| 43 |
|
| 44 |
+
style = st.selectbox("Choose art style", ["Realistic", "Anime", "Ghibli"])
|
| 45 |
|
| 46 |
+
tab1, tab2 = st.tabs(["π Text Input", "π€ Voice Input"])
|
| 47 |
+
|
| 48 |
+
prompt = ""
|
| 49 |
+
|
| 50 |
+
with tab1:
|
| 51 |
+
prompt = st.text_area("Enter your romantic poem or message")
|
| 52 |
+
|
| 53 |
+
with tab2:
|
| 54 |
+
audio_file = st.file_uploader("Upload your audio file (mp3, wav)", type=["mp3", "wav"])
|
| 55 |
+
if audio_file is not None:
|
| 56 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
|
| 57 |
+
tmp.write(audio_file.read())
|
| 58 |
+
tmp_path = tmp.name
|
| 59 |
+
with st.spinner("Transcribing..."):
|
| 60 |
+
result = whisper_model.transcribe(tmp_path)
|
| 61 |
+
prompt = result["text"]
|
| 62 |
+
st.success("Transcription Complete!")
|
| 63 |
+
st.markdown(f"**Transcribed Text:** {prompt}")
|
| 64 |
+
os.remove(tmp_path)
|
| 65 |
|
| 66 |
if st.button("Generate Image"):
|
| 67 |
+
if prompt.strip() == "":
|
| 68 |
+
st.warning("Please enter or upload a love message.")
|
| 69 |
else:
|
| 70 |
with st.spinner("Generating image..."):
|
| 71 |
+
image = pipes[style](prompt).images[0]
|
| 72 |
+
st.image(image, caption="Original Generated Image", use_column_width=True)
|
| 73 |
+
|
| 74 |
+
with st.spinner("Enhancing image..."):
|
| 75 |
+
enhanced = enhancer.predict(image)
|
| 76 |
+
st.image(enhanced, caption="Enhanced Image", use_column_width=True)
|