Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
|
|
3 |
|
4 |
# Load model and tokenizer
|
5 |
@st.cache_resource
|
@@ -10,47 +11,64 @@ def load_model():
|
|
10 |
|
11 |
model, tokenizer = load_model()
|
12 |
|
13 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
st.title("Watch Description Generator")
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import json
|
3 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
4 |
|
5 |
# Load model and tokenizer
|
6 |
@st.cache_resource
|
|
|
11 |
|
12 |
model, tokenizer = load_model()
|
13 |
|
14 |
+
# Load the JSON file
|
15 |
+
@st.cache_data
|
16 |
+
def load_json():
|
17 |
+
with open('watches_data.json') as f: # Your JSON file name
|
18 |
+
data = json.load(f)
|
19 |
+
return data
|
20 |
+
|
21 |
+
data = load_json()
|
22 |
+
|
23 |
+
# Extract unique brands
|
24 |
+
brands = sorted(list(set([item["brand"] for item in data])))
|
25 |
+
|
26 |
+
# Streamlit UI
|
27 |
st.title("Watch Description Generator")
|
28 |
|
29 |
+
# Select brand
|
30 |
+
selected_brand = st.selectbox("Select a Brand", ["Select"] + brands)
|
31 |
+
|
32 |
+
# Filter watches and SKUs by the selected brand
|
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]
|
36 |
+
|
37 |
+
selected_watch = st.selectbox("Select Watch Name (Optional)", ["Select"] + watches)
|
38 |
+
selected_sku = st.selectbox("Select SKU (Optional)", ["Select"] + skus)
|
39 |
+
|
40 |
+
# Get the selected watch data from the JSON
|
41 |
+
watch_data = None
|
42 |
+
if selected_watch != "Select":
|
43 |
+
watch_data = next((item for item in data if item["name"] == selected_watch), None)
|
44 |
+
elif selected_sku != "Select":
|
45 |
+
watch_data = next((item for item in data if item["sku"] == selected_sku), None)
|
46 |
+
|
47 |
+
if watch_data:
|
48 |
+
# Generate description based on attributes
|
49 |
+
if st.button("Generate Description"):
|
50 |
+
attributes = {
|
51 |
+
"brand": watch_data["brand"],
|
52 |
+
"name": watch_data.get("name", "Unknown Watch"),
|
53 |
+
"sku": watch_data.get("sku", "Unknown SKU"),
|
54 |
+
"features": watch_data.get("features", "Unknown Features"),
|
55 |
+
"casesize": watch_data.get("casesize", "Unknown Case Size"),
|
56 |
+
"movement": watch_data.get("movement", "Unknown Movement"),
|
57 |
+
"gender": watch_data.get("gender", "Unknown Gender"),
|
58 |
+
# Add more attributes as needed
|
59 |
+
}
|
60 |
+
|
61 |
+
input_text = f"Brand: {attributes['brand']}, Watch Name: {attributes['name']}, SKU: {attributes['sku']}, Features: {attributes['features']}, Case Size: {attributes['casesize']}, Movement: {attributes['movement']}, Gender: {attributes['gender']}"
|
62 |
+
|
63 |
+
# Tokenize input and generate description
|
64 |
+
inputs = tokenizer(input_text, return_tensors="pt")
|
65 |
+
outputs = model.generate(**inputs)
|
66 |
+
|
67 |
+
# Decode generated text
|
68 |
+
description = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
69 |
+
|
70 |
+
# Display the result
|
71 |
+
st.write("### Generated Description")
|
72 |
+
st.write(description)
|
73 |
+
else:
|
74 |
+
st.warning("Please select a brand.")
|