meraj12 commited on
Commit
4817e04
·
verified ·
1 Parent(s): fae9c20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -26
app.py CHANGED
@@ -1,31 +1,76 @@
1
- import streamlit as st
2
- import requests
 
 
 
3
 
4
- # Streamlit UI for uploading content
5
- st.title("Facebook Auto-Post Scheduler")
 
 
 
 
6
 
7
- # User input for content
8
- message = st.text_area("Message for Post", "Hello, this is an automated post!")
9
- file = st.file_uploader("Upload an Image or Video", type=["jpg", "jpeg", "png", "mp4"])
10
 
11
- # Replace with your ngrok URL from Colab
12
- COLAB_URL = 'https://your-colab-url.ngrok.io' # Example: https://abc123.ngrok.io
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- if st.button("Schedule Post"):
15
- if file:
16
- # Upload media to backend API (Colab)
17
- files = {'file': file.getvalue()}
18
- response = requests.post(f"{COLAB_URL}/upload", files=files)
19
- if response.status_code == 200:
20
- st.success("Media uploaded successfully!")
21
- else:
22
- st.error(f"Failed to upload media: {response.text}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- if message:
25
- # Send message to backend API (Colab)
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()