File size: 5,811 Bytes
aa44125 bad9357 aa44125 1da96a1 aa44125 9d64dbb aa44125 c4dadef aa44125 c4dadef aa44125 7699705 aa44125 |
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 |
import streamlit as st
import os
from openai import OpenAI
from PIL import Image
import io
import base64
st.set_page_config(
page_title="Your App",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS for better styling
def apply_custom_css():
st.markdown("""
<style>
.stApp {
max-width: 1200px;
margin: 0 auto;
}
.upload-box {
border: 2px dashed #4CAF50;
border-radius: 10px;
padding: 20px;
text-align: center;
background-color: #f8f9fa;
}
.sidebar-content {
padding: 20px;
background-color: #f1f3f4;
border-radius: 10px;
}
.response-box {
background-color: #ffffff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.title-text {
color: #1E88E5;
text-align: center;
font-size: 2.5rem;
margin-bottom: 2rem;
}
</style>
""", unsafe_allow_html=True)
def initialize_session_state():
if 'history' not in st.session_state:
st.session_state.history = []
def encode_image_to_base64(image):
buffered = io.BytesIO()
image.save(buffered, format="PNG")
return base64.b64encode(buffered.getvalue()).decode('utf-8')
def analyze_image(image, question, api_key):
try:
client = OpenAI(api_key=api_key)
base64_image = encode_image_to_base64(image)
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": question},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{base64_image}"
}
},
],
}
],
max_tokens=500
)
return completion.choices[0].message.content
except Exception as e:
return f"Error: {str(e)}"
def main():
apply_custom_css()
initialize_session_state()
# st.set_page_config(
# page_title="Smart Image Analyzer",
# page_icon="π",
# layout="wide"
# )
# Sidebar Configuration
with st.sidebar:
st.markdown('<div class="sidebar-content">', unsafe_allow_html=True)
st.image("https://your-logo-url.com/logo.png", width=100) # Add your logo
st.header("βοΈ Configuration")
api_key = st.text_input("OpenAI API Key:", type="password")
st.markdown("### π― Quick Prompts")
quick_prompts = [
"Suggest receipe based on ingredients"
"Describe this image in detail",
"What objects are present?",
"Analyze the composition",
"Identify any text in the image",
]
selected_prompt = st.selectbox("Select a prompt:", quick_prompts)
st.markdown("</div>", unsafe_allow_html=True)
# Main Content
st.markdown('<h1 class="title-text">π AI Receipe Generator</h1>', unsafe_allow_html=True)
# Image Upload Section
col1, col2, col3 = st.columns([1, 2, 1])
with col2:
st.markdown('<div class="upload-box">', unsafe_allow_html=True)
uploaded_file = st.file_uploader(
"Drop your image here or click to upload",
type=['png', 'jpg', 'jpeg'],
help="Supported formats: PNG, JPG, JPEG"
)
st.markdown('</div>', unsafe_allow_html=True)
if uploaded_file:
image = Image.open(uploaded_file)
# Image Display and Analysis Section
col1, col2 = st.columns([1, 1])
with col1:
st.image(image, use_container_width=True, caption="Uploaded Image")
with col2:
st.markdown('<div class="response-box">', unsafe_allow_html=True)
question = st.text_input(
"Your Question:",
value=selected_prompt,
key="question_input"
)
if st.button("π Analyze", use_container_width=True):
if not api_key:
st.error("β οΈ Please enter your OpenAI API key in the sidebar.")
else:
with st.spinner("π Analyzing your image..."):
response = analyze_image(image, question, api_key)
st.markdown("### π Analysis Result:")
st.write(response)
# Add to history
st.session_state.history.append({
"question": question,
"response": response,
"timestamp": st.session_state.get("timestamp", "")
})
st.markdown('</div>', unsafe_allow_html=True)
# History Section
if st.session_state.history:
st.markdown("### π Previous Analyses")
for item in reversed(st.session_state.history[-5:]):
with st.expander(f"Q: {item['question'][:50]}..."):
st.write(item['response'])
# Footer
st.markdown("---")
st.markdown(
"""
<div style='text-align: center'>
<p>Built with β€οΈ using Streamlit and OpenAI GPT-4 Vision API</p>
<p>Β© 2024 Smart Image Analyzer</p>
</div>
""",
unsafe_allow_html=True
)
if __name__ == "__main__":
main() |