ExhibitAI / app.py
dalybuilds's picture
Update app.py
7c5d575 verified
raw
history blame
3.04 kB
import gradio as gr
from datasets import load_dataset
from transformers import pipeline
# Load the WikiArt dataset in streaming mode
dataset = load_dataset("huggan/wikiart", streaming=True)
# Function to display artwork details
def display_artwork(index):
try:
for i, record in enumerate(dataset["train"]): # Stream through the dataset
if i == index:
return record["image"], f"Title: {record['title']}\nArtist: {record['artist']}\nStyle: {record['style']}\nGenre: {record['genre']}"
return None, "Error: Index out of range or invalid."
except Exception as e:
return None, f"Error: {str(e)}"
# Function to filter artworks based on metadata
def filter_artworks(artist=None, genre=None, style=None):
results = []
try:
for record in dataset["train"]:
if (artist is None or record["artist"] == artist) and \
(genre is None or record["genre"] == genre) and \
(style is None or record["style"] == style):
results.append(record)
except Exception as e:
return []
return results
# Function to display filtered artworks
def display_filtered_artworks(artist, genre, style):
filtered_results = filter_artworks(artist, genre, style)
if len(filtered_results) == 0:
return None, "No artworks found with the specified filters."
return [(r["image"], f"Title: {r['title']}\nArtist: {r['artist']}\nStyle: {r['style']}\nGenre: {r['genre']}") for r in filtered_results]
# Chatbot functionality for museum guide persona using a publicly available Hugging Face model
chatbot = pipeline("text-generation", model="gpt2") # Replace with a valid Hugging Face model
def museum_guide_query(prompt):
try:
response = chatbot(prompt, max_length=100, num_return_sequences=1)
return response[0]["generated_text"]
except Exception as e:
return f"Error: {str(e)}"
# Gradio interfaces
artwork_interface = gr.Interface(
fn=display_artwork,
inputs=gr.Number(label="Artwork Index"),
outputs=[gr.Image(label="Artwork"), gr.Text(label="Details")],
title="Exhibit AI - Virtual Art Gallery"
)
filter_interface = gr.Interface(
fn=display_filtered_artworks,
inputs=[gr.Text(label="Artist"), gr.Text(label="Genre"), gr.Text(label="Style")],
outputs=gr.Gallery(label="Filtered Artworks"), # Removed the 'caption' argument
title="Filter Artworks"
)
chatbot_interface = gr.Interface(
fn=museum_guide_query,
inputs=gr.Textbox(label="Ask the Museum Guide"),
outputs=gr.Text(label="Guide Response"),
title="Museum Guide Chatbot"
)
# Launch Gradio Blocks to combine all interfaces
def launch_app():
with gr.Blocks() as demo:
gr.Markdown("# Exhibit AI - Virtual Art Gallery")
gr.TabbedInterface(
[artwork_interface, filter_interface, chatbot_interface],
["View Artwork", "Filter Artworks", "Ask the Museum Guide"]
)
demo.launch()
if __name__ == "__main__":
launch_app()