Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,5 @@
|
|
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
|
@@ -24,22 +22,7 @@ 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(
|
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")
|
@@ -47,7 +30,6 @@ st.title("Watch Description Generator")
|
|
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]
|
@@ -55,31 +37,14 @@ if selected_brand != "Select":
|
|
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
|
67 |
-
if image_url:
|
68 |
st.image(image_url, caption=f"{watch_data['name']} Image")
|
69 |
|
70 |
-
#
|
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,8 +59,8 @@ 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
|
99 |
Brand: {attributes['brand']}
|
100 |
Name: {attributes['name']}
|
101 |
SKU: {attributes['sku']}
|
@@ -108,46 +73,44 @@ Power Reserve: {attributes['power_reserve']}
|
|
108 |
Dial Color: {attributes['dial_color']}
|
109 |
Strap Material: {attributes['strap_material']}
|
110 |
|
111 |
-
|
112 |
-
{ethos_description}
|
113 |
-
|
114 |
-
Description:"""
|
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 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
)
|
130 |
|
131 |
# Decode generated text
|
132 |
description = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
133 |
|
134 |
-
# Display the
|
135 |
-
st.write("###
|
136 |
st.write(description)
|
137 |
-
|
138 |
-
|
|
|
|
|
139 |
else:
|
140 |
st.warning("Please select a brand.")
|
141 |
|
142 |
-
#
|
143 |
st.sidebar.title("About")
|
144 |
st.sidebar.info(
|
145 |
"This app uses a fine-tuned AI model to generate descriptions for watches. "
|
146 |
"Select a brand and a watch to get started. The model will generate a unique "
|
147 |
-
"description based on the watch's attributes
|
148 |
)
|
149 |
|
150 |
-
#
|
151 |
st.markdown(
|
152 |
"""
|
153 |
<style>
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
|
|
|
|
3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
4 |
|
5 |
# Load model and tokenizer
|
|
|
22 |
data = load_json_from_url(json_url)
|
23 |
|
24 |
# Extract unique brands
|
25 |
+
brands = sorted(set(item["brand"] for item in data))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# Streamlit UI
|
28 |
st.title("Watch Description Generator")
|
|
|
30 |
# Select brand
|
31 |
selected_brand = st.selectbox("Select a Brand", ["Select"] + brands)
|
32 |
|
|
|
33 |
if selected_brand != "Select":
|
34 |
watches = [item["name"] for item in data if item["brand"] == selected_brand]
|
35 |
skus = [item["sku"] for item in data if item["brand"] == selected_brand]
|
|
|
37 |
selected_sku = st.selectbox("Select SKU (Optional)", ["Select"] + skus)
|
38 |
|
39 |
# Get the selected watch data from the JSON
|
40 |
+
watch_data = next((item for item in data if item["name"] == selected_watch or item["sku"] == selected_sku), None)
|
|
|
|
|
|
|
|
|
41 |
|
42 |
if watch_data:
|
43 |
# Display the image from the JSON
|
44 |
+
if image_url := watch_data.get("image"):
|
|
|
45 |
st.image(image_url, caption=f"{watch_data['name']} Image")
|
46 |
|
47 |
+
# Attributes without price
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
attributes = {
|
49 |
"brand": watch_data["brand"],
|
50 |
"name": watch_data.get("name", "Unknown Watch"),
|
|
|
59 |
"strap_material": watch_data.get("strap_material", "Unknown Strap Material")
|
60 |
}
|
61 |
|
62 |
+
# Create a detailed description prompt
|
63 |
+
input_text = f"""Generate a detailed 200-word description for the following watch:
|
64 |
Brand: {attributes['brand']}
|
65 |
Name: {attributes['name']}
|
66 |
SKU: {attributes['sku']}
|
|
|
73 |
Dial Color: {attributes['dial_color']}
|
74 |
Strap Material: {attributes['strap_material']}
|
75 |
|
76 |
+
Description: Provide a luxurious, detailed description focusing on the craftsmanship, innovation, and design. Highlight the unique features and selling points of this watch. Use vivid language to paint a picture of the watch's appearance and functionality. Discuss how this watch stands out in the {attributes['brand']} collection and why it would appeal to watch enthusiasts."""
|
|
|
|
|
|
|
77 |
|
78 |
# Tokenize input and generate description
|
79 |
+
inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
|
80 |
+
outputs = model.generate(
|
81 |
+
**inputs,
|
82 |
+
max_length=300, # Increased to allow for longer descriptions
|
83 |
+
num_return_sequences=1,
|
84 |
+
temperature=0.8,
|
85 |
+
top_k=50,
|
86 |
+
top_p=0.95,
|
87 |
+
do_sample=True,
|
88 |
+
repetition_penalty=1.2,
|
89 |
+
no_repeat_ngram_size=3 # Prevent repetition of 3-gram phrases
|
90 |
+
)
|
|
|
91 |
|
92 |
# Decode generated text
|
93 |
description = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
94 |
|
95 |
+
# Display the result
|
96 |
+
st.write("### Generated Description")
|
97 |
st.write(description)
|
98 |
+
|
99 |
+
# Add word count
|
100 |
+
word_count = len(description.split())
|
101 |
+
st.write(f"Word count: {word_count}")
|
102 |
else:
|
103 |
st.warning("Please select a brand.")
|
104 |
|
105 |
+
# Add some information about the app
|
106 |
st.sidebar.title("About")
|
107 |
st.sidebar.info(
|
108 |
"This app uses a fine-tuned AI model to generate descriptions for watches. "
|
109 |
"Select a brand and a watch to get started. The model will generate a unique "
|
110 |
+
"description based on the watch's attributes."
|
111 |
)
|
112 |
|
113 |
+
# Add a footer
|
114 |
st.markdown(
|
115 |
"""
|
116 |
<style>
|