shreyanshjha0709 commited on
Commit
7c2acfb
·
verified ·
1 Parent(s): caabb16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -145
app.py CHANGED
@@ -1,101 +1,8 @@
1
- import streamlit as st
2
- import requests
3
- from bs4 import BeautifulSoup
4
- import json
5
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
6
 
7
- # Load model and tokenizer
8
- @st.cache_resource
9
- def load_model():
10
- model = AutoModelForSeq2SeqLM.from_pretrained("shreyanshjha0709/watch-description-generator")
11
- tokenizer = AutoTokenizer.from_pretrained("shreyanshjha0709/watch-description-generator")
12
- return model, tokenizer
13
 
14
- model, tokenizer = load_model()
15
-
16
- # Load the JSON file from a URL
17
- @st.cache_data
18
- def load_json_from_url(url):
19
- response = requests.get(url)
20
- return response.json()
21
-
22
- # Provide your JSON URL here
23
- json_url = "https://www.ethoswatches.com/feeds/holbox_ai.json"
24
- data = load_json_from_url(json_url)
25
-
26
- # Extract unique brands
27
- brands = sorted(list(set([item["brand"] for item in data])))
28
-
29
- # Function to scrape Ethos product description using the specified selector
30
- def scrape_ethos_description(product_link):
31
- try:
32
- response = requests.get(product_link)
33
- soup = BeautifulSoup(response.text, 'html.parser')
34
-
35
- # Target the specific selector to extract the description
36
- description = soup.select_one('#brand_collection > div > div.lHeight_100.spec_editorNotes > div > p:nth-child(5)')
37
- if description:
38
- return description.get_text(strip=True)
39
- else:
40
- return "No detailed description available from Ethos."
41
- except Exception as e:
42
- return f"Error fetching details from Ethos: {str(e)}"
43
-
44
- # Streamlit UI
45
- st.title("Watch Description Generator")
46
-
47
- # Select brand
48
- selected_brand = st.selectbox("Select a Brand", ["Select"] + brands)
49
-
50
- # Filter watches and SKUs by the selected brand
51
- if selected_brand != "Select":
52
- watches = [item["name"] for item in data if item["brand"] == selected_brand]
53
- skus = [item["sku"] for item in data if item["brand"] == selected_brand]
54
- selected_watch = st.selectbox("Select Watch Name (Optional)", ["Select"] + watches)
55
- selected_sku = st.selectbox("Select SKU (Optional)", ["Select"] + skus)
56
-
57
- # Get the selected watch data from the JSON
58
- watch_data = None
59
- if selected_watch != "Select":
60
- watch_data = next((item for item in data if item["name"] == selected_watch), None)
61
- elif selected_sku != "Select":
62
- watch_data = next((item for item in data if item["sku"] == selected_sku), None)
63
-
64
- if watch_data:
65
- # Display the image from the JSON
66
- image_url = watch_data.get("image", None)
67
- if image_url:
68
- st.image(image_url, caption=f"{watch_data['name']} Image")
69
-
70
- # Get the Ethos product link for web scraping
71
- product_link = watch_data.get("url", None)
72
- if product_link:
73
- st.write(f"Fetching details from: [Product Page]({product_link})")
74
-
75
- # Scrape Ethos product page for description
76
- ethos_description = scrape_ethos_description(product_link)
77
- st.write("### Ethos Product Description (Extracted)")
78
- st.write(ethos_description)
79
- else:
80
- st.warning("No Ethos link available for this SKU.")
81
-
82
- # Generate a watch description based on attributes and scraped content
83
- attributes = {
84
- "brand": watch_data["brand"],
85
- "name": watch_data.get("name", "Unknown Watch"),
86
- "sku": watch_data.get("sku", "Unknown SKU"),
87
- "features": watch_data.get("features", "Unknown Features"),
88
- "casesize": watch_data.get("casesize", "Unknown Case Size"),
89
- "movement": watch_data.get("movement", "Unknown Movement"),
90
- "gender": watch_data.get("gender", "Unknown Gender"),
91
- "water_resistance": watch_data.get("water_resistance", "Unknown Water Resistance"),
92
- "power_reserve": watch_data.get("power_reserve", "Unknown Power Reserve"),
93
- "dial_color": watch_data.get("dial_color", "Unknown Dial Color"),
94
- "strap_material": watch_data.get("strap_material", "Unknown Strap Material")
95
- }
96
-
97
- # Combine Ethos description and attributes into a prompt
98
- input_text = f"""Generate a detailed 100-word description for the following watch:
99
  Brand: {attributes['brand']}
100
  Name: {attributes['name']}
101
  SKU: {attributes['sku']}
@@ -111,55 +18,28 @@ Strap Material: {attributes['strap_material']}
111
  Additional details from Ethos:
112
  {ethos_description}
113
 
114
- Description: Provide a luxurious, detailed description focusing on the craftsmanship, innovation, and design, in a style similar to high-end editorials."""
115
-
116
- # Tokenize input and generate description
117
- inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
118
- outputs = model.generate(
119
- **inputs,
120
- max_length=150, # Output length 100-150 words
121
- num_return_sequences=1,
122
- temperature=0.7,
123
- top_k=50,
124
- top_p=0.95,
125
- do_sample=True,
126
- repetition_penalty=1.2 # Prevent repetition
127
- )
128
-
129
- # Decode generated text
130
- description = tokenizer.decode(outputs[0], skip_special_tokens=True)
131
 
