meraj12 commited on
Commit
91c303a
Β·
verified Β·
1 Parent(s): 341a155

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -16
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 load_pipeline():
8
- model_id = "runwayml/stable-diffusion-v1-5"
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
- pipe = load_pipeline()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  st.set_page_config(page_title="Love Text to Image", layout="centered")
19
- st.title("πŸ’Œ Love Text to Image Generator")
 
 
20
 
21
- st.markdown("Write a poem or love message, and see it transformed into an image!")
22
 
23
- user_input = st.text_area("Enter your love poem, song, or message here...", height=150)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  if st.button("Generate Image"):
26
- if user_input.strip() == "":
27
- st.warning("Please write something romantic to generate an image!")
28
  else:
29
  with st.spinner("Generating image..."):
30
- image = pipe(user_input).images[0]
31
- st.image(image, caption="Generated from your text πŸ’–", use_column_width=True)
 
 
 
 
 
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)