Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,87 +1,225 @@
|
|
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
|
|
|
5 |
from ibm_watsonx_ai.foundation_models.utils.enums import ModelTypes, DecodingMethods
|
|
|
6 |
from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams
|
|
|
7 |
import os
|
8 |
|
9 |
-
|
|
|
|
|
|
|
10 |
st.set_page_config(page_title="AI Product Design & Development", layout="wide")
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
# Sidebar - User inputs for Product Specifications
|
|
|
13 |
st.sidebar.title("Product Specifications")
|
|
|
14 |
product_name = st.sidebar.text_input("Product Name", "Example Product")
|
|
|
15 |
material = st.sidebar.selectbox("Material", ["Plastic", "Metal", "Wood", "Composite"])
|
|
|
16 |
dimensions = st.sidebar.text_input("Dimensions (L x W x H in cm)", "10 x 5 x 3")
|
|
|
17 |
constraints = st.sidebar.text_area("Design Constraints", "E.g., Must be lightweight, eco-friendly")
|
|
|
18 |
budget = st.sidebar.number_input("Budget ($)", min_value=0, value=1000)
|
19 |
|
|
|
|
|
20 |
st.sidebar.subheader("Project Info")
|
|
|
21 |
st.sidebar.text("AI-Powered Product Design")
|
22 |
|
|
|
|
|
23 |
# Main app title and description
|
|
|
24 |
st.title("AI Product Design & Development Tool")
|
|
|
25 |
st.markdown("""
|
|
|
26 |
Welcome to the AI-powered product design and development tool. This app leverages generative AI to accelerate the design process, optimize products for manufacturing, and simulate product performance.
|
|
|
27 |
""")
|
28 |
|
|
|
|
|
29 |
# Tabs for different sections of the app
|
|
|
30 |
tabs = st.tabs(["Design Generation", "Simulation", "Optimization"])
|
31 |
|
|
|
|
|
32 |
# IBM WatsonX API Setup
|
|
|
33 |
project_id = os.getenv('WATSONX_PROJECT_ID')
|
|
|
34 |
api_key = os.getenv('WATSONX_API_KEY')
|
35 |
|
|
|
|
|
36 |
if api_key and project_id:
|
|
|
37 |
credentials = Credentials(url="https://us-south.ml.cloud.ibm.com", api_key=api_key)
|
|
|
38 |
client = APIClient(credentials)
|
|
|
39 |
client.set.default_project(project_id)
|
40 |
|
|
|
|
|
41 |
parameters = {
|
|
|
42 |
GenParams.DECODING_METHOD: DecodingMethods.GREEDY,
|
|
|
43 |
GenParams.MIN_NEW_TOKENS: 50,
|
|
|
44 |
GenParams.MAX_NEW_TOKENS: 200,
|
|
|
45 |
GenParams.STOP_SEQUENCES: ["\n"]
|
|
|
46 |
}
|
47 |
|
48 |
-
|
|
|
|
|
|
|
49 |
model = ModelInference(model_id=model_id, params=parameters, credentials=credentials, project_id=project_id)
|
50 |
|
|
|
|
|
51 |
# Design Generation Tab
|
|
|
52 |
with tabs[0]:
|
|
|
53 |
st.header("Generate Product Designs")
|
|
|
54 |
st.write("Input your product specifications in the sidebar and click below to generate design concepts.")
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
if st.button("Ask for a cheaper variant"):
|
|
|
73 |
follow_up_prompt = prompt + "\nPlease suggest a cheaper variant."
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
if st.button("Explore alternative materials"):
|
|
|
79 |
follow_up_prompt = prompt + "\nPlease explore alternative materials that might better fit the design constraints."
|
80 |
-
follow_up_response = model.generate_text(prompt=follow_up_prompt, params=parameters)
|
81 |
-
st.info("Alternative Materials:")
|
82 |
-
st.write(follow_up_response)
|
83 |
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
# Simulation and Optimization tabs will be expanded in future steps.
|
|
|
1 |
import streamlit as st
|
2 |
+
|
3 |
from ibm_watsonx_ai import APIClient
|
4 |
+
|
5 |
from ibm_watsonx_ai import Credentials
|
6 |
+
|
7 |
from ibm_watsonx_ai.foundation_models import ModelInference
|
8 |
+
|
9 |
from ibm_watsonx_ai.foundation_models.utils.enums import ModelTypes, DecodingMethods
|
10 |
+
|
11 |
from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams
|
12 |
+
|
13 |
import os
|
14 |
|
15 |
+
|
16 |
+
|
17 |
+
# Set up page configuration
|
18 |
+
|
19 |
st.set_page_config(page_title="AI Product Design & Development", layout="wide")
|
20 |
|
21 |
+
|
22 |
+
|
23 |
+
# Initialize session state to keep track of queries
|
24 |
+
|
25 |
+
if 'query_count' not in st.session_state:
|
26 |
+
|
27 |
+
st.session_state.query_count = 0
|
28 |
+
|
29 |
+
if 'generated_response' not in st.session_state:
|
30 |
+
|
31 |
+
st.session_state.generated_response = None
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
# Limit the number of queries per session
|
36 |
+
|
37 |
+
MAX_QUERIES = 5
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
# Sidebar - User inputs for Product Specifications
|
42 |
+
|
43 |
st.sidebar.title("Product Specifications")
|
44 |
+
|
45 |
product_name = st.sidebar.text_input("Product Name", "Example Product")
|
46 |
+
|
47 |
material = st.sidebar.selectbox("Material", ["Plastic", "Metal", "Wood", "Composite"])
|
48 |
+
|
49 |
dimensions = st.sidebar.text_input("Dimensions (L x W x H in cm)", "10 x 5 x 3")
|
50 |
+
|
51 |
constraints = st.sidebar.text_area("Design Constraints", "E.g., Must be lightweight, eco-friendly")
|
52 |
+
|
53 |
budget = st.sidebar.number_input("Budget ($)", min_value=0, value=1000)
|
54 |
|
55 |
+
|
56 |
+
|
57 |
st.sidebar.subheader("Project Info")
|
58 |
+
|
59 |
st.sidebar.text("AI-Powered Product Design")
|
60 |
|
61 |
+
|
62 |
+
|
63 |
# Main app title and description
|
64 |
+
|
65 |
st.title("AI Product Design & Development Tool")
|
66 |
+
|
67 |
st.markdown("""
|
68 |
+
|
69 |
Welcome to the AI-powered product design and development tool. This app leverages generative AI to accelerate the design process, optimize products for manufacturing, and simulate product performance.
|
70 |
+
|
71 |
""")
|
72 |
|
73 |
+
|
74 |
+
|
75 |
# Tabs for different sections of the app
|
76 |
+
|
77 |
tabs = st.tabs(["Design Generation", "Simulation", "Optimization"])
|
78 |
|
79 |
+
|
80 |
+
|
81 |
# IBM WatsonX API Setup
|
82 |
+
|
83 |
project_id = os.getenv('WATSONX_PROJECT_ID')
|
84 |
+
|
85 |
api_key = os.getenv('WATSONX_API_KEY')
|
86 |
|
87 |
+
|
88 |
+
|
89 |
if api_key and project_id:
|
90 |
+
|
91 |
credentials = Credentials(url="https://us-south.ml.cloud.ibm.com", api_key=api_key)
|
92 |
+
|
93 |
client = APIClient(credentials)
|
94 |
+
|
95 |
client.set.default_project(project_id)
|
96 |
|
97 |
+
|
98 |
+
|
99 |
parameters = {
|
100 |
+
|
101 |
GenParams.DECODING_METHOD: DecodingMethods.GREEDY,
|
102 |
+
|
103 |
GenParams.MIN_NEW_TOKENS: 50,
|
104 |
+
|
105 |
GenParams.MAX_NEW_TOKENS: 200,
|
106 |
+
|
107 |
GenParams.STOP_SEQUENCES: ["\n"]
|
108 |
+
|
109 |
}
|
110 |
|
111 |
+
|
112 |
+
|
113 |
+
model_id = ModelTypes.GRANITE_13B_CHAT_V2 # Initial model, to be evaluated further
|
114 |
+
|
115 |
model = ModelInference(model_id=model_id, params=parameters, credentials=credentials, project_id=project_id)
|
116 |
|
117 |
+
|
118 |
+
|
119 |
# Design Generation Tab
|
120 |
+
|
121 |
with tabs[0]:
|
122 |
+
|
123 |
st.header("Generate Product Designs")
|
124 |
+
|
125 |
st.write("Input your product specifications in the sidebar and click below to generate design concepts.")
|
126 |
+
|
127 |
+
|
128 |
+
|
129 |
+
if st.session_state.query_count < MAX_QUERIES:
|
130 |
+
|
131 |
+
if st.button("Generate Design Concepts"):
|
132 |
+
|
133 |
+
prompt = f"""You are an AI specialized in product design. Generate creative product design concepts based on the following details:\n
|
134 |
+
|
135 |
+
Product Name: {product_name}\n
|
136 |
+
|
137 |
+
Material: {material}\n
|
138 |
+
|
139 |
+
Dimensions: {dimensions}\n
|
140 |
+
|
141 |
+
Constraints: {constraints}\n
|
142 |
+
|
143 |
+
Budget: {budget} USD\n
|
144 |
+
|
145 |
+
Provide detailed design concepts, explaining how they meet the constraints and budget. Also, suggest alternatives if the current design exceeds the budget or constraints."""
|
146 |
+
|
147 |
+
|
148 |
+
|
149 |
+
try:
|
150 |
+
|
151 |
+
with st.spinner("Generating design concepts..."):
|
152 |
+
|
153 |
+
response = model.generate_text(prompt=prompt, params=parameters)
|
154 |
+
|
155 |
+
st.session_state.generated_response = response
|
156 |
+
|
157 |
+
st.session_state.query_count += 1
|
158 |
+
|
159 |
+
st.success("Generated Design Concepts:")
|
160 |
+
|
161 |
+
st.write(response)
|
162 |
+
|
163 |
+
except Exception as e:
|
164 |
+
|
165 |
+
st.error(f"An error occurred: {e}")
|
166 |
+
|
167 |
+
else:
|
168 |
+
|
169 |
+
st.warning(f"You have reached the query limit of {MAX_QUERIES}. Please restart the session to continue.")
|
170 |
+
|
171 |
+
|
172 |
+
|
173 |
+
# Display the previous generated response and allow for follow-up queries
|
174 |
+
|
175 |
+
if st.session_state.generated_response:
|
176 |
+
|
177 |
+
st.subheader("Refine Your Design")
|
178 |
+
|
179 |
+
if st.session_state.query_count < MAX_QUERIES:
|
180 |
+
|
181 |
if st.button("Ask for a cheaper variant"):
|
182 |
+
|
183 |
follow_up_prompt = prompt + "\nPlease suggest a cheaper variant."
|
184 |
+
|
185 |
+
try:
|
186 |
+
|
187 |
+
follow_up_response = model.generate_text(prompt=follow_up_prompt, params=parameters)
|
188 |
+
|
189 |
+
st.session_state.query_count += 1
|
190 |
+
|
191 |
+
st.info("Cheaper Variant:")
|
192 |
+
|
193 |
+
st.write(follow_up_response)
|
194 |
+
|
195 |
+
except Exception as e:
|
196 |
+
|
197 |
+
st.error(f"An error occurred: {e}")
|
198 |
+
|
199 |
+
|
200 |
+
|
201 |
if st.button("Explore alternative materials"):
|
202 |
+
|
203 |
follow_up_prompt = prompt + "\nPlease explore alternative materials that might better fit the design constraints."
|
|
|
|
|
|
|
204 |
|
205 |
+
try:
|
206 |
+
|
207 |
+
follow_up_response = model.generate_text(prompt=follow_up_prompt, params=parameters)
|
208 |
+
|
209 |
+
st.session_state.query_count += 1
|
210 |
+
|
211 |
+
st.info("Alternative Materials:")
|
212 |
+
|
213 |
+
st.write(follow_up_response)
|
214 |
+
|
215 |
+
except Exception as e:
|
216 |
+
|
217 |
+
st.error(f"An error occurred: {e}")
|
218 |
+
|
219 |
+
else:
|
220 |
+
|
221 |
+
st.warning("You have reached the query limit.")
|
222 |
+
|
223 |
+
|
224 |
|
225 |
# Simulation and Optimization tabs will be expanded in future steps.
|