Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import streamlit as st
|
2 |
-
from PIL import Image
|
3 |
from ibm_watsonx_ai import APIClient
|
4 |
from ibm_watsonx_ai import Credentials
|
5 |
from ibm_watsonx_ai.foundation_models import ModelInference
|
@@ -7,29 +6,34 @@ from ibm_watsonx_ai.foundation_models.utils.enums import ModelTypes, DecodingMet
|
|
7 |
from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams
|
8 |
import os
|
9 |
|
10 |
-
#
|
11 |
-
st.set_page_config(page_title="AI Product
|
12 |
|
13 |
-
#
|
14 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
product_name = st.sidebar.text_input("Product Name", "Example Product")
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
budget = st.sidebar.number_input("Budget ($)", min_value=0, value=1000)
|
20 |
|
21 |
-
|
22 |
-
st.sidebar.
|
23 |
|
24 |
# Main app title and description
|
25 |
-
st.title("
|
26 |
st.markdown("""
|
27 |
-
Welcome to
|
|
|
28 |
""")
|
29 |
|
30 |
-
# Tabs for different sections of the app
|
31 |
-
tabs = st.tabs(["Design Generation", "Simulation", "Optimization"])
|
32 |
-
|
33 |
# IBM WatsonX API Setup
|
34 |
project_id = os.getenv('WATSONX_PROJECT_ID')
|
35 |
api_key = os.getenv('WATSONX_API_KEY')
|
@@ -39,35 +43,79 @@ if api_key and project_id:
|
|
39 |
client = APIClient(credentials)
|
40 |
client.set.default_project(project_id)
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
model_id = ModelTypes.GRANITE_13B_CHAT_V2
|
50 |
-
model = ModelInference(model_id=model_id, params=parameters, credentials=credentials, project_id=project_id)
|
51 |
-
|
52 |
-
# Design Generation Tab
|
53 |
-
with tabs[0]:
|
54 |
-
st.header("Generate Product Designs")
|
55 |
-
st.write("Input your product specifications in the sidebar and click below to generate design concepts.")
|
56 |
-
|
57 |
-
if st.button("Generate Design Concepts"):
|
58 |
-
prompt = f"""You are an AI specialized in product design. Generate creative product design concepts based on the following details:\n
|
59 |
Product Name: {product_name}\n
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
Provide detailed design concepts and explain their features."""
|
65 |
-
|
66 |
try:
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
except Exception as e:
|
71 |
-
st.error(f"An error occurred: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
|
|
|
|
1 |
import streamlit as st
|
|
|
2 |
from ibm_watsonx_ai import APIClient
|
3 |
from ibm_watsonx_ai import Credentials
|
4 |
from ibm_watsonx_ai.foundation_models import ModelInference
|
|
|
6 |
from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams
|
7 |
import os
|
8 |
|
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 and query count
|
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 |
+
|
20 |
+
# Sidebar for product data input
|
21 |
+
st.sidebar.title("Product Data Input")
|
22 |
product_name = st.sidebar.text_input("Product Name", "Example Product")
|
23 |
+
features = st.sidebar.text_area("Product Features", "Feature 1, Feature 2, Feature 3")
|
24 |
+
benefits = st.sidebar.text_area("Product Benefits", "Benefit 1, Benefit 2, Benefit 3")
|
25 |
+
specifications = st.sidebar.text_area("Product Specifications", "Specification 1, Specification 2, Specification 3")
|
|
|
26 |
|
27 |
+
# Select target language for translation
|
28 |
+
target_language = st.sidebar.selectbox("Target Language for Translation", ["French", "Spanish", "German", "Chinese", "Japanese"])
|
29 |
|
30 |
# Main app title and description
|
31 |
+
st.title("ProductProse - AI Product Description Generator")
|
32 |
st.markdown("""
|
33 |
+
Welcome to ProductProse, an AI-powered tool for generating and customizing product descriptions using IBM Granite LLMs.
|
34 |
+
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.
|
35 |
""")
|
36 |
|
|
|
|
|
|
|
37 |
# IBM WatsonX API Setup
|
38 |
project_id = os.getenv('WATSONX_PROJECT_ID')
|
39 |
api_key = os.getenv('WATSONX_API_KEY')
|
|
|
43 |
client = APIClient(credentials)
|
44 |
client.set.default_project(project_id)
|
45 |
|
46 |
+
# Generate Product Description
|
47 |
+
st.header("Step 1: Generate Product Description")
|
48 |
+
if st.button("Generate Description"):
|
49 |
+
if product_name and features and benefits and specifications:
|
50 |
+
# Prompt engineering for Granite-13B-Instruct
|
51 |
+
prompt = f"""
|
52 |
+
You are an AI that generates high-quality product descriptions. Based on the following details, generate a detailed product description:\n
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
Product Name: {product_name}\n
|
54 |
+
Features: {features}\n
|
55 |
+
Benefits: {benefits}\n
|
56 |
+
Specifications: {specifications}\n
|
57 |
+
"""
|
|
|
|
|
58 |
try:
|
59 |
+
model = ModelInference(model_id=ModelTypes.GRANITE_13B_INSTRUCT_V2, params={
|
60 |
+
GenParams.DECODING_METHOD: DecodingMethods.GREEDY,
|
61 |
+
GenParams.MIN_NEW_TOKENS: 50,
|
62 |
+
GenParams.MAX_NEW_TOKENS: 200,
|
63 |
+
GenParams.STOP_SEQUENCES: ["\n"]
|
64 |
+
}, credentials=credentials, project_id=project_id)
|
65 |
+
|
66 |
+
with st.spinner("Generating product description..."):
|
67 |
+
description_response = model.generate_text(prompt=prompt)
|
68 |
+
st.session_state.generated_description = description_response
|
69 |
+
st.success("Product description generated!")
|
70 |
+
st.write(description_response)
|
71 |
except Exception as e:
|
72 |
+
st.error(f"An error occurred while generating the description: {e}")
|
73 |
+
else:
|
74 |
+
st.warning("Please fill in all the product data fields before generating a description.")
|
75 |
+
|
76 |
+
# Translate Product Description
|
77 |
+
st.header("Step 2: Translate Product Description")
|
78 |
+
if st.session_state.generated_description and st.button("Translate Description"):
|
79 |
+
try:
|
80 |
+
# Translate the description using Granite-20B-Multilingual
|
81 |
+
prompt = f"Translate the following product description into {target_language}:\n{st.session_state.generated_description}"
|
82 |
+
model = ModelInference(model_id=ModelTypes.GRANITE_20B_MULTILINGUAL, params={
|
83 |
+
GenParams.DECODING_METHOD: DecodingMethods.GREEDY,
|
84 |
+
GenParams.MIN_NEW_TOKENS: 50,
|
85 |
+
GenParams.MAX_NEW_TOKENS: 200,
|
86 |
+
GenParams.STOP_SEQUENCES: ["\n"]
|
87 |
+
}, credentials=credentials, project_id=project_id)
|
88 |
+
|
89 |
+
with st.spinner(f"Translating product description to {target_language}..."):
|
90 |
+
translation_response = model.generate_text(prompt=prompt)
|
91 |
+
st.session_state.translated_description = translation_response
|
92 |
+
st.success(f"Product description translated to {target_language}!")
|
93 |
+
st.write(translation_response)
|
94 |
+
except Exception as e:
|
95 |
+
st.error(f"An error occurred while translating the description: {e}")
|
96 |
+
|
97 |
+
# Customize Product Description via Chat Interface
|
98 |
+
st.header("Step 3: Customize Product Description")
|
99 |
+
customization_prompt = st.text_input("Customize the product description (e.g., adjust tone, add brand-specific details)")
|
100 |
+
|
101 |
+
if st.session_state.generated_description and customization_prompt and st.button("Customize Description"):
|
102 |
+
try:
|
103 |
+
# Customize the description using Granite-13B-Chat
|
104 |
+
prompt = f"Customize the following product description based on the user's request:\n{st.session_state.generated_description}\nUser Request: {customization_prompt}"
|
105 |
+
model = ModelInference(model_id=ModelTypes.GRANITE_13B_CHAT_V2, params={
|
106 |
+
GenParams.DECODING_METHOD: DecodingMethods.GREEDY,
|
107 |
+
GenParams.MIN_NEW_TOKENS: 50,
|
108 |
+
GenParams.MAX_NEW_TOKENS: 200,
|
109 |
+
GenParams.STOP_SEQUENCES: ["\n"]
|
110 |
+
}, credentials=credentials, project_id=project_id)
|
111 |
+
|
112 |
+
with st.spinner("Customizing product description..."):
|
113 |
+
customization_response = model.generate_text(prompt=prompt)
|
114 |
+
st.session_state.customized_description = customization_response
|
115 |
+
st.success("Product description customized!")
|
116 |
+
st.write(customization_response)
|
117 |
+
except Exception as e:
|
118 |
+
st.error(f"An error occurred while customizing the description: {e}")
|
119 |
|
120 |
+
else:
|
121 |
+
st.error("IBM WatsonX API credentials are not set. Please check your environment variables.")
|