14likhit commited on
Commit
5655d9c
·
verified ·
1 Parent(s): f5940c8

Updated app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -22
app.py CHANGED
@@ -2,6 +2,9 @@ import streamlit as st
2
  import torch
3
  from PIL import Image
4
  import io
 
 
 
5
 
6
  # Set page config
7
  st.set_page_config(
@@ -14,6 +17,36 @@ st.set_page_config(
14
  st.title("AI Portrait Generator")
15
  st.markdown("Generate beautiful portraits using the AWPortraitCN2 model")
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # Model parameters
18
  with st.sidebar:
19
  st.header("Generation Settings")
@@ -31,27 +64,42 @@ prompt = st.text_area(
31
  value="Masterpiece portrait of a beautiful young woman with flowing hair, detailed face, photorealistic, 8k, professional photography"
32
  )
33
 
34
- # Load the model when the user clicks the button
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  if st.button("Generate Portrait", type="primary"):
36
  with st.spinner("Loading model and generating portrait..."):
37
  try:
38
- # Import here to avoid the cached_download issue until the model is actually needed
39
- from diffusers import DiffusionPipeline
40
 
41
- # Set up the model pipeline
42
- pipeline = DiffusionPipeline.from_pretrained(
43
- "Shakker-Labs/AWPortraitCN2",
44
- torch_dtype=torch.float16,
45
- use_safetensors=True
46
- )
47
-
48
- # Move to GPU if available
49
- device = "cuda" if torch.cuda.is_available() else "cpu"
50
- pipeline = pipeline.to(device)
51
 
52
  # Set seed if specified
53
  generator = None
54
  if seed != -1:
 
55
  generator = torch.Generator(device).manual_seed(seed)
56
 
57
  # Generate the image
@@ -85,13 +133,7 @@ if st.button("Generate Portrait", type="primary"):
85
  # Add requirements info at the bottom
86
  st.markdown("---")
87
  st.markdown("""
88
- ### Requirements
89
- To run this app, you need:
90
- - diffusers
91
- - transformers
92
- - accelerate
93
- - torch
94
- - streamlit
95
-
96
- Install with: `pip install diffusers transformers accelerate torch streamlit`
97
  """)
 
2
  import torch
3
  from PIL import Image
4
  import io
5
+ import os
6
+ import subprocess
7
+ import sys
8
 
9
  # Set page config
10
  st.set_page_config(
 
17
  st.title("AI Portrait Generator")
18
  st.markdown("Generate beautiful portraits using the AWPortraitCN2 model")
19
 
20
+ # Check and install compatible versions if needed
21
+ @st.cache_resource
22
+ def install_dependencies():
23
+ try:
24
+ # Try to import diffusers to see if it works
25
+ import diffusers
26
+ return True
27
+ except ImportError:
28
+ st.warning("Installing required packages. This may take a few minutes...")
29
+ # Install specific versions known to work together
30
+ subprocess.check_call([
31
+ sys.executable, "-m", "pip", "install",
32
+ "huggingface-hub==0.16.4",
33
+ "diffusers==0.20.0",
34
+ "transformers==4.32.0",
35
+ "accelerate==0.21.0"
36
+ ])
37
+ return True
38
+ except Exception as e:
39
+ st.error(f"Failed to install dependencies: {e}")
40
+ return False
41
+
42
+ # Try to install compatible dependencies
43
+ dependencies_installed = install_dependencies()
44
+
45
+ # If dependencies installation failed, show message and exit
46
+ if not dependencies_installed:
47
+ st.error("Could not set up the required environment. Please check the logs.")
48
+ st.stop()
49
+
50
  # Model parameters
51
  with st.sidebar:
52
  st.header("Generation Settings")
 
64
  value="Masterpiece portrait of a beautiful young woman with flowing hair, detailed face, photorealistic, 8k, professional photography"
65
  )
66
 
67
+ # Function to load model with proper dependencies
68
+ @st.cache_resource
69
+ def load_model():
70
+ try:
71
+ from diffusers import StableDiffusionPipeline
72
+
73
+ pipeline = StableDiffusionPipeline.from_pretrained(
74
+ "Shakker-Labs/AWPortraitCN2",
75
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
76
+ use_safetensors=True
77
+ )
78
+
79
+ # Move to GPU if available
80
+ device = "cuda" if torch.cuda.is_available() else "cpu"
81
+ pipeline = pipeline.to(device)
82
+
83
+ return pipeline
84
+ except Exception as e:
85
+ st.error(f"Error loading model: {e}")
86
+ return None
87
+
88
+ # Generate button
89
  if st.button("Generate Portrait", type="primary"):
90
  with st.spinner("Loading model and generating portrait..."):
91
  try:
92
+ # Load the model
93
+ pipeline = load_model()
94
 
95
+ if pipeline is None:
96
+ st.error("Failed to load the model. Please check the logs.")
97
+ st.stop()
 
 
 
 
 
 
 
98
 
99
  # Set seed if specified
100
  generator = None
101
  if seed != -1:
102
+ device = "cuda" if torch.cuda.is_available() else "cpu"
103
  generator = torch.Generator(device).manual_seed(seed)
104
 
105
  # Generate the image
 
133
  # Add requirements info at the bottom
134
  st.markdown("---")
135
  st.markdown("""
136
+ ### About This App
137
+ This app uses the AWPortraitCN2 model to generate AI portraits based on your text prompts.
138
+ Adjust the settings in the sidebar to customize your generation.
 
 
 
 
 
 
139
  """)