Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,76 @@
|
|
1 |
-
import
|
2 |
-
import
|
|
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
data = {'message': message}
|
27 |
-
response = requests.post(f"{COLAB_URL}/post", json=data)
|
28 |
-
if response.status_code == 200:
|
29 |
-
st.success("Post scheduled successfully!")
|
30 |
-
else:
|
31 |
-
st.error(f"Failed to schedule post: {response.text}")
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
|
4 |
+
from PIL import Image
|
5 |
+
import time
|
6 |
|
7 |
+
# Load CPU-optimized model
|
8 |
+
model_id = "OFA-Sys/small-stable-diffusion-v0" # Smaller model for CPU
|
9 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
10 |
+
model_id,
|
11 |
+
torch_dtype=torch.float32 # Force float32 for CPU
|
12 |
+
)
|
13 |
|
14 |
+
# Use DPMSolver for better CPU performance
|
15 |
+
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
16 |
+
pipe = pipe.to("cpu")
|
17 |
|
18 |
+
def generate_image(text):
|
19 |
+
if not text:
|
20 |
+
return None, "Please enter some text first!"
|
21 |
+
|
22 |
+
start_time = time.time()
|
23 |
+
|
24 |
+
try:
|
25 |
+
# Generate with reduced steps for faster processing
|
26 |
+
image = pipe(
|
27 |
+
text,
|
28 |
+
num_inference_steps=20, # Reduced from typical 50 steps
|
29 |
+
guidance_scale=7.5
|
30 |
+
).images[0]
|
31 |
+
|
32 |
+
if image.mode != "RGB":
|
33 |
+
image = image.convert("RGB")
|
34 |
+
|
35 |
+
gen_time = time.time() - start_time
|
36 |
+
return image, f"Generated in {gen_time:.1f} seconds"
|
37 |
+
|
38 |
+
except Exception as e:
|
39 |
+
return None, f"Error: {str(e)}"
|
40 |
|
41 |
+
# Create Gradio interface with loading states
|
42 |
+
with gr.Blocks(title="CPU Poetry to Image") as demo:
|
43 |
+
gr.Markdown("# 💖 CPU-Friendly Poetry to Image")
|
44 |
+
gr.Markdown("Note: Generation may take 2-5 minutes on CPU")
|
45 |
+
|
46 |
+
with gr.Row():
|
47 |
+
with gr.Column():
|
48 |
+
input_text = gr.Textbox(
|
49 |
+
label="Your Romantic Text",
|
50 |
+
placeholder="e.g., 'Your eyes sparkle like stars'",
|
51 |
+
lines=3
|
52 |
+
)
|
53 |
+
generate_btn = gr.Button("Create Magic ✨")
|
54 |
+
|
55 |
+
with gr.Column():
|
56 |
+
output_image = gr.Image(label="Your Generated Art")
|
57 |
+
time_info = gr.Textbox(label="Generation Time")
|
58 |
+
|
59 |
+
examples = gr.Examples(
|
60 |
+
examples=[
|
61 |
+
["A moonlit beach with heart-shaped waves"],
|
62 |
+
["Two roses intertwined with golden light"],
|
63 |
+
["A love letter floating in the clouds"]
|
64 |
+
],
|
65 |
+
inputs=[input_text]
|
66 |
+
)
|
67 |
+
|
68 |
+
generate_btn.click(
|
69 |
+
fn=generate_image,
|
70 |
+
inputs=[input_text],
|
71 |
+
outputs=[output_image, time_info],
|
72 |
+
api_name="generate"
|
73 |
+
)
|
74 |
|
75 |
+
if __name__ == "__main__":
|
76 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|