File size: 18,331 Bytes
1302286
 
 
 
46e790e
4fc9ed4
 
4a5fd46
 
4fc9ed4
4a5fd46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e709466
 
 
 
4a5fd46
 
 
 
 
 
 
 
b6871ac
4fc9ed4
4a5fd46
4fc9ed4
 
 
4a5fd46
4fc9ed4
 
 
 
b6871ac
4fc9ed4
 
b6871ac
 
 
 
 
 
 
 
 
4fc9ed4
 
4a5fd46
 
4fc9ed4
 
 
 
 
 
 
 
2c14c49
4fc9ed4
 
 
 
 
 
 
 
 
46e790e
4a5fd46
 
 
46e790e
1302286
4a5fd46
1302286
 
6befe96
bd36be8
 
1302286
4a5fd46
b6871ac
4fc9ed4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1302286
 
46e790e
1302286
4a5fd46
b6871ac
4fc9ed4
46e790e
4a5fd46
4fc9ed4
 
 
 
 
 
46e790e
4fc9ed4
 
 
 
4a5fd46
4fc9ed4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a5fd46
4fc9ed4
 
 
 
 
46e790e
 
 
 
4fc9ed4
 
b6871ac
4fc9ed4
 
 
 
 
e709466
4fc9ed4
 
e709466
 
4fc9ed4
 
e709466
4fc9ed4
 
4a5fd46
4fc9ed4
e709466
4a5fd46
4fc9ed4
 
 
 
 
4a5fd46
 
 
 
4fc9ed4
 
 
 
 
 
 
 
 
 
 
 
e709466
4fc9ed4
 
4a5fd46
4fc9ed4
 
 
 
 
b6871ac
4fc9ed4
e709466
 
4fc9ed4
 
e709466
4fc9ed4
 
 
 
 
 
e709466
4fc9ed4
e709466
4fc9ed4
 
e709466
4fc9ed4
 
e709466
4fc9ed4
 
 
 
 
 
e709466
4fc9ed4
e709466
b6871ac
4a5fd46
4fc9ed4
 
4a5fd46
 
 
4fc9ed4
 
 
 
 
 
 
 
 
 
 
 
 
 
4a5fd46
 
 
4fc9ed4
4a5fd46
4fc9ed4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46e790e
 
 
4a5fd46
 
 
46e790e
b6871ac
4fc9ed4
b6871ac
4fc9ed4
 
 
b2a7d1e
4fc9ed4
 
 
 
 
 
 
 
 
b2a7d1e
4fc9ed4
 
 
 
 
 
b6871ac
4fc9ed4
b6871ac
b2a7d1e
 
e709466
 
b2a7d1e
b6871ac
b2a7d1e
 
b6871ac
 
 
 
 
 
 
 
 
 
b2a7d1e
b6871ac
4fc9ed4
 
b6871ac
4fc9ed4
 
 
 
 
 
b2a7d1e
 
 
e709466
 
b2a7d1e
 
 
 
 
b6871ac
 
 
 
b2a7d1e
b6871ac
 
 
 
b2a7d1e
b6871ac
4fc9ed4
 
 
 
 
 
b2a7d1e
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#import libraries
import streamlit as st
from PIL import Image
import io
from openai import OpenAI
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from text_generation import Client
from huggingface_hub import InferenceClient
import config_llm

# Initialize session state variables
if 'user_input' not in st.session_state:
    st.session_state['user_input'] = ""
if 'simplified_text' not in st.session_state:
    st.session_state['simplified_text'] = ''
if 'new_caption' not in st.session_state:
    st.session_state['new_caption'] = None
if 'model_clip' not in st.session_state:
    st.session_state['model_clip'] = None
if 'transform_clip' not in st.session_state:
    st.session_state['transform_clip'] = None
if 'openai_api_key' not in st.session_state:
    st.session_state['openai_api_key'] = ''
if 'huggingface_key' not in st.session_state:
    st.session_state['huggingface_key'] = ''
if 'message_content_from_caption' not in st.session_state:
    st.session_state['message_content_from_caption'] = ''
if 'message_content_from_simplified_text' not in st.session_state:
    st.session_state['message_content_from_simplified_text'] = ''
if 'mixtral_from_caption' not in st.session_state:
    st.session_state['mixtral_from_caption'] = ''
if 'mixtral_from_simplified' not in st.session_state:
    st.session_state['mixtral_from_simplified'] = ''
if 'image_from_caption' not in st.session_state:
    st.session_state['image_from_caption'] = None
if 'image_from_simplified_text' not in st.session_state:
    st.session_state['image_from_simplified_text'] = None
