Arfa-ilyas commited on
Commit
859c92c
·
verified ·
1 Parent(s): 2ec5856

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +226 -0
app.py ADDED
@@ -0,0 +1,226 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from PIL import Image, ImageEnhance, ImageFilter, ImageDraw, ImageFont
3
+ from transformers import BlipProcessor, BlipForConditionalGeneration
4
+ import streamlit as st
5
+ import torch
6
+ import os
7
+ from io import BytesIO
8
+ import barcode
9
+ from barcode.writer import ImageWriter
10
+
11
+ # Set device to CPU
12
+ device = torch.device("cpu")
13
+
14
+ # Load the BLIP model and processor
15
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
16
+ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large").to(device)
17
+
18
+ # Your Gemini API key
19
+ GEMINI_API_KEY = os.getenv('gemini_api')
20
+
21
+ def generate_detailed_description(image):
22
+ inputs = processor(image, return_tensors="pt")
23
+ out = model.generate(**inputs)
24
+ description = processor.decode(out[0], skip_special_tokens=True)
25
+ return description
26
+
27
+ def suggest_enhancements(image):
28
+ suggestions = [
29
+ "Consider adjusting the brightness for a more vivid image.",
30
+ "Increase contrast to make the image details stand out more.",
31
+ "Apply sharpening to enhance image details.",
32
+ "Use a filter to create a specific mood or style."
33
+ ]
34
+ return suggestions
35
+
36
+ def enhance_description_with_gemini(description):
37
+ url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key={GEMINI_API_KEY}"
38
+ headers = {"Content-Type": "application/json"}
39
+ data = {
40
+ "contents": [
41
+ {
42
+ "parts": [
43
+ {
44
+ "text": (
45
+ f"Based on the following description of an image, provide a more detailed description, "
46
+ f"generate possible captions, and suggest logo ideas. \n\n"
47
+ f"Description: {description}\n\n"
48
+ f"Detailed Description:\n"
49
+ f"Captions:\n"
50
+ f"Logo Suggestions:\n"
51
+ )
52
+ }
53
+ ]
54
+ }
55
+ ]
56
+ }
57
+
58
+ try:
59
+ response = requests.post(url, headers=headers, json=data)
60
+ response.raise_for_status()
61
+ response_json = response.json()
62
+ if "candidates" in response_json and len(response_json["candidates"]) > 0:
63
+ candidate = response_json["candidates"][0]
64
+ if "content" in candidate and "parts" in candidate["content"]:
65
+ parts = candidate["content"]["parts"]
66
+ if len(parts) > 0:
67
+ return parts[0].get("text", "No response text found")
68
+ return "No contents or parts in response"
69
+ except requests.exceptions.RequestException as e:
70
+ return f"Request failed: {e}"
71
+
72
+ def generate_barcode(data):
73
+ code = barcode.get('ean13', data, writer=ImageWriter())
74
+ barcode_image = code.render()
75
+ return barcode_image
76
+
77
+ def create_label(image, ingredients, usage, expiry_date, barcode_data):
78
+ # Create an image for the label
79
+ label_image = Image.new('RGB', (800, 1200), color='white')
80
+ draw = ImageDraw.Draw(label_image)
81
+
82
+ # Define font and size
83
+ try:
84
+ font = ImageFont.truetype("arial.ttf", 24)
85
+ except IOError:
86
+ font = ImageFont.load_default()
87
+
88
+ # Draw the main image
89
+ image.thumbnail((600, 400))
90
+ label_image.paste(image, (100, 50))
91
+
92
+ # Draw text
93
+ text_position = (100, 500)
94
+ draw.text(text_position, f"Ingredients:\n{ingredients}", font=font, fill="black")
95
+ text_position = (100, 600)
96
+ draw.text(text_position, f"Usage:\n{usage}", font=font, fill="black")
97
+ text_position = (100, 700)
98
+ draw.text(text_position, f"Expiry Date: {expiry_date}", font=font, fill="black")
99
+
100
+ # Draw barcode
101
+ barcode_image = generate_barcode(barcode_data)
102
+ label_image.paste(barcode_image, (100, 800))
103
+
104
+ return label_image
105
+
106
+ # Streamlit interface
107
+ st.title("Image Detailed Description, Captions, Logo Suggestions Generator with Image Enhancement Options")
108
+
109
+ # Custom CSS for the button style and alignment
110
+ st.markdown("""
111
+ <style>
112
+ .download-button-container {
113
+ display: flex;
114
+ justify-content: flex-end;
115
+ margin-top: -45px; /* Adjust this value to align with the image */
116
+ }
117
+ .stDownloadButton button {
118
+ background-color: #4CAF50; /* Green background */
119
+ color: white; /* White text */
120
+ padding: 10px 20px; /* Padding */
121
+ border: none; /* No border */
122
+ border-radius: 5px; /* Rounded corners */
123
+ text-align: center; /* Centered text */
124
+ text-decoration: none; /* No underline */
125
+ display: inline-block; /* Make the link behave like a button */
126
+ font-size: 16px; /* Increase font size */
127
+ margin: 4px 2px; /* Margin */
128
+ cursor: pointer; /* Pointer cursor on hover */
129
+ transition-duration: 0.4s; /* Transition for hover effect */
130
+ }
131
+ .stDownloadButton button:hover {
132
+ background-color: #45a049; /* Darker green on hover */
133
+ }
134
+ </style>
135
+ """, unsafe_allow_html=True)
136
+
137
+ # Session state to store the descriptions
138
+ if 'initial_description' not in st.session_state:
139
+ st.session_state['initial_description'] = None
140
+ if 'enhanced_text' not in st.session_state:
141
+ st.session_state['enhanced_text'] = None
142
+
143
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
144
+ img_url = st.text_input("Or enter image URL...")
145
+
146
+ if uploaded_file or img_url:
147
+ if uploaded_file:
148
+ image = Image.open(uploaded_file).convert('RGB')
149
+ else:
150
+ response = requests.get(img_url, stream=True)
151
+ image = Image.open(response.raw).convert('RGB')
152
+
153
+ st.image(image, caption="Uploaded Image", use_column_width=True)
154
+
155
+ if st.session_state['initial_description'] is None:
156
+ with st.spinner("Generating caption..."):
157
+ initial_description = generate_detailed_description(image)
158
+ enhanced_text = enhance_description_with_gemini(initial_description)
159
+ st.session_state['initial_description'] = initial_description
160
+ st.session_state['enhanced_text'] = enhanced_text
161
+ else:
162
+ initial_description = st.session_state['initial_description']
163
+ enhanced_text = st.session_state['enhanced_text']
164
+
165
+ st.subheader("Initial Description")
166
+ st.write(initial_description)
167
+
168
+ st.subheader("Enhanced Description, Captions, and Logo Suggestions with Image Enhancement Options")
169
+ st.write(enhanced_text)
170
+
171
+ # Function to generate and show enhancement suggestions
172
+ def show_enhancements(image, effect_name):
173
+ buffer = BytesIO()
174
+ image.save(buffer, format="PNG")
175
+ buffer.seek(0)
176
+
177
+ st.markdown('<div class="download-button-container">', unsafe_allow_html=True)
178
+ st.download_button(
179
+ label=f"Download {effect_name} Image",
180
+ data=buffer,
181
+ file_name=f"{effect_name}_image.png",
182
+ mime="image/png"
183
+ )
184
+ st.markdown('</div>', unsafe_allow_html=True)
185
+
186
+ # Display enhancement options
187
+ st.subheader("Enhancement Options")
188
+
189
+ if st.button('Increase Brightness'):
190
+ enhancer = ImageEnhance.Brightness(image)
191
+ enhanced_image = enhancer.enhance(1.5)
192
+ st.image(enhanced_image, caption="Enhanced Brightness")
193
+ show_enhancements(enhanced_image, "Brightness")
194
+
195
+ if st.button('Increase Contrast'):
196
+ enhancer = ImageEnhance.Contrast(image)
197
+ enhanced_image = enhancer.enhance(1.5)
198
+ st.image(enhanced_image, caption="Enhanced Contrast")
199
+ show_enhancements(enhanced_image, "Contrast")
200
+
201
+ if st.button('Sharpen Image'):
202
+ enhanced_image = image.filter(ImageFilter.SHARPEN)
203
+ st.image(enhanced_image, caption="Sharpened Image")
204
+ show_enhancements(enhanced_image, "Sharpen")
205
+
206
+ if st.button('Apply Gaussian Blur'):
207
+ enhanced_image = image.filter(ImageFilter.GaussianBlur(radius=2))
208
+ st.image(enhanced_image, caption="Blurred Image")
209
+ show_enhancements(enhanced_image, "Blur")
210
+
211
+ if st.button('Apply Edge Enhancement'):
212
+ enhanced_image = image.filter(ImageFilter.EDGE_ENHANCE)
213
+ st.image(enhanced_image, caption="Edge Enhanced Image")
214
+ show_enhancements(enhanced_image, "Edge Enhancement")
215
+
216
+ # Inputs for label details
217
+ st.subheader("Generate Label")
218
+ ingredients = st.text_area("Ingredients")
219
+ usage = st.text_area("Usage Instructions")
220
+ expiry_date = st.text_input("Expiry Date (e.g., 2024-12-31)")
221
+ barcode_data = st.text_input("Barcode Data")
222
+
223
+ if st.button('Generate Label'):
224
+ if ingredients and usage and expiry_date and barcode_data:
225
+ label_image = create_label(image, ingredients, usage, expiry_date, barcode_data)
226
+ st.image(label_image