coztomate commited on
Commit
b6871ac
·
1 Parent(s): b2a7d1e

231229: fix diffusor part

Browse files
Files changed (1) hide show
  1. app.py +58 -30
app.py CHANGED
@@ -6,8 +6,12 @@ from openai import OpenAI
6
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
7
  from text_generation import Client
8
 
 
 
 
 
9
  from huggingface_hub import InferenceClient
10
- client = InferenceClient()
11
 
12
  # load the simplifier model
13
  # Load the tokenizer and model (do this outside the function for efficiency)
@@ -19,13 +23,20 @@ def simplify_text(input_text):
19
  input_ids = tokenizer.encode("simplify: " + input_text, return_tensors="pt")
20
 
21
  # Generate the simplified text
22
- output = model.generate(input_ids, min_length=10, max_length=60, do_sample=True)
23
 
24
  # Decode the simplified text
25
  simplified_text = tokenizer.decode(output[0], skip_special_tokens=True)
26
 
27
- # Remove or replace unwanted tokens like "SEP>"
28
- cleaned_text = simplified_text.replace("SEP>", "")
 
 
 
 
 
 
 
29
 
30
  return cleaned_text
31
 
@@ -68,7 +79,7 @@ if 'message_content_from_simplified_text' not in st.session_state:
68
  st.session_state['message_content_from_simplified_text'] = ''
69
  if 'mistral_from_caption' not in st.session_state:
70
  st.session_state['mistral_from_caption'] = ''
71
- if 'mistral_from_simplified_text' not in st.session_state:
72
  st.session_state['mistral_from_simplified'] = ''
73
  if 'image_from_caption' not in st.session_state:
74
  st.session_state['image_from_caption'] = None
@@ -88,7 +99,7 @@ st.title("ARTSPEAK > s i m p l i f i e r")
88
  st.markdown("---")
89
 
90
  # Create a sub-section
91
- with st.expander("Upload Area"):
92
  st.markdown("## Upload Text and Image")
93
  ##### Upload of files
94
  st.write("Paste your text here or upload example:")
@@ -114,7 +125,7 @@ with st.expander("Upload Area"):
114
  st.markdown("---")
115
 
116
  #### Simplifier
117
- with st.expander("Simplify Space"):
118
  st.markdown("## 'Simplify' Text and Image")
119
 
120
  if st.button("Simplify the Input Text"):
@@ -126,7 +137,6 @@ with st.expander("Simplify Space"):
126
 
127
  # Display the simplified text from session state
128
  if st.session_state['simplified_text']:
129
- st.write("Simplify Original Text:")
130
  st.write(st.session_state['simplified_text'])
131
 
132
 
@@ -166,7 +176,7 @@ st.markdown("---")
166
 
167
  ########################################################################
168
 
169
- with st.expander("Press Text Generation Zone"):
170
  st.markdown("## Generate New Presstext for an Exhibition")
171
 
172
  # Define radio button options
@@ -182,11 +192,7 @@ with st.expander("Press Text Generation Zone"):
182
  ###Mistral##
183
  ############
184
 
185
- # Add a text input for the HF key
186
- hf_key_mistral = st.secrets["hf_key"]
187
-
188
 
189
-
190
  #defaults for Mistral
191
  DEFAULT_SYSTEM_PROMPT = "You will be given a very short description of a contemporary artwork. Please create a complex exhibition press text based on the given artwork description using international art english dealing with post-colonialism, military industrial complex, anthropocene, identity politics and queerness through the language of Rancière, Fontane, Paglen, Deleuze, Steyerl, Spivak, Preciado, Žižek, Foucault and Harraway. Avoid excessive namedropping. Just output press text without explaining your actions."
192
  MAX_MAX_NEW_TOKENS = 4096
@@ -194,9 +200,9 @@ with st.expander("Press Text Generation Zone"):
194
  EOS_STRING = "</s>"
195
  EOT_STRING = "<EOT>"
196
 
197
- model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
198
 