if 'image_from_press_text' not in st.session_state:
    st.session_state['image_from_press_text'] = None
if 'image_from_press_text_from_caption' not in st.session_state:
    st.session_state['image_from_press_text_from_caption'] = None


# Load the tokenizer and simplifier model
tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-small-finetuned-text-simplification")
model = AutoModelForSeq2SeqLM.from_pretrained("mrm8488/t5-small-finetuned-text-simplification")

# Function to simplify text
def simplify_text(input_text):
    # Tokenize and encode the input text
    input_ids = tokenizer.encode("simplify: " + input_text, return_tensors="pt")
    # Generate the simplified text
    output = model.generate(input_ids, min_length=5, max_length=80, do_sample=True)
    # Decode the simplified text
    simplified_text = tokenizer.decode(output[0], skip_special_tokens=True)
    # Post-process to ensure the output ends with a complete sentence
    # Find the last period, question mark, or exclamation point
    last_valid_ending = max(simplified_text.rfind('.'), simplified_text.rfind('?'), simplified_text.rfind('!'))
    if last_valid_ending != -1:
        # Ensure the output ends with the last complete sentence
        cleaned_text = simplified_text[:last_valid_ending+1]
    else:
        # No sentence ending found; return the whole text or handle as appropriate
        cleaned_text = simplified_text
    return cleaned_text


# Define the path to example text
example_text_path = "example_text.txt"

# Function to load example text from a file
def load_example_text():
    with open(example_text_path, "r", encoding="utf-8") as file:
        return file.read()
    
# Define the path to your example image
example_image_path = "example.jpg"

# Function to load image from file
def load_image(image_path):
    with open(image_path, "rb") as file:
        # Open the image using PIL
        img = Image.open(file)
        # Load the image data into memory
        img.load()
    return img   

#get huggingface key
st.session_state['huggingface_key'] = st.secrets["hf_key"]
client = InferenceClient(token=st.session_state['huggingface_key'])


########################################################################

# Create a Streamlit app
st.title("ARTSPEAK  >  s i m p l i f i e r")

st.markdown("---")

# Create a sub-section for uploading the files
with st.expander("Upload Files"):
        st.markdown("## Upload Text and Image")
        ##### Upload of files
        st.write("Paste your text here or upload example:")
        # Add a button to load example text into the text area
        if st.button('Load Example Text'):
            # Update the session state for user input with the example text
            st.session_state['user_input'] = load_example_text()
        # Add a text input field for user input
        # Directly use session state variable for the value parameter
        user_input = st.text_area("Enter text here", value=st.session_state['user_input'])

        st.markdown("---")
        
        # Load and display example image separately and save for further use
        if st.button("Load Example Image"):
            st.session_state['example_image'] = load_image(example_image_path)
            st.image(st.session_state['example_image'], caption="Example Image")

        # Displaying the file uploader
        uploaded_image = st.file_uploader("Upload an image (jpg or png)", type=["jpg", "png"])


st.markdown("---")

#### Simplifier and Image Caption
with st.expander("Simplify Text and Image"):
    st.markdown("## 'Simplify' Text and Image")

    ## Text simplifier
    if st.button("Simplify the Input Text"):
        if user_input:
            simplified_text = simplify_text(user_input)
            st.session_state['simplified_text'] = simplified_text
        else:
            st.warning("Please enter text in the input field before clicking 'Save'")

    # Display the simplified text from session state
    if st.session_state['simplified_text']:
        st.write(st.session_state['simplified_text'])

    ## Get new caption
    # Button to get new caption
    if st.button("Get New Caption for Image"):
        # Initialize image data variable
        image_data = None

        # Check if the user has uploaded an image
        if uploaded_image is not None:
            image_data = uploaded_image.getvalue()
        # If not, check if the example image has been loaded
        elif 'example_image' in st.session_state:
            # Convert PIL Image to bytes for example image
            buffer = io.BytesIO()
            st.session_state['example_image'].save(buffer, format="PNG")
            buffer.seek(0)
            image_data = buffer.getvalue()

        # If we have image data, get the caption
        if image_data is not None:
            try:
                # Generate the caption (make sure to send the image in the correct format expected by your API)
                caption = client.image_to_text(image_data)
                # Update the session state
                st.session_state['new_caption'] = caption
                st.write(st.session_state['new_caption'])

            except Exception as e:
                st.error(f"An error occurred: {e}")
        else:
            st.warning("Please upload an image or load the example image before clicking 'Get New Caption for Image'")


st.markdown("---")

########################################################################

