Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,12 +5,22 @@ from transformers import pipeline
|
|
| 5 |
# Load the WikiArt dataset in streaming mode
|
| 6 |
dataset = load_dataset("huggan/wikiart", streaming=True)
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# Function to display artwork details
|
| 9 |
def display_artwork(index):
|
| 10 |
try:
|
| 11 |
for i, record in enumerate(dataset["train"]): # Stream through the dataset
|
| 12 |
if i == index:
|
| 13 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
return None, "Error: Index out of range or invalid."
|
| 15 |
except Exception as e:
|
| 16 |
return None, f"Error: {str(e)}"
|
|
@@ -20,9 +30,9 @@ def filter_artworks(artist=None, genre=None, style=None):
|
|
| 20 |
results = []
|
| 21 |
try:
|
| 22 |
for record in dataset["train"]:
|
| 23 |
-
if (artist is None or record
|
| 24 |
-
(genre is None or record
|
| 25 |
-
(style is None or record
|
| 26 |
results.append(record)
|
| 27 |
except Exception as e:
|
| 28 |
return []
|
|
@@ -33,7 +43,12 @@ def display_filtered_artworks(artist, genre, style):
|
|
| 33 |
filtered_results = filter_artworks(artist, genre, style)
|
| 34 |
if len(filtered_results) == 0:
|
| 35 |
return None, "No artworks found with the specified filters."
|
| 36 |
-
return [(r
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# Chatbot functionality for museum guide persona using a publicly available Hugging Face model
|
| 39 |
chatbot = pipeline("text-generation", model="gpt2") # Replace with a valid Hugging Face model
|
|
|
|
| 5 |
# Load the WikiArt dataset in streaming mode
|
| 6 |
dataset = load_dataset("huggan/wikiart", streaming=True)
|
| 7 |
|
| 8 |
+
# Function to safely get a field value or return a default message
|
| 9 |
+
def get_field(record, field, default="Unknown"):
|
| 10 |
+
return record[field] if field in record and record[field] is not None else default
|
| 11 |
+
|
| 12 |
# Function to display artwork details
|
| 13 |
def display_artwork(index):
|
| 14 |
try:
|
| 15 |
for i, record in enumerate(dataset["train"]): # Stream through the dataset
|
| 16 |
if i == index:
|
| 17 |
+
return (
|
| 18 |
+
get_field(record, "image"),
|
| 19 |
+
f"Title: {get_field(record, 'title')}\n"
|
| 20 |
+
f"Artist: {get_field(record, 'artist')}\n"
|
| 21 |
+
f"Style: {get_field(record, 'style')}\n"
|
| 22 |
+
f"Genre: {get_field(record, 'genre')}"
|
| 23 |
+
)
|
| 24 |
return None, "Error: Index out of range or invalid."
|
| 25 |
except Exception as e:
|
| 26 |
return None, f"Error: {str(e)}"
|
|
|
|
| 30 |
results = []
|
| 31 |
try:
|
| 32 |
for record in dataset["train"]:
|
| 33 |
+
if (artist is None or get_field(record, "artist") == artist) and \
|
| 34 |
+
(genre is None or get_field(record, "genre") == genre) and \
|
| 35 |
+
(style is None or get_field(record, "style") == style):
|
| 36 |
results.append(record)
|
| 37 |
except Exception as e:
|
| 38 |
return []
|
|
|
|
| 43 |
filtered_results = filter_artworks(artist, genre, style)
|
| 44 |
if len(filtered_results) == 0:
|
| 45 |
return None, "No artworks found with the specified filters."
|
| 46 |
+
return [(get_field(r, "image"),
|
| 47 |
+
f"Title: {get_field(r, 'title')}\n"
|
| 48 |
+
f"Artist: {get_field(r, 'artist')}\n"
|
| 49 |
+
f"Style: {get_field(r, 'style')}\n"
|
| 50 |
+
f"Genre: {get_field(r, 'genre')}")
|
| 51 |
+
for r in filtered_results]
|
| 52 |
|
| 53 |
# Chatbot functionality for museum guide persona using a publicly available Hugging Face model
|
| 54 |
chatbot = pipeline("text-generation", model="gpt2") # Replace with a valid Hugging Face model
|