Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
|
| 4 |
+
# Directory containing local images
|
| 5 |
+
IMAGE_DIR = "images/"
|
| 6 |
+
|
| 7 |
+
def fetch_animal_image(animal):
|
| 8 |
+
animal_images = {
|
| 9 |
+
"cat": "cat.jpg",
|
| 10 |
+
"dog": "dog.jpg",
|
| 11 |
+
"bird": "bird.jpg",
|
| 12 |
+
"horse": "horse.jpg",
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
# Fetch image file name based on user input
|
| 16 |
+
image_file = animal_images.get(animal.lower(), "default.jpg") # Fallback image
|
| 17 |
+
|
| 18 |
+
# Open and return the image
|
| 19 |
+
img_path = IMAGE_DIR + image_file
|
| 20 |
+
img = Image.open(img_path)
|
| 21 |
+
|
| 22 |
+
return img
|
| 23 |
+
|
| 24 |
+
# Define the Gradio Interface
|
| 25 |
+
demo = gr.Interface(
|
| 26 |
+
fn=fetch_animal_image,
|
| 27 |
+
inputs=gr.inputs.Textbox(label="What's your favorite animal?"),
|
| 28 |
+
outputs=gr.outputs.Image(type="pil"),
|
| 29 |
+
title="Favorite Animal Image",
|
| 30 |
+
description="Enter the name of your favorite animal and see an image of it."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Launch the interface
|
| 34 |
+
demo.launch()
|