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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py CHANGED
@@ -1,3 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # ... (previous code remains the same)
2
 
3
  # Combine Ethos description and attributes into a prompt
 
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
  # ... (previous code remains the same)
98
 
99
  # Combine Ethos description and attributes into a prompt