Prathamesh1420 commited on
Commit
1b8d4f7
·
verified ·
1 Parent(s): 69f8906

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -85
app.py CHANGED
@@ -3,98 +3,88 @@ import os
3
  from PIL import Image
4
  import numpy as np
5
  from chatbot import Chatbot # Assuming you have a chatbot module
6
-
7
 
8
- # Function to save uploaded file
9
- def save_uploaded_file(uploaded_file):
10
- try:
11
- if not os.path.exists('uploads'):
12
- os.makedirs('uploads')
13
- with open(os.path.join('uploads', uploaded_file.name), 'wb') as f:
14
- f.write(uploaded_file.getbuffer())
15
- return True
16
- except Exception as e:
17
- st.error(f"Error: {e}")
18
- return False
19
-
20
 
21
- # Function to show dashboard content
22
- def show_dashboard():
23
- st.title("Fashion Recommender System")
24
- st.write("Welcome to our Fashion Recommender System! Upload an image and get personalized product recommendations based on your image and queries.")
25
-
26
 
27
- chatbot = Chatbot()
28
- chatbot.load_data()
29
-
30
 
31
- # Load and set up the ResNet model
32
- uploaded_file = st.file_uploader("Upload an Image", type=['jpg', 'jpeg', 'png'])
33
-
34
- if uploaded_file:
35
- if save_uploaded_file(uploaded_file):
36
- st.sidebar.header("Uploaded Image")
37
- display_image = Image.open(uploaded_file)
38
- st.sidebar.image(display_image, caption='Uploaded Image', use_column_width=True)
39
-
40
- # Generate image caption
41
- image_path = os.path.join("uploads", uploaded_file.name)
42
- caption = chatbot.generate_image_caption(image_path)
43
- st.write("### Generated Caption")
44
- st.write(caption)
45
-
46
- # Use caption to get product recommendations
47
- _, recommended_products = chatbot.generate_response(caption)
48
-
49
 
50
- st.write("### Recommended Products")
51
- col1, col2, col3, col4, col5 = st.columns(5)
52
- for i, idx in enumerate(recommended_products[:5]):
53
- with col1 if i == 0 else col2 if i == 1 else col3 if i == 2 else col4 if i == 3 else col5:
54
- product_image = chatbot.images[idx['corpus_id']]
55
- st.image(product_image, caption=f"Product {i+1}", width=150)
56
- else:
57
- st.error("Error in uploading the file.")
58
-
59
 
60
- # Chatbot section
61
- st.write("### Chat with our Fashion Assistant")
62
- user_question = st.text_input("Ask a question about fashion:")
63
- if user_question:
64
- bot_response, recommended_products = chatbot.generate_response(user_question)
65
- st.write("**Chatbot Response:**")
66
- st.write(bot_response)
67
-
68
 
69
- # Display recommended products based on the user question
70
- st.write("**Recommended Products:**")
71
- for result in recommended_products:
72
- pid = result['corpus_id']
73
- product_info = chatbot.product_data[pid]
74
- st.write(f"**Product Name:** {product_info['productDisplayName']}")
75
- st.write(f"**Category:** {product_info['masterCategory']}")
76
- st.write(f"**Article Type:** {product_info['articleType']}")
77
- st.write(f"**Usage:** {product_info['usage']}")
78
- st.write(f"**Season:** {product_info['season']}")
79
- st.write(f"**Gender:** {product_info['gender']}")
80
- st.image(chatbot.images[pid], width=150)
81
-
82
 
83
- # Main Streamlit app
84
- def main():
85
- # Set page configuration
86
- st.set_page_config(
87
- page_title="Fashion Recommender System",
88
- page_icon=":dress:",
89
- layout="wide",
90
- initial_sidebar_state="expanded"
91
- )
92
-
93
 
94
- # Show dashboard content directly
95
- show_dashboard()
96
-
97
 
98
- # Run the main app
99
- if __name__ == "__main__":
100
- main()
 