with st.expander("Press Text Generation"):
    st.markdown("## Generate New Presstext for an Exhibition")

    # Define radio button options
    option = st.radio(
        "Choose a Language Model:",
        ('Mixtral 8x7B', 'GPT-3.5 Turbo'))

    # Conditional logic based on radio button choice
    if option == 'Mixtral 8x7B':
        st.header("Mixtral 8x7B")

        ############
        ###Mixtral##
        ############

        headers = {"Authorization": f"Bearer {st.session_state['huggingface_key']}"}

        client_mixtral = Client(
            config_llm.API_URL,
            headers=headers,
        )

        def run_single_input(
            message: str,
            system_prompt: str = config_llm.DEFAULT_SYSTEM_PROMPT,
            max_new_tokens: int = config_llm.MAX_NEW_TOKENS,
            temperature: float = config_llm.TEMPERATURE,
            top_p: float = config_llm.TOP_P
        ) -> str:
            """
            Run the model for a single input and return a single output.
            """
            prompt = f"{system_prompt}\n\nUser: {message.strip()}\n"

            generate_kwargs = dict(
                max_new_tokens=max_new_tokens,
                do_sample=True,
                top_p=top_p,
                temperature=temperature,
            )
            stream = client_mixtral.generate_stream(prompt, **generate_kwargs)
            output = ""
            for response in stream:
                if any([end_token in response.token.text for end_token in [config_llm.EOS_STRING, config_llm.EOT_STRING]]):
                    break  # Stop at the first end token
                else:
                    output += response.token.text

            return output.strip()  # Return the complete output
        

        # Button to generate press text from new caption from Mixtral
        if st.button("Generate Press Text from New Image Caption with Mixtral"):
            if st.session_state['new_caption']:
                try:
                    st.session_state['mixtral_from_caption'] = run_single_input(st.session_state['new_caption'], config_llm.DEFAULT_SYSTEM_PROMPT)
                except Exception as e:
                    st.error(f"An error occurred: {e}")
            else:
                st.warning("Please ensure a caption is generated.")

        # Display the generated press text from new caption
        if st.session_state['mixtral_from_caption']:
            st.write("Generated Press Text from New Caption of Artwork:")
            st.write(st.session_state['mixtral_from_caption'])

        # Button to generate press text from simplified text
        if st.button("Generate Press Text from Simplified Text with Mixtral"):
            if st.session_state['simplified_text']:
                try:
                    st.session_state['mixtral_from_simplified'] = run_single_input(st.session_state['simplified_text'], config_llm.DEFAULT_SYSTEM_PROMPT)
                except Exception as e:
                    st.error(f"An error occurred: {e}")
            else:
                st.warning("Please ensure simplified text is available.")

        # Display the generated press text from simplified text
        if st.session_state['mixtral_from_simplified']:
            st.write("Generated Press Text from Simplified Text:")
            st.write(st.session_state['mixtral_from_simplified'])
        
    elif option == 'GPT-3.5 Turbo':
        st.header("GPT-3.5")

        ##########
        ##OpenAI##
        #########
        # Add a text input for the OpenAI API key
        api_key_input = st.text_input("Enter your OpenAI API key to continue", type="password")

        # Button to save the API key
        if st.button('Save API Key'):
            st.session_state['openai_api_key'] = api_key_input
            st.success("API Key saved temporarily for this session.")
        st.write("-  -  -")

        # Function to get completion from OpenAI API
        def get_openai_completion(api_key, prompt_message):
            client = OpenAI(api_key=api_key,)
            completion = client.chat.completions.create(
                model="gpt-3.5-turbo",
                max_tokens=config_llm.MAX_NEW_TOKENS,
                temperature = config_llm.TEMPERATURE,
                top_p = config_llm.TOP_P,
                messages=[
                    {"role": "system", "content": config_llm.DEFAULT_SYSTEM_PROMPT},
                    {"role": "user", "content": prompt_message}
                ]
            )
            return completion.choices[0].message.content

        # Button to generate press text from new caption
        if st.button("Generate Press Text from New Image Caption with GPT"):
            if st.session_state['new_caption'] and st.session_state['openai_api_key']:
                try:
                    st.session_state['message_content_from_caption'] = get_openai_completion(st.session_state['openai_api_key'], st.session_state['new_caption'])
                except Exception as e:
                    st.error(f"An error occurred: {e}")
            else:
                st.warning("Please ensure a caption is generated and an API key is entered.")

        # Display the generated press text from new caption
        if st.session_state['message_content_from_caption']:
            st.write("Generated Press Text from New Caption of Artwork:")
            st.write(st.session_state['message_content_from_caption'])

        # Button to generate press text from simplified text
        if st.button("Generate Press Text from Simplified Text with GPT"):
            if st.session_state['simplified_text'] and st.session_state['openai_api_key']:
                try:
                    st.session_state['message_content_from_simplified_text'] = get_openai_completion(st.session_state['openai_api_key'], st.session_state['simplified_text'])
                except Exception as e:
                    st.error(f"An error occurred: {e}")
            else:
                st.warning("Please ensure simplified text is available and an API key is entered.")

        # Display the generated press text from simplified text
        if st.session_state['message_content_from_simplified_text']:
            st.write("Generated Press Text from Simplified Text:")
            st.write(st.session_state['message_content_from_simplified_text'])