132
- # Display the final generated description
133
- st.write("### Final Generated Description")
134
- st.write(description)
135
- else:
136
- st.warning("Please select a brand.")
137
 
138
- # Sidebar information
139
- st.sidebar.title("About")
140
- st.sidebar.info(
141
- "This app uses a fine-tuned AI model to generate descriptions for watches. "
142
- "Select a brand and a watch to get started. The model will generate a unique "
143
- "description based on the watch's attributes and additional details from the Ethos website."
144
- )
145
 
146
- # Footer
147
- st.markdown(
148
- """
149
- <style>
150
- .footer {
151
- position: fixed;
152
- left: 0;
153
- bottom: 0;
154
- width: 100%;
155
- background-color: #f1f1f1;
156
- color: black;
157
- text-align: center;
158
- }
159
- </style>
160
- <div class="footer">
161
- <p>Developed with ❤️ by Shreyansh Jha</p>
162
- </div>
163
- """,
164
- unsafe_allow_html=True
165
- )
 
1
+ # ... (previous code remains the same)
 
 
 
 
2
 
3
+ # Combine Ethos description and attributes into a prompt
4
+ input_text = f"""Generate a detailed, luxurious 150-word description for the following watch, focusing on its craftsmanship, innovation, and design. Use a style similar to high-end watch editorials, highlighting the watch's unique features and its appeal to connoisseurs:
 
 
 
 
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  Brand: {attributes['brand']}
7
  Name: {attributes['name']}
8
  SKU: {attributes['sku']}
 
18
  Additional details from Ethos:
19
  {ethos_description}
20
 
21
+ Description:"""
22
+
23
+ # Tokenize input and generate description
24
+ inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
25
+ outputs = model.generate(
26
+ **inputs,
27
+ max_length=300, # Increased to allow for longer descriptions
28
+ min_length=200, # Ensure a minimum length
29
+ num_return_sequences=1,
30
+ temperature=0.8, # Slightly increased for more creativity
31
+ top_k=50,
32
+ top_p=0.95,
33
+ do_sample=True,
34
+ repetition_penalty=1.2, # Prevent repetition
35
+ length_penalty=1.5 # Encourage longer outputs
36
+ )
 
37
 
38
+ # Decode generated text
39
+ description = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
40
 
41
+ # Display the final generated description
42
+ st.write("### Final Generated Description")
43
+ st.write(description)
 
 
 
 
44
 
45
+ # ... (rest of the code remains the same)