shreyanshjha0709 commited on
Commit
caabb16
·
verified ·
1 Parent(s): 27419d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -18
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
- # Web Scraping function to get product description from Ethos website
30
  def scrape_ethos_description(product_link):
31
  try:
32
  response = requests.get(product_link)
33
  soup = BeautifulSoup(response.text, 'html.parser')
34
 
35
- # Assuming product description is within a specific class or ID, find it
36
- description = soup.find('div', class_='product-description') # Example class, adjust based on actual site structure
37
  if description:
38
  return description.get_text(strip=True)
39
  else:
40
- return "No detailed description available on Ethos site."
41
  except Exception as e:
42
- return f"Error fetching details from Ethos site: {str(e)}"
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 (assuming 'url' key exists in your JSON)
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 more description
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
- # Create a detailed description prompt combining scraped content and attributes
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 a high-end editorial style."""
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, # Limit output to 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 in descriptions
127
  )
128
 
129
  # Decode generated text
130
  description = tokenizer.decode(outputs[0], skip_special_tokens=True)
131
 
132
- # Display the result
133
- st.write("### Generated Description")
134
  st.write(description)
135
  else:
136
  st.warning("Please select a brand.")
137
 
138
- # Add some information about the app
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
- # Add a footer
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>