Vorxart commited on
Commit
847adf4
·
verified ·
1 Parent(s): f4a502f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -162
app.py CHANGED
@@ -1,225 +1,129 @@
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.
 
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
  # Set up page configuration
 
10
  st.set_page_config(page_title="AI Product Design & Development", layout="wide")
11
 
12
+ # Initialize session state to track queries and responses
 
 
 
13
  if 'query_count' not in st.session_state:
 
14
  st.session_state.query_count = 0
 
15
  if 'generated_response' not in st.session_state:
 
16
  st.session_state.generated_response = None
17
 
 
 
18
  # Limit the number of queries per session
 
19
  MAX_QUERIES = 5
20
 
 
 
21
  # Sidebar - User inputs for Product Specifications
 
22
  st.sidebar.title("Product Specifications")
 
23
  product_name = st.sidebar.text_input("Product Name", "Example Product")
 
24
  material = st.sidebar.selectbox("Material", ["Plastic", "Metal", "Wood", "Composite"])
 
25
  dimensions = st.sidebar.text_input("Dimensions (L x W x H in cm)", "10 x 5 x 3")
 
26
  constraints = st.sidebar.text_area("Design Constraints", "E.g., Must be lightweight, eco-friendly")
 
27
  budget = st.sidebar.number_input("Budget ($)", min_value=0, value=1000)
28
 
 
 
29
  st.sidebar.subheader("Project Info")
 
30
  st.sidebar.text("AI-Powered Product Design")
31
 
 
 
32
  # Main app title and description
 
33
  st.title("AI Product Design & Development Tool")
 
34
  st.markdown("""
 
35
  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.
 
36
  """)
37
 
38
+ # Model Selection
39
+ model_choice = st.sidebar.selectbox(
40
+ "Choose AI Model",
41
+ options=[
42
+ "Granite-13B-Chat-V2 (Text Generation)",
43
+ "Granite-13B-Instruct-V2 (Detailed Instructions)",
44
+ "Granite-20B-Multilingual (Multilingual Support)"
45
+ ]
46
+ )
47
 
48
  # IBM WatsonX API Setup
 
49
  project_id = os.getenv('WATSONX_PROJECT_ID')
 
50
  api_key = os.getenv('WATSONX_API_KEY')
51
 
 
 
52
  if api_key and project_id:
 
53
  credentials = Credentials(url="https://us-south.ml.cloud.ibm.com", api_key=api_key)
 
54
  client = APIClient(credentials)
 
55
  client.set.default_project(project_id)
56
 
57
+ # Model Mapping
58
+ model_mapping = {
59
+ "Granite-13B-Chat-V2 (Text Generation)": ModelTypes.GRANITE_13B_CHAT_V2,
60
+ "Granite-13B-Instruct-V2 (Detailed Instructions)": ModelTypes.GRANITE_13B_INSTRUCT_V2,
61
+ "Granite-20B-Multilingual (Multilingual Support)": ModelTypes.GRANITE_20B_MULTILINGUAL
62
+ }
63
 
64
+ selected_model = model_mapping[model_choice]
65
  parameters = {
 
66
  GenParams.DECODING_METHOD: DecodingMethods.GREEDY,
 
67
  GenParams.MIN_NEW_TOKENS: 50,
 
68
  GenParams.MAX_NEW_TOKENS: 200,
 
69
  GenParams.STOP_SEQUENCES: ["\n"]
 
70
  }
71
 
72
+ model = ModelInference(model_id=selected_model, params=parameters, credentials=credentials, project_id=project_id)
73
 
74
+ # Chat Interaction Box
75
+ st.subheader("Chat with AI for Product Design")
76
+ user_input = st.text_input("Ask AI a question related to product design", "")
77
 
78
+ if user_input and st.button("Submit Query"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  if st.session_state.query_count < MAX_QUERIES:
80
+ # Build the prompt based on user input and specifications
81
+ prompt = f"""You are an AI specialized in product design. Based on the following details:\n
82
+ Product Name: {product_name}\n
83
+ Material: {material}\n
84
+ Dimensions: {dimensions}\n
85
+ Constraints: {constraints}\n
86
+ Budget: {budget} USD\n
87
+ User Query: {user_input}\n
88
+ Please provide a detailed response."""
89
+
90
+ try:
91
+ with st.spinner("Generating response..."):
92
+ response = model.generate_text(prompt=prompt, params=parameters)
93
+ st.session_state.generated_response = response
94
+ st.session_state.query_count += 1
95
+ st.success("AI Response:")
96
+ st.write(response)
97
+ except Exception as e:
98
+ st.error(f"An error occurred: {e}")
99
+ else:
100
+ st.warning(f"You have reached the query limit of {MAX_QUERIES}. Please restart the session to continue.")
101
 
102
+ # Follow-up queries
103
+ if st.session_state.generated_response:
104
+ st.subheader("Refine Your Design")
105
+
106
+ if st.session_state.query_count < MAX_QUERIES:
107
+ if st.button("Ask for a cheaper variant"):
108
+ follow_up_prompt = prompt + "\nPlease suggest a cheaper variant."
 
 
 
 
 
 
 
 
 
 
 
109
  try:
110
+ follow_up_response = model.generate_text(prompt=follow_up_prompt, params=parameters)
111
+ st.session_state.query_count += 1
112
+ st.info("Cheaper Variant:")
113
+ st.write(follow_up_response)
 
 
 
 
 
 
 
 
 
114
  except Exception as e:
 
115
  st.error(f"An error occurred: {e}")
116
 
117
+ if st.button("Explore alternative materials"):
118
+ follow_up_prompt = prompt + "\nPlease explore alternative materials that might better fit the design constraints."
119
+ try:
120
+ follow_up_response = model.generate_text(prompt=follow_up_prompt, params=parameters)
121
+ st.session_state.query_count += 1
122
+ st.info("Alternative Materials:")
123
+ st.write(follow_up_response)
124
+ except Exception as e:
125
+ st.error(f"An error occurred: {e}")
126
  else:
127
+ st.warning("You have reached the query limit.")
128
+ else:
129
+ st.error("IBM WatsonX API credentials are not set. Please check your environment variables.")