gaspar-avit commited on
Commit
6f7d10f
·
1 Parent(s): b4e5a30

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -21
app.py CHANGED
@@ -16,10 +16,21 @@ from sentence_transformers import SentenceTransformer
16
  from diffusers import DiffusionPipeline
17
 
18
 
19
- # Environment variables
 
 
 
 
 
 
 
 
 
 
20
  os.environ['KAGGLE_USERNAME'] = st.secrets['username']
21
  os.environ['KAGGLE_KEY'] = st.secrets['key']
22
- #os.environ['KAGGLE_CONFIG_DIR'] = '/home/appuser/.kaggle/kaggle.json'
 
23
  from kaggle.api.kaggle_api_extended import KaggleApi
24
 
25
 
@@ -48,10 +59,13 @@ def load_dataset():
48
 
49
  return data
50
 
51
- @st.cache
52
  def load_model():
53
- #return DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
54
- return DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
 
 
 
55
 
56
 
57
  def query_summarization(text):
@@ -79,12 +93,14 @@ def generate_poster(movie_data):
79
  synopsis_sum = query_summarization(movie_data.overview.values[0])
80
  st.text(synopsis_sum)
81
 
 
 
 
 
 
82
  # Get image based on synopsis
83
- pipeline = load_model()
84
- #pipeline = DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-2")
85
-
86
- #image = pipe(prompt).images[0]
87
- #st.image(image, caption=movie_data.title)
88
 
89
  return None #poster_image
90
 
@@ -93,30 +109,25 @@ def generate_poster(movie_data):
93
  ## --- CONNECT TO KAGGLE --- ##
94
  ###############################
95
 
96
- '''
97
- # Authenticate Kaggle account
98
- os.environ['KAGGLE_USERNAME'] = st.secrets['username']
99
- os.environ['KAGGLE_KEY'] = st.secrets['key']
100
- os.environ['KAGGLE_CONFIG_DIR'] = '/home/appuser/.kaggle/kaggle.json'
101
 
 
 
 
 
102
 
103
- api_token = {"username":st.secrets['username'],"key":st.secrets['key']}
104
- with open('/home/appuser/.kaggle/kaggle.json', 'w') as file:
105
- json.dump(api_token, file)
106
- '''
107
 
108
  # Activate Kaggle API
109
  api = KaggleApi()
110
  api.authenticate()
111
 
112
- '''
113
  try:
114
  api.authenticate()
115
  except:
116
  with open('/home/appuser/.kaggle/kaggle.json', 'w') as file:
117
  json.dump(api_token, file)
118
  api.authenticate()
119
- '''
120
 
121
 
122
  ###############################
 
16
  from diffusers import DiffusionPipeline
17
 
18
 
19
+ ###############################
20
+ ## --- GLOBAL VARIABLES ---- ##
21
+ ###############################
22
+
23
+
24
+ IS_MODEL_LOADED = False
25
+ PATH_JSON = '/home/user/app/.kaggle/kaggle.json'
26
+
27
+
28
+
29
+ # Environment variables to authenticate Kaggle account
30
  os.environ['KAGGLE_USERNAME'] = st.secrets['username']
31
  os.environ['KAGGLE_KEY'] = st.secrets['key']
32
+ os.environ['KAGGLE_CONFIG_DIR'] = PATH_JSON
33
+
34
  from kaggle.api.kaggle_api_extended import KaggleApi
35
 
36
 
 
59
 
60
  return data
61
 
62
+ @st.cache(persist=True, show_spinner=False, allow_output_mutation=True, suppress_st_warning=True)
63
  def load_model():
64
+ model = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
65
+ IS_MODEL_LOADED = True
66
+ return model
67
+ #return DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
68
+ #return DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-2")
69
 
70
 
71
  def query_summarization(text):
 
93
  synopsis_sum = query_summarization(movie_data.overview.values[0])
94
  st.text(synopsis_sum)
95
 
96
+ # Load text-to-image model
97
+ if not IS_MODEL_LOADED:
98
+ with st.spinner("Loading Text to Image model..."):
99
+ pipeline = load_model()
100
+
101
  # Get image based on synopsis
102
+ image = pipeline(prompt).images[0]
103
+ st.image(image, caption=movie_data.title)
 
 
 
104
 
105
  return None #poster_image
106
 
 
109
  ## --- CONNECT TO KAGGLE --- ##
110
  ###############################
111
 
 
 
 
 
 
112
 
113
+ if not os.path.exists(PATH_JSON):
114
+ api_token = {"username":st.secrets['username'],"key":st.secrets['key']}
115
+ with open(PATH_JSON, 'w') as file:
116
+ json.dump(api_token, file)
117
 
 
 
 
 
118
 
119
  # Activate Kaggle API
120
  api = KaggleApi()
121
  api.authenticate()
122
 
123
+
124
  try:
125
  api.authenticate()
126
  except:
127
  with open('/home/appuser/.kaggle/kaggle.json', 'w') as file:
128
  json.dump(api_token, file)
129
  api.authenticate()
130
+
131
 
132
 
133
  ###############################