streamlit file added
Browse files- streamlit_app.py +164 -0
streamlit_app.py
ADDED
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import os
|
4 |
+
import subprocess
|
5 |
+
import time
|
6 |
+
|
7 |
+
# Constants for file paths and MIME types
|
8 |
+
PDF_MIME_TYPE = "application/pdf"
|
9 |
+
BRAND_MARKETING = "data/reports/template_PDF/brand marketing.pdf"
|
10 |
+
CONTENT_MARKETING = "data/reports/template_PDF/content marketing.pdf"
|
11 |
+
SOCIAL_MEDIA_MARKETING = "data/reports/template_PDF/social media marketing.pdf"
|
12 |
+
PRODUCT_IMAGES_DIR = "data/product"
|
13 |
+
COMPETITOR_IMAGES_DIR = "data/competitor"
|
14 |
+
REPORT = "src/Report/report.pdf"
|
15 |
+
STATUS_FILE = "src/Report/status.txt" # Path to the status file
|
16 |
+
|
17 |
+
# Ensure directories for saving images exist
|
18 |
+
os.makedirs(PRODUCT_IMAGES_DIR, exist_ok=True)
|
19 |
+
os.makedirs(COMPETITOR_IMAGES_DIR, exist_ok=True)
|
20 |
+
|
21 |
+
# Status checker function
|
22 |
+
def check_status_file(timeout=600, interval=5):
|
23 |
+
"""Monitor the status.txt file for completion or errors."""
|
24 |
+
start_time = time.time()
|
25 |
+
while time.time() - start_time < timeout:
|
26 |
+
if os.path.exists(STATUS_FILE):
|
27 |
+
with open(STATUS_FILE, "r") as f:
|
28 |
+
status = f.read().strip()
|
29 |
+
if status == "done":
|
30 |
+
return "success"
|
31 |
+
elif status.startswith("error"):
|
32 |
+
return status
|
33 |
+
time.sleep(interval)
|
34 |
+
return "timeout"
|
35 |
+
|
36 |
+
# Initialize session state variables to track process
|
37 |
+
if "report_generated" not in st.session_state:
|
38 |
+
st.session_state.report_generated = False
|
39 |
+
if "analysis_in_progress" not in st.session_state:
|
40 |
+
st.session_state.analysis_in_progress = False
|
41 |
+
|
42 |
+
# Streamlit Title and File Upload
|
43 |
+
st.title("Product vs Competitor Image Analysis")
|
44 |
+
|
45 |
+
# Company Name Input
|
46 |
+
company_name = st.text_input(
|
47 |
+
"Company Name",
|
48 |
+
placeholder="Enter the company name..."
|
49 |
+
)
|
50 |
+
|
51 |
+
st.subheader("Upload 6 Product Images and Provide Description")
|
52 |
+
product_images = st.file_uploader(
|
53 |
+
"Upload Product Images",
|
54 |
+
type=["jpg", "jpeg", "png"],
|
55 |
+
accept_multiple_files=True,
|
56 |
+
key="product"
|
57 |
+
)
|
58 |
+
about_product = st.text_area(
|
59 |
+
"About Product",
|
60 |
+
placeholder="Enter a brief description about the product...",
|
61 |
+
height=80
|
62 |
+
)
|
63 |
+
|
64 |
+
st.subheader("Upload 6 Competitor Images and Provide Description")
|
65 |
+
competitor_images = st.file_uploader(
|
66 |
+
"Upload Competitor Images",
|
67 |
+
type=["jpg", "jpeg", "png"],
|
68 |
+
accept_multiple_files=True,
|
69 |
+
key="competitor"
|
70 |
+
)
|
71 |
+
about_competitor = st.text_area(
|
72 |
+
"About Competitor",
|
73 |
+
placeholder="Enter a brief description about the competitor...",
|
74 |
+
height=80
|
75 |
+
)
|
76 |
+
|
77 |
+
# Validation for Image Count
|
78 |
+
if product_images and competitor_images:
|
79 |
+
if len(product_images) == 6 and len(competitor_images) == 6:
|
80 |
+
st.success("All images uploaded successfully!")
|
81 |
+
else:
|
82 |
+
st.warning("Please upload exactly 6 images for both products and competitors.")
|
83 |
+
|
84 |
+
# Main "Generate" button logic
|
85 |
+
if st.button("Generate"):
|
86 |
+
# Only proceed if report hasn't been generated or analysis isn't already in progress
|
87 |
+
if not st.session_state.report_generated and not st.session_state.analysis_in_progress:
|
88 |
+
if product_images and competitor_images and len(product_images) == 6 and len(competitor_images) == 6:
|
89 |
+
if company_name:
|
90 |
+
st.session_state.analysis_in_progress = True # mark as in progress
|
91 |
+
st.subheader("Product Images vs Competitor Images")
|
92 |
+
|
93 |
+
# Save images to respective directories
|
94 |
+
for idx, img_file in enumerate(product_images):
|
95 |
+
img = Image.open(img_file)
|
96 |
+
if img.mode == "RGBA":
|
97 |
+
img = img.convert("RGB")
|
98 |
+
img.save(os.path.join(PRODUCT_IMAGES_DIR, f"image{idx + 1}.jpeg"), "JPEG")
|
99 |
+
|
100 |
+
for idx, img_file in enumerate(competitor_images):
|
101 |
+
img = Image.open(img_file)
|
102 |
+
if img.mode == "RGBA":
|
103 |
+
img = img.convert("RGB")
|
104 |
+
img.save(os.path.join(COMPETITOR_IMAGES_DIR, f"image{idx + 1}.jpeg"), "JPEG")
|
105 |
+
|
106 |
+
try:
|
107 |
+
# Initialize status file with "pending"
|
108 |
+
with open(STATUS_FILE, "w") as f:
|
109 |
+
f.write("pending")
|
110 |
+
|
111 |
+
# Execute the analysis script asynchronously
|
112 |
+
subprocess.Popen(['python', 'src/analysis.py', company_name])
|
113 |
+
|
114 |
+
# Monitor the status file
|
115 |
+
st.info("Running analysis. Please wait...")
|
116 |
+
status = check_status_file()
|
117 |
+
|
118 |
+
if status == "success":
|
119 |
+
st.success("Analysis completed successfully!")
|
120 |
+
st.session_state.report_generated = True # mark as generated
|
121 |
+
|
122 |
+
# Download Buttons for Generated PDF Reports
|
123 |
+
for report_name, label in [
|
124 |
+
(BRAND_MARKETING, f"{company_name} Brand Marketing Report"),
|
125 |
+
(CONTENT_MARKETING, f"{company_name} Content Marketing Report"),
|
126 |
+
(SOCIAL_MEDIA_MARKETING, f"{company_name} Social Media Marketing Report")
|
127 |
+
]:
|
128 |
+
if os.path.exists(report_name):
|
129 |
+
with open(report_name, "rb") as report_file:
|
130 |
+
st.download_button(
|
131 |
+
label=f"Download {label}",
|
132 |
+
data=report_file,
|
133 |
+
file_name=f"{company_name} {label}.pdf",
|
134 |
+
mime=PDF_MIME_TYPE
|
135 |
+
)
|
136 |
+
else:
|
137 |
+
st.error(f"{report_name} not found. Please generate the report first.")
|
138 |
+
|
139 |
+
# For the custom report
|
140 |
+
custom_report_name = f"{company_name} report.pdf"
|
141 |
+
if os.path.exists(REPORT):
|
142 |
+
with open(REPORT, "rb") as report_file:
|
143 |
+
st.download_button(
|
144 |
+
label=f"Download {company_name} Report",
|
145 |
+
data=report_file,
|
146 |
+
file_name=custom_report_name,
|
147 |
+
mime=PDF_MIME_TYPE
|
148 |
+
)
|
149 |
+
else:
|
150 |
+
st.error(f"{REPORT} not found. Please generate the report first.")
|
151 |
+
elif status == "timeout":
|
152 |
+
st.error("Analysis timed out. Please try again.")
|
153 |
+
else:
|
154 |
+
st.error(f"Analysis script failed: {status}")
|
155 |
+
except Exception as e:
|
156 |
+
st.error(f"Error running analysis script: {e}")
|
157 |
+
finally:
|
158 |
+
st.session_state.analysis_in_progress = False # reset the flag
|
159 |
+
else:
|
160 |
+
st.warning("Please enter your company name.")
|
161 |
+
else:
|
162 |
+
st.warning("Please upload exactly 6 images for both products and competitors before generating.")
|
163 |
+
else:
|
164 |
+
st.info("Report has already been generated for this session.")
|