3
  from PIL import Image
4
  import numpy as np
5
  from chatbot import Chatbot # Assuming you have a chatbot module
 
6
 
7
+ # Function to save uploaded file
8
+ def save_uploaded_file(uploaded_file):
9
+ try:
10
+ if not os.path.exists('uploads'):
11
+ os.makedirs('uploads')
12
+ with open(os.path.join('uploads', uploaded_file.name), 'wb') as f:
13
+ f.write(uploaded_file.getbuffer())
14
+ return True
15
+ except Exception as e:
16
+ st.error(f"Error: {e}")
17
+ return False
 
18
 
19
+ # Function to show dashboard content
20
+ def show_dashboard():
21
+ st.title("Fashion Recommender System")
22
+ st.write("Welcome to our Fashion Recommender System! Upload an image and get personalized product recommendations based on your image and queries.")
 
23
 
24
+ chatbot = Chatbot()
25
+ chatbot.load_data()
 
26
 
27
+ # Load and set up the ResNet model
28
+ uploaded_file = st.file_uploader("Upload an Image", type=['jpg', 'jpeg', 'png'])
29
+
30
+ if uploaded_file:
31
+ if save_uploaded_file(uploaded_file):
32
+ st.sidebar.header("Uploaded Image")
33
+ display_image = Image.open(uploaded_file)
34
+ st.sidebar.image(display_image, caption='Uploaded Image', use_column_width=True)
35
+
36
+ # Generate image caption
37
+ image_path = os.path.join("uploads", uploaded_file.name)
38
+ caption = chatbot.generate_image_caption(image_path)
39
+ st.write("### Generated Caption")
40
+ st.write(caption)
41
+
42
+ # Use caption to get product recommendations
43
+ _, recommended_products = chatbot.generate_response(caption)
 
44
 
45
+ st.write("### Recommended Products")
46
+ col1, col2, col3, col4, col5 = st.columns(5)
47
+ for i, idx in enumerate(recommended_products[:5]):
48
+ with col1 if i == 0 else col2 if i == 1 else col3 if i == 2 else col4 if i == 3 else col5:
49
+ product_image = chatbot.images[idx['corpus_id']]
50
+ st.image(product_image, caption=f"Product {i+1}", width=150)
51
+ else:
52
+ st.error("Error in uploading the file.")
 
53
 
54
+ # Chatbot section
55
+ st.write("### Chat with our Fashion Assistant")
56
+ user_question = st.text_input("Ask a question about fashion:")
57
+ if user_question:
58
+ bot_response, recommended_products = chatbot.generate_response(user_question)
59
+ st.write("**Chatbot Response:**")
60
+ st.write(bot_response)
 
61
 
62
+ # Display recommended products based on the user question
63
+ st.write("**Recommended Products:**")
64
+ for result in recommended_products:
65
+ pid = result['corpus_id']
66
+ product_info = chatbot.product_data[pid]
67
+ st.write(f"**Product Name:** {product_info['productDisplayName']}")
68
+ st.write(f"**Category:** {product_info['masterCategory']}")
69
+ st.write(f"**Article Type:** {product_info['articleType']}")
70
+ st.write(f"**Usage:** {product_info['usage']}")
71
+ st.write(f"**Season:** {product_info['season']}")
72
+ st.write(f"**Gender:** {product_info['gender']}")
73
+ st.image(chatbot.images[pid], width=150)
 
74
 
75
+ # Main Streamlit app
76
+ def main():
77
+ # Set page configuration
78
+ st.set_page_config(
79
+ page_title="Fashion Recommender System",
80
+ page_icon=":dress:",
81
+ layout="wide",
82
+ initial_sidebar_state="expanded"
83
+ )
 
84
 
85
+ # Show dashboard content directly
86
+ show_dashboard()
 
87
 
88
+ # Run the main app
89
+ if __name__ == "__main__":
90
+ main()