Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,68 @@
|
|
1 |
import gradio as gr
|
2 |
from datasets import load_dataset
|
|
|
3 |
|
4 |
-
# Load the WikiArt dataset
|
5 |
-
dataset = load_dataset("huggan/wikiart")
|
6 |
|
7 |
# Function to display artwork details
|
8 |
def display_artwork(index):
|
9 |
-
record
|
10 |
-
|
|
|
11 |
|
12 |
-
#
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
fn=display_artwork,
|
15 |
inputs=gr.Number(label="Artwork Index"),
|
16 |
outputs=[gr.Image(label="Artwork"), gr.Text(label="Details")],
|
17 |
-
title="
|
18 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from datasets import load_dataset
|
3 |
+
from transformers import pipeline
|
4 |
|
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 |
+
for i, record in enumerate(dataset["train"]):
|
11 |
+
if i == index:
|
12 |
+
return record["image"], f"Welcome to the Gallery\n\nTitle: {record['title']}\nArtist: {record['artist']}\nStyle: {record['style']}\nGenre: {record['genre']}\n\nStep into the world of art and explore its details."
|
13 |
|
14 |
+
# Function to filter artworks based on metadata
|
15 |
+
def filter_artworks(artist=None, genre=None, style=None):
|
16 |
+
results = []
|
17 |
+
for record in dataset["train"]:
|
18 |
+
if (artist is None or record["artist"] == artist) and \
|
19 |
+
(genre is None or record["genre"] == genre) and \
|
20 |
+
(style is None or record["style"] == style):
|
21 |
+
results.append(record)
|
22 |
+
return results
|
23 |
+
|
24 |
+
# Function to display filtered artworks
|
25 |
+
def display_filtered_artworks(artist, genre, style):
|
26 |
+
filtered_results = filter_artworks(artist, genre, style)
|
27 |
+
return [(r["image"], f"Title: {r['title']}\nArtist: {r['artist']}\nStyle: {r['style']}\nGenre: {r['genre']}") for r in filtered_results]
|
28 |
+
|
29 |
+
# Chatbot functionality for museum guide persona
|
30 |
+
chatbot = pipeline("text-generation", model="gpt-4")
|
31 |
+
|
32 |
+
def museum_guide_query(prompt):
|
33 |
+
return chatbot(prompt, max_length=100, num_return_sequences=1)[0]["generated_text"]
|
34 |
+
|
35 |
+
# Gradio interfaces
|
36 |
+
artwork_interface = gr.Interface(
|
37 |
fn=display_artwork,
|
38 |
inputs=gr.Number(label="Artwork Index"),
|
39 |
outputs=[gr.Image(label="Artwork"), gr.Text(label="Details")],
|
40 |
+
title="Exhibit AI - Virtual Art Gallery"
|
41 |
+
)
|
42 |
+
|
43 |
+
filter_interface = gr.Interface(
|
44 |
+
fn=display_filtered_artworks,
|
45 |
+
inputs=[gr.Text(label="Artist"), gr.Text(label="Genre"), gr.Text(label="Style")],
|
46 |
+
outputs=gr.Gallery(label="Filtered Artworks", caption="Explore artworks based on your preferences."),
|
47 |
+
title="Filter Artworks"
|
48 |
+
)
|
49 |
+
|
50 |
+
chatbot_interface = gr.Interface(
|
51 |
+
fn=museum_guide_query,
|
52 |
+
inputs=gr.Textbox(label="Ask the Museum Guide"),
|
53 |
+
outputs=gr.Text(label="Guide Response"),
|
54 |
+
title="Museum Guide Chatbot"
|
55 |
+
)
|
56 |
+
|
57 |
+
# Launch Gradio Blocks to combine all interfaces
|
58 |
+
def launch_app():
|
59 |
+
with gr.Blocks() as demo:
|
60 |
+
gr.Markdown("# Exhibit AI - Virtual Art Gallery")
|
61 |
+
gr.TabbedInterface(
|
62 |
+
[artwork_interface, filter_interface, chatbot_interface],
|
63 |
+
["View Artwork", "Filter Artworks", "Ask the Museum Guide"]
|
64 |
+
)
|
65 |
+
demo.launch()
|
66 |
+
|
67 |
+
if __name__ == "__main__":
|
68 |
+
launch_app()
|