Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,33 @@ import plotly.express as px
|
|
12 |
import time
|
13 |
import threading
|
14 |
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
# Database setup for user authentication
|
17 |
def init_db():
|
@@ -216,81 +243,34 @@ with gr.Blocks() as app:
|
|
216 |
</div>
|
217 |
""")
|
218 |
gr.Markdown("# 🔐 Circular Economy Marketplace")
|
|
|
|
|
219 |
with gr.Tab("Login/Register"):
|
220 |
-
#
|
221 |
-
|
222 |
-
<div style="text-align: center;">
|
223 |
-
<img src="https://miro.medium.com/v2/resize:fit:1157/0*DmOXUHYIA7vl5QgR.jpeg?text=Login+or+Register" alt="Login/Register" style="width: 100%; max-width: 400px;">
|
224 |
-
</div>
|
225 |
-
""")
|
226 |
-
with gr.Tab("Register"):
|
227 |
-
reg_username = gr.Textbox(label="Username")
|
228 |
-
reg_password = gr.Textbox(label="Password", type="password")
|
229 |
-
reg_btn = gr.Button("Register")
|
230 |
-
reg_output = gr.Textbox()
|
231 |
-
reg_btn.click(register, inputs=[reg_username, reg_password], outputs=reg_output)
|
232 |
-
with gr.Tab("Login"):
|
233 |
-
log_username = gr.Textbox(label="Username")
|
234 |
-
log_password = gr.Textbox(label="Password", type="password")
|
235 |
-
log_btn = gr.Button("Login")
|
236 |
-
log_output = gr.Textbox()
|
237 |
-
log_btn.click(login, inputs=[log_username, log_password], outputs=log_output)
|
238 |
with gr.Tab("Product Lifecycle Prediction"):
|
239 |
-
|
240 |
-
|
241 |
-
gr.Textbox(label="Product Name"),
|
242 |
-
gr.Number(label="Price"),
|
243 |
-
gr.Number(label="Rating"),
|
244 |
-
gr.Number(label="NumReviews"),
|
245 |
-
gr.Number(label="StockQuantity"),
|
246 |
-
gr.Number(label="Discount")
|
247 |
-
]
|
248 |
-
lifecycle_output = gr.Textbox(label="Prediction")
|
249 |
-
lifecycle_btn = gr.Button("Predict")
|
250 |
-
lifecycle_btn.click(predict_lifecycle, inputs=lifecycle_inputs, outputs=lifecycle_output)
|
251 |
with gr.Tab("Dynamic Pricing"):
|
252 |
-
|
253 |
-
|
254 |
-
"LG Refrigerator", "HP Printer", "Bose Speaker"], label="Product Name"),
|
255 |
-
gr.Dropdown(["Electronics", "Fashion", "Home Appliances"], label="Category"),
|
256 |
-
gr.Number(label="Base Price"),
|
257 |
-
gr.Number(label="Competitor Price"),
|
258 |
-
gr.Dropdown(["Low", "Medium", "High"], label="Demand"),
|
259 |
-
gr.Number(label="Stock"),
|
260 |
-
gr.Number(label="Reviews"),
|
261 |
-
gr.Number(label="Rating"),
|
262 |
-
gr.Dropdown(["Holiday", "Summer", "Winter", "Off-season"], label="Season"),
|
263 |
-
gr.Number(label="Discount (%)")
|
264 |
-
]
|
265 |
-
pricing_output = gr.Textbox(label="Predicted Price")
|
266 |
-
pricing_btn = gr.Button("Predict")
|
267 |
-
pricing_btn.click(predict_price, inputs=pricing_inputs, outputs=pricing_output)
|
268 |
with gr.Tab("Product Recommendation"):
|
269 |
-
#
|
270 |
-
|
271 |
-
<div style="text-align: center;">
|
272 |
-
<img src="https://wizzy.ai/blog/wp-content/uploads/2021/04/Product-Recommendation-Implementation.jpg?text=Product+Recommendations" alt="Product Recommendations" style="width: 100%; max-width: 400px;">
|
273 |
-
</div>
|
274 |
-
""")
|
275 |
-
recommendation_input = gr.Dropdown(choices=df_recommendation['category'].unique().tolist(), label="Select Product Category")
|
276 |
-
recommendation_output = gr.Dataframe()
|
277 |
-
recommendation_btn = gr.Button("Recommend")
|
278 |
-
recommendation_btn.click(recommend_products, inputs=recommendation_input, outputs=recommendation_output)
|
279 |
with gr.Tab("Circular Economy Analytics"):
|
280 |
-
#
|
|
|
|
|
|
|
281 |
gr.Markdown("""
|
282 |
<div style="text-align: center;">
|
283 |
-
<img src="https://
|
284 |
</div>
|
285 |
""")
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
gr.Plot(label="Sustainability & Recycling Insights")
|
291 |
-
]
|
292 |
-
dashboard_btn = gr.Button("Generate Dashboard")
|
293 |
-
dashboard_btn.click(generate_dashboard, inputs=[], outputs=dashboard_outputs)
|
294 |
|
295 |
# Simulate real-time data updates
|
296 |
def live_update():
|
|
|
12 |
import time
|
13 |
import threading
|
14 |
import random
|
15 |
+
import requests
|
16 |
+
import os # Import the os module to access environment variables
|
17 |
+
|
18 |
+
# Hugging Face API configuration
|
19 |
+
HUGGINGFACE_API_URL = ""https://router.huggingface.co/hf-inference/models/google-bert/bert-base-multilingual-cased"
|
20 |
+
|
21 |
+
HUGGINGFACE_API_KEY = os.environ["HUGGINGFACE_API_KEY"] # Access the API key from environment variables
|
22 |
+
|
23 |
+
# Hugging Face Chatbot Function
|
24 |
+
def huggingface_chatbot(user_input):
|
25 |
+
try:
|
26 |
+
headers = {
|
27 |
+
"Authorization": f"Bearer {HUGGINGFACE_API_KEY}",
|
28 |
+
"Content-Type": "application/json"
|
29 |
+
}
|
30 |
+
data = {
|
31 |
+
"inputs": user_input,
|
32 |
+
"parameters": {
|
33 |
+
"max_length": 100, # Adjust as needed
|
34 |
+
"temperature": 0.7 # Adjust as needed
|
35 |
+
}
|
36 |
+
}
|
37 |
+
response = requests.post(HUGGINGFACE_API_URL, headers=headers, json=data)
|
38 |
+
response.raise_for_status() # Raise an error for bad status codes
|
39 |
+
return response.json()[0]["generated_text"]
|
40 |
+
except Exception as e:
|
41 |
+
return f"Error: {str(e)}"
|
42 |
|
43 |
# Database setup for user authentication
|
44 |
def init_db():
|
|
|
243 |
</div>
|
244 |
""")
|
245 |
gr.Markdown("# 🔐 Circular Economy Marketplace")
|
246 |
+
|
247 |
+
# Existing tabs (Login/Register, Product Lifecycle Prediction, etc.)
|
248 |
with gr.Tab("Login/Register"):
|
249 |
+
# ... (existing code)
|
250 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
251 |
with gr.Tab("Product Lifecycle Prediction"):
|
252 |
+
# ... (existing code)
|
253 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
with gr.Tab("Dynamic Pricing"):
|
255 |
+
# ... (existing code)
|
256 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
257 |
with gr.Tab("Product Recommendation"):
|
258 |
+
# ... (existing code)
|
259 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
260 |
with gr.Tab("Circular Economy Analytics"):
|
261 |
+
# ... (existing code)
|
262 |
+
|
263 |
+
# Add a new tab for the Hugging Face Chatbot
|
264 |
+
with gr.Tab("AI Chatbot"):
|
265 |
gr.Markdown("""
|
266 |
<div style="text-align: center;">
|
267 |
+
<img src="https://via.placeholder.com/400x200.png?text=AI+Chatbot" alt="Chatbot" style="width: 100%; max-width: 400px;">
|
268 |
</div>
|
269 |
""")
|
270 |
+
chatbot_input = gr.Textbox(label="Ask me anything about circular economy, product lifecycle, dynamic pricing, and recommendations!")
|
271 |
+
chatbot_output = gr.Textbox(label="AI Response")
|
272 |
+
chatbot_btn = gr.Button("Ask")
|
273 |
+
chatbot_btn.click(huggingface_chatbot, inputs=chatbot_input, outputs=chatbot_output)
|
|
|
|
|
|
|
|
|
274 |
|
275 |
# Simulate real-time data updates
|
276 |
def live_update():
|