Saleh-Asaad commited on
Commit
9d826e2
·
verified ·
1 Parent(s): 5969b19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -28
app.py CHANGED
@@ -1,36 +1,28 @@
1
  import streamlit as st
2
- import requests
 
3
  from PIL import Image
4
- from io import BytesIO
5
 
6
- # Hugging Face API config
7
- API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-3.5-large"
8
- API_TOKEN = "YOUR TOKEN"
9
- HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
 
 
 
 
 
 
10
 
11
- # Function to send prompt to HF and get image
12
- def generate_image(prompt):
13
- payload = {
14
- "inputs": prompt,
15
- "options": {"wait_for_model": True}
16
- }
17
- response = requests.post(API_URL, headers=HEADERS, json=payload)
18
- if response.status_code == 200:
19
- return Image.open(BytesIO(response.content))
20
- else:
21
- st.error(f"Error {response.status_code}: {response.text}")
22
- return None
23
 
24
  # --- Streamlit UI ---
25
- st.set_page_config(page_title="SD 3.5 Image Generator", page_icon="🖼️")
26
- st.title("🖼️ Text to Image Generator with Stable Diffusion 3.5")
27
- st.markdown("Generate images using `stabilityai/stable-diffusion-3.5-large` via Hugging Face")
28
-
29
- # Prompt input
30
- prompt = st.text_input("Enter your prompt here")
31
 
32
  if st.button("Generate Image") and prompt:
33
- with st.spinner("Generating image..."):
34
- image = generate_image(prompt)
35
- if image:
36
- st.image(image, caption="Generated Image", use_column_width=True)
 
1
  import streamlit as st
2
+ from diffusers import StableDiffusionPipeline
3
+ import torch
4
  from PIL import Image
 
5
 
6
+ # Load the model locally (first time will auto-download)
7
+ @st.cache_resource
8
+ def load_pipeline():
9
+ pipe = StableDiffusionPipeline.from_pretrained(
10
+ "stabilityai/stable-diffusion-3.5-large",
11
+ torch_dtype=torch.float16,
12
+ variant="fp16",
13
+ )
14
+ pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
15
+ return pipe
16
 
17
+ pipe = load_pipeline()
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  # --- Streamlit UI ---
20
+ st.set_page_config(page_title="Local SD 3.5 Generator", page_icon="🧠")
21
+ st.title("🎨 Local Image Generator (SD 3.5)")
22
+ prompt = st.text_input("Enter your prompt here:")
 
 
 
23
 
24
  if st.button("Generate Image") and prompt:
25
+ with st.spinner("Generating..."):
26
+ image = pipe(prompt).images[0]
27
+ st.image(image, caption="Generated by Stable Diffusion 3.5")
28
+