Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -26,20 +26,20 @@ data = load_json_from_url(json_url)
|
|
26 |
# Extract unique brands
|
27 |
brands = sorted(list(set([item["brand"] for item in data])))
|
28 |
|
29 |
-
#
|
30 |
def scrape_ethos_description(product_link):
|
31 |
try:
|
32 |
response = requests.get(product_link)
|
33 |
soup = BeautifulSoup(response.text, 'html.parser')
|
34 |
|
35 |
-
#
|
36 |
-
description = soup.
|
37 |
if description:
|
38 |
return description.get_text(strip=True)
|
39 |
else:
|
40 |
-
return "No detailed description available
|
41 |
except Exception as e:
|
42 |
-
return f"Error fetching details from Ethos
|
43 |
|
44 |
# Streamlit UI
|
45 |
st.title("Watch Description Generator")
|
@@ -67,19 +67,19 @@ if selected_brand != "Select":
|
|
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
|
76 |
ethos_description = scrape_ethos_description(product_link)
|
77 |
-
st.write("### Ethos Product Description")
|
78 |
st.write(ethos_description)
|
79 |
else:
|
80 |
st.warning("No Ethos link available for this SKU.")
|
81 |
|
82 |
-
# Generate watch description based on attributes and scraped content
|
83 |
attributes = {
|
84 |
"brand": watch_data["brand"],
|
85 |
"name": watch_data.get("name", "Unknown Watch"),
|
@@ -94,7 +94,7 @@ if selected_brand != "Select":
|
|
94 |
"strap_material": watch_data.get("strap_material", "Unknown Strap Material")
|
95 |
}
|
96 |
|
97 |
-
#
|
98 |
input_text = f"""Generate a detailed 100-word description for the following watch:
|
99 |
Brand: {attributes['brand']}
|
100 |
Name: {attributes['name']}
|
@@ -111,39 +111,39 @@ 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, similar to
|
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, #
|
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
|
133 |
-
st.write("### Generated Description")
|
134 |
st.write(description)
|
135 |
else:
|
136 |
st.warning("Please select a brand.")
|
137 |
|
138 |
-
#
|
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."
|
144 |
)
|
145 |
|
146 |
-
#
|
147 |
st.markdown(
|
148 |
"""
|
149 |
<style>
|
|
|
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")
|
|
|
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"),
|
|
|
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']}
|
|
|
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>
|