199
- API_URL = "https://api-inference.huggingface.co/models/" + model_id
200
  headers = {"Authorization": f"Bearer {hf_key_mistral}"}
201
 
202
  client_mistral = Client(
@@ -233,6 +239,7 @@ with st.expander("Press Text Generation Zone"):
233
  output += response.token.text
234
 
235
  return output.strip() # Return the complete output
 
236
 
237
  # Button to generate press text from new caption from Mistral
238
  if st.button("Generate Press Text from New Image Caption with Mistral"):
@@ -263,12 +270,11 @@ with st.expander("Press Text Generation Zone"):
263
  if st.session_state['mistral_from_simplified']:
264
  st.write("Generated Press Text from Simplified Text:")
265
  st.write(st.session_state['mistral_from_simplified'])
266
-
267
 
268
 
269
  elif option == 'GPT-3.5 Turbo (needs API Key)':
270
  st.header("GPT-3.5")
271
- # ... additional content for Section 2 ...
272
 
273
  #######
274
  #OpenAI API
@@ -331,9 +337,9 @@ st.markdown("---")
331
  ##Diffusor##
332
  ############
333
 
334
- with st.expander("Image Generation Territory"):
335
  st.markdown("## Generate new Images from Texts")
336
- # Example button to generate image from new caption
337
  if st.button("Generate Image from New Caption of Artwork"):
338
  if st.session_state['new_caption']:
339
  prompt_caption = f"contemporary art of {st.session_state['new_caption']}"
@@ -353,35 +359,50 @@ with st.expander("Image Generation Territory"):
353
  if st.session_state['image_from_simplified_text'] is not None:
354
  st.image(st.session_state['image_from_simplified_text'], caption="Image from Simplified Text", use_column_width=True)
355
 
 
 
 
356
  # Button to generate image from press text from simplified text
 
357
  if st.button("Generate Image from new Press Text from Simplified Text"):
358
- text_to_use = None
359
 
360
  # Check which variable is available and set it to text_to_use
361
  if 'mistral_from_simplified' in st.session_state and st.session_state['mistral_from_simplified']:
362
- text_to_use = st.session_state['mistral_from_simplified']
363
  elif 'message_content_from_simplified_text' in st.session_state and st.session_state['message_content_from_simplified_text']:
364
- text_to_use = st.session_state['message_content_from_simplified_text']
365
 
366
  # Use the available text to generate the image
367
- if text_to_use:
368
- prompt_press_text_simple = f"contemporary art of {text_to_use}"
369
- st.session_state['image_from_press_text'] = client.text_to_image(prompt_press_text_simple, model="prompthero/openjourney-v4")
 
 
 
 
 
 
 
370
  else:
371
- st.error("First generate a caption.")
372
 
373
  # Display the image generated from press text from simplified text
374
- if st.session_state['image_from_press_text'] is not None:
375
  st.image(st.session_state['image_from_press_text'],
376
  caption="Image from Press Text from simplified Text",
377
  use_column_width=True)
378
 
379
 
 
 
 
 
 
380
  # Button to generate image from press text from caption
381
  if st.button("Generate Image from new Press Text from new Caption"):
382
  # Initialize the variable
383
  text_to_use_cap = None
384
-
385
  # Check which variable is available and set it to text_to_use
386
  if 'mistral_from_caption' in st.session_state and st.session_state['mistral_from_caption']:
387
  text_to_use_cap = st.session_state['mistral_from_caption']
@@ -390,10 +411,17 @@ with st.expander("Image Generation Territory"):
390
 
391
  # Use the available text to generate the image
392
  if text_to_use_cap:
 
 
 
 
393
  prompt_press_text_caption = f"contemporary art of {text_to_use_cap}"
394
- st.session_state['image_from_press_text_from_caption'] = client.text_to_image(prompt_press_text_caption, model="prompthero/openjourney-v4")
 
 
 
395
  else:
396
- st.error("First generate a caption.")
397
 
398
  # Display the image generated from press text from caption
399
  if st.session_state['image_from_press_text_from_caption'] is not None:
 
6
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
7
  from text_generation import Client
8
 
9
+
10
+ hf_key_mistral = st.secrets["hf_key"]
11
+
12
+
13
  from huggingface_hub import InferenceClient
14
+ client = InferenceClient(token=hf_key_mistral)
15
 
16
  # load the simplifier model
17
  # Load the tokenizer and model (do this outside the function for efficiency)
 
23
  input_ids = tokenizer.encode("simplify: " + input_text, return_tensors="pt")
24
 
25
  # Generate the simplified text
26
+ output = model.generate(input_ids, min_length=5, max_length=80, do_sample=True)
27
 
28
  # Decode the simplified text
29
  simplified_text = tokenizer.decode(output[0], skip_special_tokens=True)
30
 
31
+ # Post-process to ensure the output ends with a complete sentence
32
+ # Find the last period, question mark, or exclamation point
33
+ last_valid_ending = max(simplified_text.rfind('.'), simplified_text.rfind('?'), simplified_text.rfind('!'))
34
+ if last_valid_ending != -1:
35
+ # Ensure the output ends with the last complete sentence
36
+ cleaned_text = simplified_text[:last_valid_ending+1]
37
+ else:
38
+ # No sentence ending found; return the whole text or handle as appropriate
39
+ cleaned_text = simplified_text
40
 
41
  return cleaned_text
42
 
 
79
  st.session_state['message_content_from_simplified_text'] = ''
80
  if 'mistral_from_caption' not in st.session_state:
81
  st.session_state['mistral_from_caption'] = ''
82
+ if 'mistral_from_simplified' not in st.session_state:
83
  st.session_state['mistral_from_simplified'] = ''
84
  if 'image_from_caption' not in st.session_state:
85
  st.session_state['image_from_caption'] = None
 
99
  st.markdown("---")
100
 
101
  # Create a sub-section
102
+ with st.expander("Upload Files"):
103
  st.markdown("## Upload Text and Image")
104
  ##### Upload of files
105
  st.write("Paste your text here or upload example:")
 
125
  st.markdown("---")
126
 
127
  #### Simplifier
128
+ with st.expander("Simplify Text and Image"):
129
  st.markdown("## 'Simplify' Text and Image")
130
 
131
  if st.button("Simplify the Input Text"):
 
137
 
138
  # Display the simplified text from session state
139
  if st.session_state['simplified_text']:
 
140
  st.write(st.session_state['simplified_text'])
141
 
142
 
 
176
 
177
  ########################################################################
178
 
179
+ with st.expander("Press Text Generation"):
180
  st.markdown("## Generate New Presstext for an Exhibition")
181
 
182
  # Define radio button options
 
192
  ###Mistral##
193
  ############
194
 
 
 
 
195
 
 
196
  #defaults for Mistral
197
  DEFAULT_SYSTEM_PROMPT = "You will be given a very short description of a contemporary artwork. Please create a complex exhibition press text based on the given artwork description using international art english dealing with post-colonialism, military industrial complex, anthropocene, identity politics and queerness through the language of Rancière, Fontane, Paglen, Deleuze, Steyerl, Spivak, Preciado, Žižek, Foucault and Harraway. Avoid excessive namedropping. Just output press text without explaining your actions."
198
  MAX_MAX_NEW_TOKENS = 4096
 
200
  EOS_STRING = "</s>"
201
  EOT_STRING = "<EOT>"
202
 
203
+ model_id_mistral = "mistralai/Mixtral-8x7B-Instruct-v0.1"
204
 
205
+ API_URL = "https://api-inference.huggingface.co/models/" + model_id_mistral
206
  headers = {"Authorization": f"Bearer {hf_key_mistral}"}
207
 
208
  client_mistral = Client(
 
239
  output += response.token.text
240
 
241
  return output.strip() # Return the complete output
242
+
243
 
244
  # Button to generate press text from new caption from Mistral
245
  if st.button("Generate Press Text from New Image Caption with Mistral"):
 
270
  if st.session_state['mistral_from_simplified']:
271
  st.write("Generated Press Text from Simplified Text:")
272
  st.write(st.session_state['mistral_from_simplified'])
273
+
274
 
275
 
276
  elif option == 'GPT-3.5 Turbo (needs API Key)':
277
  st.header("GPT-3.5")
 
278
 
279
  #######
280
  #OpenAI API
 
337
  ##Diffusor##
338
  ############
339
 
340
+ with st.expander("Image Generation"):
341
  st.markdown("## Generate new Images from Texts")
342
+ # Button to generate image from new caption
343
  if st.button("Generate Image from New Caption of Artwork"):
344
  if st.session_state['new_caption']:
345
  prompt_caption = f"contemporary art of {st.session_state['new_caption']}"
 
359
  if st.session_state['image_from_simplified_text'] is not None:
360
  st.image(st.session_state['image_from_simplified_text'], caption="Image from Simplified Text", use_column_width=True)
361
 
362
+
363
+
364
+
365
  # Button to generate image from press text from simplified text
366
+
367
  if st.button("Generate Image from new Press Text from Simplified Text"):
368
+ text_to_use_simp = None
369
 
370
  # Check which variable is available and set it to text_to_use
371
  if 'mistral_from_simplified' in st.session_state and st.session_state['mistral_from_simplified']:
372
+ text_to_use_simp = st.session_state['mistral_from_simplified']
373
  elif 'message_content_from_simplified_text' in st.session_state and st.session_state['message_content_from_simplified_text']:
374
+ text_to_use_simp = st.session_state['message_content_from_simplified_text']
375
 
376
  # Use the available text to generate the image
377
+ if text_to_use_simp:
378
+ # Check for length of the text and truncate if necessary
379
+ if len(text_to_use_simp) > 509: # Adjust based on your model's max length (512-3)
380
+ text_to_use_simp = text_to_use_simp[:509] # Truncate the text
381
+
382
+ prompt_press_text_simple = f"contemporary art of {text_to_use_simp}"
383
+ try:
384
+ st.session_state['image_from_press_text'] = client.text_to_image(prompt_press_text_simple, model="prompthero/openjourney-v4")
385
+ except Exception as e:
386
+ st.error("Failed to generate image: " + str(e))
387
  else:
388
+ st.error("First generate a press text from summary.")
389
 
390
  # Display the image generated from press text from simplified text
391
+ if 'image_from_press_text' in st.session_state and st.session_state['image_from_press_text'] is not None:
392
  st.image(st.session_state['image_from_press_text'],
393
  caption="Image from Press Text from simplified Text",
394
  use_column_width=True)
395
 
396
 
397
+
398
+
399
+
400
+
401
+
402
  # Button to generate image from press text from caption
403
  if st.button("Generate Image from new Press Text from new Caption"):
404
  # Initialize the variable
405
  text_to_use_cap = None
 
406
  # Check which variable is available and set it to text_to_use
407
  if 'mistral_from_caption' in st.session_state and st.session_state['mistral_from_caption']:
408
  text_to_use_cap = st.session_state['mistral_from_caption']
 
411
 
412
  # Use the available text to generate the image
413
  if text_to_use_cap:
414
+ # Check for length of the text and truncate if necessary
415
+ if len(text_to_use_cap) > 509: # Adjust based on your model's max length
416
+ text_to_use_cap = text_to_use_cap[:509] # Truncate the text
417
+
418
  prompt_press_text_caption = f"contemporary art of {text_to_use_cap}"
419
+ try:
420
+ st.session_state['image_from_press_text_from_caption'] = client.text_to_image(prompt_press_text_caption, model="prompthero/openjourney-v4")
421
+ except Exception as e:
422
+ st.error("Failed to generate image: " + str(e))
423
  else:
424
+ st.error("First generate a press text from summary.")
425
 
426
  # Display the image generated from press text from caption
427
  if st.session_state['image_from_press_text_from_caption'] is not None: