Skip to content

Commit ceafee9

Browse files
initial commit and working app
0 parents  commit ceafee9

15 files changed

+515
-0
lines changed

.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GOOGLE_API_KEY=YOUR_GOOGLE_API_KEY

.gitignore

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.coverage
42+
.coverage.*
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
*.cover
47+
.hypothesis/
48+
.pytest_cache/
49+
50+
# Translations
51+
*.mo
52+
*.pot
53+
54+
# Django stuff:
55+
*.log
56+
local_settings.py
57+
db.sqlite3
58+
59+
# Flask stuff:
60+
instance/
61+
.webassets-cache
62+
63+
# Scrapy stuff:
64+
.scrapy
65+
66+
# Sphinx documentation
67+
docs/_build/
68+
69+
# PyBuilder
70+
target/
71+
72+
# Jupyter Notebook
73+
.ipynb_checkpoints
74+
75+
# pyenv
76+
.python-version
77+
pyvenv.cfg
78+
79+
# celery beat schedule file
80+
celerybeat-schedule
81+
82+
# SageMath parsed files
83+
*.sage.py
84+
85+
# Environments
86+
.env
87+
.venv
88+
env/
89+
venv/
90+
project-env/
91+
ENV/
92+
env.bak/
93+
venv.bak/
94+
95+
# Spyder project settings
96+
.spyderproject
97+
.spyproject
98+
99+
# Rope project settings
100+
.ropeproject
101+
102+
# mkdocs documentation
103+
/site
104+
105+
# mypy
106+
.mypy_cache/
107+
108+
# Mac OS
109+
.DS_Store

.streamlit/config.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[theme]
2+
base="dark"

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Sitam Meur
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Whitespace-only changes.

app.py

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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.")

assets/example_prompt.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
I want to make a photo gallery app using Streamlit. Here are the details:
2+
3+
1. **Photo Storage:**
4+
- The photos are stored locally on my computer in a folder named 'photos'.
5+
6+
2. **Photo Display:**
7+
- Display the photos in a grid layout.
8+
- Each photo should be displayed as a thumbnail. When a thumbnail is clicked, the full-sized image should be displayed.
9+
10+
3. **Additional Features:**
11+
- Users should be able to navigate through different folders within the 'photos' directory.
12+
- Each photo should have a caption displayed below it.
13+
- Provide an option for users to download the full-sized image.
14+
- Include a search functionality to filter photos by filename or caption.
15+
16+
Please generate the Streamlit code to create this photo gallery app based on the above specifications.

images/streamlit-app-preview-1.png

207 KB
Loading

images/streamlit-app-preview-2.png

105 KB
Loading

requirements.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
streamlit==1.36.0
2+
Pillow==10.3.0
3+
streamlit-image-select==0.6.0
4+
google-generativeai==0.7.1
5+
python-dotenv==1.0.1

src/__init__.py

Whitespace-only changes.

src/config.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Set up the model configuration for content generation
2+
generation_config = {
3+
"temperature": 0.3,
4+
"top_p": 0.95,
5+
"top_k": 40,
6+
"max_output_tokens": 1400,
7+
"response_mime_type": "text/plain",
8+
}
9+
10+
# Define safety settings for content generation
11+
safety_settings = [
12+
{"category": f"HARM_CATEGORY_{category}", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}
13+
for category in [
14+
"HARASSMENT",
15+
"HATE_SPEECH",
16+
"SEXUALLY_EXPLICIT",
17+
"DANGEROUS_CONTENT",
18+
]
19+
]
20+
21+
# Define the model name for content generation using the Gemini API
22+
model_name = "gemini-1.5-pro"

0 commit comments

Comments
 (0)