Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -90,22 +90,31 @@ view = st.sidebar.radio("Go to", ["Artwork Explorer", "Visualizations", "Search
|
|
90 |
if view == "Artwork Explorer":
|
91 |
def generate_description(art):
|
92 |
prompt = (
|
93 |
-
f"Title: {art['title']}
|
94 |
-
|
95 |
-
f"
|
96 |
-
|
|
|
|
|
|
|
|
|
97 |
f"Description:"
|
98 |
)
|
|
|
99 |
try:
|
100 |
if model_type == "gpt":
|
101 |
result = describer(prompt, max_length=100, truncation=True, do_sample=True)[0]["generated_text"]
|
102 |
return result.strip()
|
103 |
else:
|
104 |
-
|
105 |
-
|
|
|
|
|
106 |
return result.strip()
|
107 |
except Exception as e:
|
108 |
return f"[Description unavailable: {e}]"
|
|
|
|
|
109 |
query_idx = st.slider("Select a Painting Index to Explore", 0, len(image_titles) - 1)
|
110 |
query_art = artworks[query_idx]
|
111 |
st.image(query_art["image"], caption=f"🎨 Query: {query_art['title']} ({query_art['artist']})", use_container_width=True)
|
@@ -119,6 +128,34 @@ if view == "Artwork Explorer":
|
|
119 |
with st.expander("🧠 AI-generated Description"):
|
120 |
st.markdown(f"> {generate_description(query_art)}")
|
121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
sim_scores = cosine_similarity([features[query_idx]], features)[0]
|
123 |
ranked = np.argsort(sim_scores)[::-1][1:6]
|
124 |
|
|
|
90 |
if view == "Artwork Explorer":
|
91 |
def generate_description(art):
|
92 |
prompt = (
|
93 |
+
f"Title: {art['title']}
|
94 |
+
"
|
95 |
+
f"Artist: {art['artist']}
|
96 |
+
"
|
97 |
+
f"Date: {art['date']}
|
98 |
+
"
|
99 |
+
f"Culture: {art['culture']}
|
100 |
+
"
|
101 |
f"Description:"
|
102 |
)
|
103 |
+
)
|
104 |
try:
|
105 |
if model_type == "gpt":
|
106 |
result = describer(prompt, max_length=100, truncation=True, do_sample=True)[0]["generated_text"]
|
107 |
return result.strip()
|
108 |
else:
|
109 |
+
prompt = f"Write a short description of this artwork:
|
110 |
+
|
111 |
+
{prompt}"
|
112 |
+
result = describer(prompt, max_length=100, truncation=True)[0]["generated_text"]
|
113 |
return result.strip()
|
114 |
except Exception as e:
|
115 |
return f"[Description unavailable: {e}]"
|
116 |
+
except Exception as e:
|
117 |
+
return f"[Description unavailable: {e}]"
|
118 |
query_idx = st.slider("Select a Painting Index to Explore", 0, len(image_titles) - 1)
|
119 |
query_art = artworks[query_idx]
|
120 |
st.image(query_art["image"], caption=f"🎨 Query: {query_art['title']} ({query_art['artist']})", use_container_width=True)
|
|
|
128 |
with st.expander("🧠 AI-generated Description"):
|
129 |
st.markdown(f"> {generate_description(query_art)}")
|
130 |
|
131 |
+
with st.expander("💬 Ask a question about this artwork"):
|
132 |
+
user_question = st.text_input("Your question")
|
133 |
+
if user_question:
|
134 |
+
qa_prompt = (
|
135 |
+
f"Context:
|
136 |
+
"
|
137 |
+
f"Title: {query_art['title']}
|
138 |
+
"
|
139 |
+
f"Artist: {query_art['artist']}
|
140 |
+
"
|
141 |
+
f"Date: {query_art['date']}
|
142 |
+
"
|
143 |
+
f"Culture: {query_art['culture']}
|
144 |
+
"
|
145 |
+
)
|
146 |
+
if query_art.get("tags"):
|
147 |
+
tags = ", ".join(tag['term'] for tag in query_art['tags'])
|
148 |
+
qa_prompt += f"Tags: {tags}
|
149 |
+
"
|
150 |
+
qa_prompt += f"
|
151 |
+
Question: {user_question}
|
152 |
+
Answer:"
|
153 |
+
try:
|
154 |
+
answer = describer(qa_prompt, max_length=100, truncation=True, do_sample=True)[0]["generated_text"]
|
155 |
+
st.markdown(f"**Answer:** {answer.strip()}")
|
156 |
+
except Exception as e:
|
157 |
+
st.error(f"[Answer unavailable: {e}]")
|
158 |
+
|
159 |
sim_scores = cosine_similarity([features[query_idx]], features)[0]
|
160 |
ranked = np.argsort(sim_scores)[::-1][1:6]
|
161 |
|