14likhit commited on
Commit
b9dd8bb
·
verified ·
1 Parent(s): 0c130dc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from diffusers import DiffusionPipeline
3
+ import torch
4
+
5
+ # Set page config
6
+ st.set_page_config(
7
+ page_title="Portrait Generator",
8
+ page_icon="🖼️",
9
+ layout="centered"
10
+ )
11
+
12
+ # App title and description
13
+ st.title("AI Portrait Generator")
14
+ st.markdown("Generate beautiful portraits using the AWPortraitCN2 model")
15
+
16
+ # Model parameters
17
+ with st.sidebar:
18
+ st.header("Generation Settings")
19
+ steps = st.slider("Inference Steps", min_value=20, max_value=100, value=40)
20
+ guidance_scale = st.slider("Guidance Scale", min_value=1.0, max_value=15.0, value=7.5, step=0.5)
21
+ negative_prompt = st.text_area(
22
+ "Negative Prompt",
23
+ value="lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, watermark, signature, out of frame"
24
+ )
25
+ seed = st.number_input("Random Seed (leave at -1 for random)", min_value=-1, value=-1)
26
+
27
+ # Main prompt input
28
+ prompt = st.text_area(
29
+ "Describe the portrait you want to generate",
30
+ value="Masterpiece portrait of a beautiful young woman with flowing hair, detailed face, photorealistic, 8k, professional photography"
31
+ )
32
+
33
+ # Generate button
34
+ if st.button("Generate Portrait", type="primary"):
35
+ with st.spinner("Loading model and generating portrait..."):
36
+ try:
37
+ # Set up the model pipeline
38
+ pipeline = DiffusionPipeline.from_pretrained(
39
+ "Shakker-Labs/AWPortraitCN2",
40
+ torch_dtype=torch.float16,
41
+ use_safetensors=True
42
+ )
43
+
44
+ # Move to GPU if available
45
+ device = "cuda" if torch.cuda.is_available() else "cpu"
46
+ pipeline = pipeline.to(device)
47
+
48
+ # Set seed if specified
49
+ generator = None
50
+ if seed != -1:
51
+ generator = torch.Generator(device).manual_seed(seed)
52
+
53
+ # Generate the image
54
+ image = pipeline(
55
+ prompt=prompt,
56
+ negative_prompt=negative_prompt,
57
+ num_inference_steps=steps,
58
+ guidance_scale=guidance_scale,
59
+ generator=generator
60
+ ).images[0]
61
+
62
+ # Display the generated image
63
+ st.image(image, caption="Generated Portrait", use_column_width=True)
64
+
65
+ # Option to download
66
+ # Convert the PIL image to bytes
67
+ import io
68
+ from PIL import Image
69
+ buf = io.BytesIO()
70
+ image.save(buf, format="PNG")
71
+ byte_im = buf.getvalue()
72
+
73
+ st.download_button(
74
+ label="Download Portrait",
75
+ data=byte_im,
76
+ file_name="generated_portrait.png",
77
+ mime="image/png"
78
+ )
79
+
80
+ except Exception as e:
81
+ st.error(f"An error occurred: {e}")
82
+ st.info("Make sure you have enough GPU memory and the required dependencies installed.")
83
+
84
+ # Add requirements info at the bottom
85
+ st.markdown("---")
86
+ st.markdown("""
87
+ ### Requirements
88
+ To run this app, you need:
89
+ - diffusers
90
+ - transformers
91
+ - accelerate
92
+ - torch
93
+ - streamlit
94
+
95
+ Install with: `pip install diffusers transformers accelerate torch streamlit`
96
+ """)