|
| 1 | +# Necessary imports |
| 2 | +import os |
| 3 | +from PIL import Image |
| 4 | + |
| 5 | +import streamlit as st |
| 6 | +from streamlit_image_select import image_select |
| 7 | + |
| 8 | +from src.utils import input_image_details |
| 9 | +from src.llm_response import ( |
| 10 | + generate_image_response, |
| 11 | + generate_text_response, |
| 12 | + generate_example_image_response, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +# App title and description |
| 17 | +st.set_page_config(page_title="Streamlit App Builder", page_icon="🛠") |
| 18 | +st.title("🛠 Streamlit App Builder") |
| 19 | +st.header("Build Your Streamlit App with Ease") |
| 20 | +st.info( |
| 21 | + "Welcome to the **Streamlit App Builder**! In this app, you can either **Show** (by providing a preview image) or **Tell** (by providing a text prompt) how you want your Streamlit app to be built. Let's get started!" |
| 22 | +) |
| 23 | + |
| 24 | +# Tabs to switch between "Show" and "Tell" |
| 25 | +tabs = st.tabs(["Show", "Tell"]) |
| 26 | + |
| 27 | + |
| 28 | +# Show how the app should be built or tell how the app should be built |
| 29 | +with tabs[0]: |
| 30 | + # Upload image option |
| 31 | + upload_img = st.toggle("Upload your own preview image") |
| 32 | + |
| 33 | + # If upload image option is selected |
| 34 | + if upload_img: |
| 35 | + st.subheader("Upload your own preview image") |
| 36 | + image_upload = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"]) |
| 37 | + |
| 38 | + # Show uploaded image if any image is uploaded by the user |
| 39 | + if image_upload: |
| 40 | + image = Image.open(image_upload) |
| 41 | + st.image(image, caption="Uploaded Image", use_column_width=True) |
| 42 | + |
| 43 | + # Example images option |
| 44 | + example_img = st.toggle("Try example preview images") |
| 45 | + |
| 46 | + # If example image option is selected |
| 47 | + if example_img: |
| 48 | + st.subheader("Try these example preview images") |
| 49 | + |
| 50 | + # Select image from list of images in images folder |
| 51 | + img = image_select( |
| 52 | + label="Select a preview image", |
| 53 | + images=[ |
| 54 | + "images/streamlit-app-preview-1.png", |
| 55 | + "images/streamlit-app-preview-2.png", |
| 56 | + ], |
| 57 | + ) |
| 58 | + |
| 59 | + # Start LLM process (only if image is uploaded or example image is selected) |
| 60 | + if (not upload_img) and (not example_img): |
| 61 | + start_button = st.button("Build", key="button_image_start", disabled=True) |
| 62 | + else: |
| 63 | + start_button = st.button("Build", key="button_image_start", disabled=False) |
| 64 | + |
| 65 | + # If image is uploaded or example image is selected |
| 66 | + if any([upload_img, example_img]) == True: |
| 67 | + if "img" in locals() or "img" in globals(): |
| 68 | + if start_button: |
| 69 | + |
| 70 | + # Processing the image |
| 71 | + with st.spinner("Processing ..."): |
| 72 | + |
| 73 | + # When image preview 1 is selected |
| 74 | + if img == "images/streamlit-app-preview-1.png": |
| 75 | + st.subheader("Input") |
| 76 | + st.image("images/streamlit-app-preview-1.png") |
| 77 | + preview_image_1 = Image.open( |
| 78 | + "images/streamlit-app-preview-1.png" |
| 79 | + ) |
| 80 | + st.subheader("Output") |
| 81 | + example_image_output_1 = generate_example_image_response( |
| 82 | + preview_image_1 |
| 83 | + ) |
| 84 | + st.write(example_image_output_1) |
| 85 | + |
| 86 | + # When image preview 2 is selected |
| 87 | + if img == "images/streamlit-app-preview-2.png": |
| 88 | + st.subheader("Input") |
| 89 | + st.image("images/streamlit-app-preview-2.png") |
| 90 | + preview_image_2 = Image.open( |
| 91 | + "images/streamlit-app-preview-2.png" |
| 92 | + ) |
| 93 | + st.subheader("Output") |
| 94 | + example_image_output_2 = generate_example_image_response( |
| 95 | + preview_image_2 |
| 96 | + ) |
| 97 | + st.write(example_image_output_2) |
| 98 | + |
| 99 | + # Clear results if "Clear" button is clicked |
| 100 | + if st.button("Clear", key="button_image_clear"): |
| 101 | + os.remove(example_image_output_1) |
| 102 | + os.remove(example_image_output_2) |
| 103 | + |
| 104 | + # If image is uploaded and start button is clicked |
| 105 | + elif image_upload is not None and start_button: |
| 106 | + |
| 107 | + # Processing the image |
| 108 | + with st.spinner("Processing ..."): |
| 109 | + image_data = input_image_details(image_upload) |
| 110 | + |
| 111 | + try: |
| 112 | + # Get the generated output from the model based on the image uploaded |
| 113 | + image_data_output = generate_image_response(image_data) |
| 114 | + # Display the output |
| 115 | + st.subheader("Output") |
| 116 | + st.write(image_data_output) |
| 117 | + |
| 118 | + # Clear results if "Clear" button is clicked |
| 119 | + if st.button("Clear", key="button_image_clear"): |
| 120 | + os.remove(image_data_output) |
| 121 | + |
| 122 | + # Raise error if any error occurs during response generation |
| 123 | + except Exception as e: |
| 124 | + st.error(f"An error occurred: {e}") |
| 125 | + |
| 126 | + else: |
| 127 | + if not image_upload and start_button: |
| 128 | + # Warn user to upload image |
| 129 | + st.warning("Please upload your preview image.") |
| 130 | + |
| 131 | + |
| 132 | +# Tell how the app should be built |
| 133 | +with tabs[1]: |
| 134 | + # Text prompt to describe the app |
| 135 | + text_prompt = st.text_area( |
| 136 | + "Describe details on the functionalities of the Streamlit app that you want to build.", |
| 137 | + "", |
| 138 | + height=240, |
| 139 | + ) |
| 140 | + |
| 141 | + # Start LLM process |
| 142 | + start_button = st.button("Build", key="button_text_start") |
| 143 | + |
| 144 | + # If text prompt is provided and start button is clicked |
| 145 | + if text_prompt is not None and start_button: |
| 146 | + |
| 147 | + # Processing the text prompt |
| 148 | + with st.spinner("Processing ..."): |
| 149 | + |
| 150 | + try: |
| 151 | + # Get the generated output from the model based on the text prompt |
| 152 | + text_data_output = generate_text_response(text_prompt) |
| 153 | + # Display the extracted content |
| 154 | + st.subheader("Output") |
| 155 | + st.write(text_data_output) |
| 156 | + |
| 157 | + # Clear results if "Clear" button is clicked |
| 158 | + if st.button("Clear", key="button_text_clear"): |
| 159 | + os.remove(text_data_output) |
| 160 | + |
| 161 | + # Raise error if any error occurs during response generation |
| 162 | + except Exception as e: |
| 163 | + st.error(f"An error occurred: {e}") |
| 164 | + |
| 165 | + else: |
| 166 | + if not text_prompt and start_button: |
| 167 | + # Warn user to provide text prompt |
| 168 | + st.warning("Please provide your text prompt.") |
0 commit comments