st.markdown("---")

########################################################################

## Image Generation Interface

with st.expander("Image Generation"):
    st.markdown("## Generate new Images from Texts")
    # Button to generate image from new caption
    if st.button("Generate Image from New Caption of Artwork"):
        if st.session_state['new_caption']:
            prompt_caption = f"contemporary art of {st.session_state['new_caption']}"
            st.session_state['image_from_caption'] = client.text_to_image(prompt_caption, model="prompthero/openjourney-v4")

    # Display the image generated from new caption
    if st.session_state['image_from_caption'] is not None:
        st.image(st.session_state['image_from_caption'], caption="Image from New Caption", use_column_width=True)

    # Button to generate image from simplified text
    if st.button("Generate Image from Simplified Text"):
        if st.session_state['simplified_text']:
            prompt_summary = f"contemporary art of {st.session_state['simplified_text']}"
            st.session_state['image_from_simplified_text'] = client.text_to_image(prompt_summary, model="prompthero/openjourney-v4")

    # Display the image generated from simplified text
    if st.session_state['image_from_simplified_text'] is not None:
        st.image(st.session_state['image_from_simplified_text'], caption="Image from Simplified Text", use_column_width=True)

    # Button to generate image from press text from simplified text

    if st.button("Generate Image from new Press Text from Simplified Text"):
        text_to_use_simp = None

        # Check which variable is available and set it to text_to_use
        if 'mixtral_from_simplified' in st.session_state and st.session_state['mixtral_from_simplified']:
            text_to_use_simp = st.session_state['mixtral_from_simplified']
        elif 'message_content_from_simplified_text' in st.session_state and st.session_state['message_content_from_simplified_text']:
            text_to_use_simp = st.session_state['message_content_from_simplified_text']

        # Use the available text to generate the image
        if text_to_use_simp:
            # Check for length of the text and truncate if necessary
            if len(text_to_use_simp) > 509:  # Adjust based on your model's max length (512-3)
                text_to_use_simp = text_to_use_simp[:509]  # Truncate the text

            prompt_press_text_simple = f"contemporary art of {text_to_use_simp}"
            try:
                st.session_state['image_from_press_text'] = client.text_to_image(prompt_press_text_simple, model="prompthero/openjourney-v4")
            except Exception as e:
                st.error("Failed to generate image: " + str(e))
        else:
            st.error("First generate a press text from summary.")

    # Display the image generated from press text from simplified text
    if 'image_from_press_text' in st.session_state and st.session_state['image_from_press_text'] is not None:
        st.image(st.session_state['image_from_press_text'], 
                caption="Image from Press Text from simplified Text", 
                use_column_width=True)

    # Button to generate image from press text from caption
    if st.button("Generate Image from new Press Text from new Caption"):
        # Initialize the variable
        text_to_use_cap = None
        # Check which variable is available and set it to text_to_use
        if 'mixtral_from_caption' in st.session_state and st.session_state['mixtral_from_caption']:
            text_to_use_cap = st.session_state['mixtral_from_caption']
        elif 'message_content_from_caption' in st.session_state and st.session_state['message_content_from_caption']:
            text_to_use_cap = st.session_state['message_content_from_caption']

        # Use the available text to generate the image
        if text_to_use_cap:
            # Check for length of the text and truncate if necessary
            if len(text_to_use_cap) > 509:  # Adjust based on your model's max length
                text_to_use_cap = text_to_use_cap[:509]  # Truncate the text

            prompt_press_text_caption = f"contemporary art of {text_to_use_cap}"
            try:
                st.session_state['image_from_press_text_from_caption'] = client.text_to_image(prompt_press_text_caption, model="prompthero/openjourney-v4")
            except Exception as e:
                st.error("Failed to generate image: " + str(e))
        else:
            st.error("First generate a press text from summary.")

    # Display the image generated from press text from caption
    if st.session_state['image_from_press_text_from_caption'] is not None:
        st.image(st.session_state['image_from_press_text_from_caption'], 
                caption="Image from Press Text from new Caption", 
                use_column_width=True)
        
st.markdown("---")