meraj12 commited on
Commit
7d58c40
Β·
verified Β·
1 Parent(s): 3b7f971

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -77
app.py CHANGED
@@ -1,87 +1,31 @@
1
  import streamlit as st
2
- import torch
3
- import io
4
  from diffusers import StableDiffusionPipeline
 
5
  from PIL import Image
6
 
7
- # Disable warnings and legacy configuration
8
- import warnings
9
- warnings.filterwarnings("ignore")
10
-
11
- # Force CPU-only mode
12
- torch.backends.cudnn.enabled = False
13
- torch.cuda.is_available = lambda: False
14
-
15
- @st.cache_resource(show_spinner=False)
16
- def load_model():
17
- return StableDiffusionPipeline.from_pretrained(
18
- "CompVis/stable-diffusion-v1-4",
19
  torch_dtype=torch.float32,
20
- use_safetensors=False,
21
- use_auth_token=False,
22
- safety_checker=None,
23
- revision="fp16",
24
- local_files_only=False
25
- ).to("cpu")
26
-
27
- def main():
28
- st.set_page_config(page_title="Poetry to Image", page_icon="🎨")
29
- st.title("✨ Romantic Text to Image Converter")
30
-
31
- # Session state management
32
- if 'image' not in st.session_state:
33
- st.session_state.image = None
34
- if 'error' not in st.session_state:
35
- st.session_state.error = None
36
 
37
- # Sidebar controls
38
- with st.sidebar:
39
- st.header("Settings")
40
- steps = st.slider("Inference Steps", 15, 50, 25)
41
- guidance = st.slider("Guidance Scale", 5.0, 15.0, 7.5)
42
 
43
- # Main interface
44
- prompt = st.text_area(
45
- "Enter your romantic text:",
46
- height=150,
47
- placeholder="Example: 'Your eyes shine like stars in the midnight sky...'"
48
- )
49
 
50
- if st.button("Generate Image", type="primary"):
51
- st.session_state.image = None
52
- st.session_state.error = None
53
-
54
- if prompt.strip():
55
- try:
56
- with st.spinner("Creating artwork (5-10 minutes)..."):
57
- pipe = load_model()
58
- image = pipe(
59
- prompt,
60
- num_inference_steps=steps,
61
- guidance_scale=guidance
62
- ).images[0]
63
- st.session_state.image = image
64
- except Exception as e:
65
- st.session_state.error = f"Error: {str(e)}"
66
- else:
67
- st.session_state.error = "Please enter some text!"
68
 
69
- # Display results
70
- if st.session_state.error:
71
- st.error(st.session_state.error)
72
-
73
- if st.session_state.image:
74
- st.image(st.session_state.image, use_column_width=True)
75
-
76
- # Download functionality
77
- buf = io.BytesIO()
78
- st.session_state.image.save(buf, format="PNG")
79
- st.download_button(
80
- "Download Image",
81
- buf.getvalue(),
82
- "poetry_image.png",
83
- "image/png"
84
- )
85
 
86
- if __name__ == "__main__":
87
- main()
 
 
 
 
 
 
1
  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)