Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,33 +9,38 @@ import os
|
|
9 |
# Set up page configuration
|
10 |
st.set_page_config(page_title="ProductProse - AI Product Description Generator", layout="wide")
|
11 |
|
12 |
-
# Initialize session state to track API responses
|
13 |
if 'generated_description' not in st.session_state:
|
14 |
st.session_state.generated_description = None
|
15 |
if 'translated_description' not in st.session_state:
|
16 |
st.session_state.translated_description = None
|
17 |
if 'customized_description' not in st.session_state:
|
18 |
st.session_state.customized_description = None
|
19 |
-
if '
|
20 |
-
st.session_state.
|
21 |
|
22 |
# Sidebar for product data input
|
23 |
-
st.sidebar
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
28 |
|
29 |
-
# Select target language for translation
|
30 |
-
target_language = st.
|
31 |
|
32 |
# Main app title and description
|
33 |
-
st.
|
34 |
st.markdown("""
|
35 |
Welcome to ProductProse, an AI-powered tool for generating and customizing product descriptions using IBM Granite LLMs.
|
36 |
Simply input your product data and let the AI do the rest, including generating descriptions, translating them into multiple languages, and customizing them to match your brand tone and style.
|
37 |
""")
|
38 |
|
|
|
|
|
|
|
|
|
39 |
# IBM WatsonX API Setup
|
40 |
project_id = os.getenv('WATSONX_PROJECT_ID')
|
41 |
api_key = os.getenv('WATSONX_API_KEY')
|
@@ -46,15 +51,15 @@ if api_key and project_id:
|
|
46 |
client.set.default_project(project_id)
|
47 |
|
48 |
# Tone Selection for Description Customization
|
49 |
-
tone_example = st.sidebar.selectbox("
|
50 |
-
st.sidebar.markdown("_Example:
|
51 |
|
52 |
# Keyword Input for SEO Optimization
|
53 |
-
seo_keywords_example = st.sidebar.text_area("SEO Keywords (comma-separated, e.g.,
|
54 |
-
st.sidebar.markdown("_Example: Add keywords
|
55 |
|
56 |
# Step 1: Generate Product Description
|
57 |
-
|
58 |
if st.button("Generate Description"):
|
59 |
if product_name and features and benefits and specifications:
|
60 |
# Prompt engineering for Granite-13B-Instruct
|
@@ -78,6 +83,7 @@ if api_key and project_id:
|
|
78 |
description_response = model.generate_text(prompt=prompt)
|
79 |
st.session_state.generated_description = description_response
|
80 |
st.session_state.translated_description = None # Clear previous translations
|
|
|
81 |
st.success("Product description generated!")
|
82 |
st.write(description_response)
|
83 |
except Exception as e:
|
@@ -86,7 +92,7 @@ if api_key and project_id:
|
|
86 |
st.warning("Please fill in all the product data fields before generating a description.")
|
87 |
|
88 |
# Step 2: Translate Product Description
|
89 |
-
|
90 |
if st.session_state.generated_description:
|
91 |
if st.button("Translate Description"):
|
92 |
try:
|
@@ -117,8 +123,8 @@ if api_key and project_id:
|
|
117 |
st.write(st.session_state.translated_description)
|
118 |
|
119 |
# Step 3: Customize Product Description via Chat Interface
|
120 |
-
|
121 |
-
customization_prompt = st.text_input("Customize the product description
|
122 |
|
123 |
if st.session_state.generated_description and customization_prompt:
|
124 |
if st.button("Customize Description"):
|
@@ -145,15 +151,59 @@ if api_key and project_id:
|
|
145 |
st.subheader("Customized Product Description")
|
146 |
st.write(st.session_state.customized_description)
|
147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
# Step 4: Feedback and Quality Scoring
|
149 |
-
|
150 |
-
|
151 |
feedback_comments = st.text_area("Additional Comments")
|
152 |
|
153 |
if st.button("Submit Feedback"):
|
154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
st.success("Thank you for your feedback!")
|
156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
|
158 |
else:
|
159 |
st.error("IBM WatsonX API credentials are not set. Please check your environment variables.")
|
|
|
9 |
# Set up page configuration
|
10 |
st.set_page_config(page_title="ProductProse - AI Product Description Generator", layout="wide")
|
11 |
|
12 |
+
# Initialize session state to track API responses, user feedback, and history
|
13 |
if 'generated_description' not in st.session_state:
|
14 |
st.session_state.generated_description = None
|
15 |
if 'translated_description' not in st.session_state:
|
16 |
st.session_state.translated_description = None
|
17 |
if 'customized_description' not in st.session_state:
|
18 |
st.session_state.customized_description = None
|
19 |
+
if 'feedback_history' not in st.session_state:
|
20 |
+
st.session_state.feedback_history = []
|
21 |
|
22 |
# Sidebar for product data input
|
23 |
+
with st.sidebar:
|
24 |
+
st.title("Product Data Input")
|
25 |
+
product_name = st.text_input("Product Name", placeholder="e.g., Smart Home Hub")
|
26 |
+
features = st.text_area("Product Features", placeholder="e.g., Voice Control, Energy Efficient, Compact Design")
|
27 |
+
benefits = st.text_area("Product Benefits", placeholder="e.g., Saves time, Reduces energy usage, Easy to install")
|
28 |
+
specifications = st.text_area("Product Specifications", placeholder="e.g., Dimensions: 10x5x3 inches, Weight: 1.5 lbs")
|
29 |
|
30 |
+
# Select target language for translation
|
31 |
+
target_language = st.selectbox("Target Language for Translation", ["French", "Spanish", "German", "Chinese", "Japanese"])
|
32 |
|
33 |
# Main app title and description
|
34 |
+
st.markdown("# ProductProse - AI Product Description Generator")
|
35 |
st.markdown("""
|
36 |
Welcome to ProductProse, an AI-powered tool for generating and customizing product descriptions using IBM Granite LLMs.
|
37 |
Simply input your product data and let the AI do the rest, including generating descriptions, translating them into multiple languages, and customizing them to match your brand tone and style.
|
38 |
""")
|
39 |
|
40 |
+
# UI Enhancement: Color-coded sections for clarity
|
41 |
+
def section_header(title, color):
|
42 |
+
st.markdown(f'<h2 style="color:{color};">{title}</h2>', unsafe_allow_html=True)
|
43 |
+
|
44 |
# IBM WatsonX API Setup
|
45 |
project_id = os.getenv('WATSONX_PROJECT_ID')
|
46 |
api_key = os.getenv('WATSONX_API_KEY')
|
|
|
51 |
client.set.default_project(project_id)
|
52 |
|
53 |
# Tone Selection for Description Customization
|
54 |
+
tone_example = st.sidebar.selectbox("Tone Example (Modify as needed)", ["Formal", "Casual", "Professional", "Playful"])
|
55 |
+
st.sidebar.markdown("_Example: Choose a tone to match your brand's style._")
|
56 |
|
57 |
# Keyword Input for SEO Optimization
|
58 |
+
seo_keywords_example = st.sidebar.text_area("SEO Keywords (comma-separated)", placeholder="e.g., wireless, fast charging, Bluetooth")
|
59 |
+
st.sidebar.markdown("_Example: Add keywords that enhance search engine optimization._")
|
60 |
|
61 |
# Step 1: Generate Product Description
|
62 |
+
section_header("Step 1: Generate Product Description", "blue")
|
63 |
if st.button("Generate Description"):
|
64 |
if product_name and features and benefits and specifications:
|
65 |
# Prompt engineering for Granite-13B-Instruct
|
|
|
83 |
description_response = model.generate_text(prompt=prompt)
|
84 |
st.session_state.generated_description = description_response
|
85 |
st.session_state.translated_description = None # Clear previous translations
|
86 |
+
st.session_state.customized_description = None # Clear previous customizations
|
87 |
st.success("Product description generated!")
|
88 |
st.write(description_response)
|
89 |
except Exception as e:
|
|
|
92 |
st.warning("Please fill in all the product data fields before generating a description.")
|
93 |
|
94 |
# Step 2: Translate Product Description
|
95 |
+
section_header("Step 2: Translate Product Description", "green")
|
96 |
if st.session_state.generated_description:
|
97 |
if st.button("Translate Description"):
|
98 |
try:
|
|
|
123 |
st.write(st.session_state.translated_description)
|
124 |
|
125 |
# Step 3: Customize Product Description via Chat Interface
|
126 |
+
section_header("Step 3: Customize Product Description", "orange")
|
127 |
+
customization_prompt = st.text_input("Customize the product description", placeholder="e.g., Make the tone more playful and mention our eco-friendly packaging")
|
128 |
|
129 |
if st.session_state.generated_description and customization_prompt:
|
130 |
if st.button("Customize Description"):
|
|
|
151 |
st.subheader("Customized Product Description")
|
152 |
st.write(st.session_state.customized_description)
|
153 |
|
154 |
+
# Option to translate the customized description if it hasn't been translated yet
|
155 |
+
if st.session_state.translated_description:
|
156 |
+
if st.button("Translate Customized Description"):
|
157 |
+
try:
|
158 |
+
# Translate the customized description using Granite-20B-Multilingual
|
159 |
+
prompt = f"Translate the following customized product description into {target_language}:\n{st.session_state.customized_description}"
|
160 |
+
model = ModelInference(model_id=ModelTypes.GRANITE_20B_MULTILINGUAL, params={
|
161 |
+
GenParams.DECODING_METHOD: DecodingMethods.GREEDY,
|
162 |
+
GenParams.MIN_NEW_TOKENS: 50,
|
163 |
+
GenParams.MAX_NEW_TOKENS: 200,
|
164 |
+
GenParams.STOP_SEQUENCES: ["\n"]
|
165 |
+
}, credentials=credentials, project_id=project_id)
|
166 |
+
|
167 |
+
with st.spinner(f"Translating customized product description to {target_language}..."):
|
168 |
+
customized_translation_response = model.generate_text(prompt=prompt)
|
169 |
+
st.session_state.translated_customized_description = customized_translation_response
|
170 |
+
st.success(f"Customized product description translated to {target_language}!")
|
171 |
+
st.write(customized_translation_response)
|
172 |
+
except Exception as e:
|
173 |
+
st.error(f"An error occurred while translating the customized description: {e}")
|
174 |
+
|
175 |
+
# Display the translated customized description if available
|
176 |
+
if 'translated_customized_description' in st.session_state:
|
177 |
+
st.subheader(f"Translated Customized Product Description ({target_language})")
|
178 |
+
st.write(st.session_state.translated_customized_description)
|
179 |
+
|
180 |
# Step 4: Feedback and Quality Scoring
|
181 |
+
section_header("Step 4: Provide Feedback", "purple")
|
182 |
+
feedback_rating = st.slider("Rate the quality of the generated product description (1 = Poor, 5 = Excellent)", 1, 5, 3)
|
183 |
feedback_comments = st.text_area("Additional Comments")
|
184 |
|
185 |
if st.button("Submit Feedback"):
|
186 |
+
# Save the feedback in session state
|
187 |
+
feedback_entry = {
|
188 |
+
"rating": feedback_rating,
|
189 |
+
"comments": feedback_comments,
|
190 |
+
"description": st.session_state.generated_description,
|
191 |
+
"customized_description": st.session_state.customized_description if st.session_state.customized_description else "N/A",
|
192 |
+
"translated_description": st.session_state.translated_description if st.session_state.translated_description else "N/A"
|
193 |
+
}
|
194 |
+
st.session_state.feedback_history.append(feedback_entry)
|
195 |
st.success("Thank you for your feedback!")
|
196 |
+
|
197 |
+
# Display the feedback summary
|
198 |
+
st.subheader("Feedback Summary")
|
199 |
+
for i, feedback in enumerate(st.session_state.feedback_history, 1):
|
200 |
+
st.write(f"**Feedback {i}:**")
|
201 |
+
st.write(f"Rating: {feedback['rating']}")
|
202 |
+
st.write(f"Comments: {feedback['comments']}")
|
203 |
+
st.write(f"Generated Description: {feedback['description']}")
|
204 |
+
st.write(f"Customized Description: {feedback['customized_description']}")
|
205 |
+
st.write(f"Translated Description: {feedback['translated_description']}")
|
206 |
+
st.markdown("---")
|
207 |
|
208 |
else:
|
209 |
st.error("IBM WatsonX API credentials are not set. Please check your environment variables.")
|