Spaces:
Sleeping
Sleeping
bsvaz commited on
Commit ·
82c9f6d
1
Parent(s): 0dfac78
deploy
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitignore +2 -0
- .gradio/flagged/dataset1.csv +0 -2
- .gradio/flagged/image/b71cfb0c013a7fad3f6a/header_chiffre_0.jpg +0 -0
- README.md +36 -12
- __pycache__/classifier.cpython-312.pyc +0 -0
- __pycache__/custom_theme.cpython-312.pyc +0 -0
- app.py +18 -14
- classifier.py +8 -0
- custom_theme.py +15 -13
- examples/atomium.jpg +0 -0
- examples/badlands_national_park.jpg +0 -0
- examples/banff_national_park.jpg +0 -0
- examples/brooklyn_bridge.jpg +0 -0
- examples/central_park.jpg +0 -0
- examples/changdeokgung.jpg +0 -0
- examples/dead_sea.jpg +0 -0
- examples/death_valley_national_park.jpg +0 -0
- examples/delicate_arch.jpg +0 -0
- examples/edinburgh_castle.jpg +0 -0
- examples/eiffel_tower.jpg +0 -0
- examples/externsteine.jpg +0 -0
- examples/forth_bridge.jpg +0 -0
- examples/gateway_of_india.jpg +0 -0
- examples/golden_gate_bridge.jpg +0 -0
- examples/grand_canyon.jpg +0 -0
- examples/great_barrier_reef.jpg +0 -0
- examples/great_wall_of_china.jpg +0 -0
- examples/gullfoss_falls.jpg +0 -0
- examples/haleakala_national_park.jpg +0 -0
- examples/hanging_temple.jpg +0 -0
- examples/kantanagar_temple.jpg +0 -0
- examples/ljubljana_castle.jpg +0 -0
- examples/london_olympic_stadium.jpg +0 -0
- examples/machu_picchu.jpg +0 -0
- examples/matterhorn.jpg +0 -0
- examples/monumento_a_la_revolucion.jpg +0 -0
- examples/moscow_raceway.jpg +0 -0
- examples/mount_rainier_national_park.jpg +0 -0
- examples/mount_rushmore_national_memorial.jpg +0 -0
- examples/niagara_falls.jpg +0 -0
- examples/petronas_towers.jpg +0 -0
- examples/pont_du_gard.jpg +0 -0
- examples/prague_astronomical_clock.jpg +0 -0
- examples/seattle_japanese_garden.jpg +0 -0
- examples/soreq_cave.jpg +0 -0
- examples/stockholm_city_hall.jpg +0 -0
- examples/stonehenge.jpg +0 -0
- examples/sydney_harbour_bridge.jpg +0 -0
- examples/sydney_opera_house.jpg +0 -0
- examples/taj_mahal.jpg +0 -0
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__
|
| 2 |
+
.venv
|
.gradio/flagged/dataset1.csv
DELETED
|
@@ -1,2 +0,0 @@
|
|
| 1 |
-
image,output,timestamp
|
| 2 |
-
.gradio/flagged/image/b71cfb0c013a7fad3f6a/header_chiffre_0.jpg,"{""label"": ""Sydney Harbour Bridge"", ""confidences"": [{""label"": ""Sydney Harbour Bridge"", ""confidence"": 0.997890055179596}, {""label"": ""Eiffel Tower"", ""confidence"": 0.9969549179077148}, {""label"": ""Forth Bridge"", ""confidence"": 0.905195415019989}, {""label"": ""Brooklyn Bridge"", ""confidence"": 0.9011015892028809}, {""label"": ""Monumento a la Revolucion"", ""confidence"": 0.8767676949501038}]}",2025-01-23 20:53:44.732426
|
|
|
|
|
|
|
|
|
.gradio/flagged/image/b71cfb0c013a7fad3f6a/header_chiffre_0.jpg
DELETED
|
Binary file (60.5 kB)
|
|
|
README.md
CHANGED
|
@@ -1,12 +1,36 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Landmark Classification with ViT
|
| 2 |
+
|
| 3 |
+
This repository contains a Gradio-based application for classifying famous landmarks using a ViT (Vision Transformer) model. It includes code for loading landmark categories, managing examples, customizing the Gradio interface, and using a pre-trained model from the Hugging Face Hub.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
- ViT-based landmark classification
|
| 7 |
+
- Organized categories with ordered data loading
|
| 8 |
+
- Custom theme using Gradio’s theming framework
|
| 9 |
+
- Example images section for quick testing
|
| 10 |
+
|
| 11 |
+
## Installation
|
| 12 |
+
1. Clone the repository.
|
| 13 |
+
2. Create and activate a virtual environment:
|
| 14 |
+
```
|
| 15 |
+
python -m venv .venv
|
| 16 |
+
source .venv/bin/activate
|
| 17 |
+
```
|
| 18 |
+
3. Install dependencies:
|
| 19 |
+
```
|
| 20 |
+
pip install -r requirements.txt
|
| 21 |
+
```
|
| 22 |
+
|
| 23 |
+
## Usage
|
| 24 |
+
1. Run the main application:
|
| 25 |
+
```
|
| 26 |
+
python app.py
|
| 27 |
+
```
|
| 28 |
+
2. Access the Gradio interface in a web browser at the URL printed in the console.
|
| 29 |
+
|
| 30 |
+
## Project Structure
|
| 31 |
+
- `app.py` : Application entry point
|
| 32 |
+
- `classifier.py` : Model loading and inference logic
|
| 33 |
+
- `custom_theme.py` : Custom theme definitions for Gradio
|
| 34 |
+
- `src/ui/interface.py` : UI layout and Gradio components
|
| 35 |
+
- `src/utils/examples.py` : Example data loading and management
|
| 36 |
+
- `landmark_categories.json` : Configuration of landmark categories
|
__pycache__/classifier.cpython-312.pyc
CHANGED
|
Binary files a/__pycache__/classifier.cpython-312.pyc and b/__pycache__/classifier.cpython-312.pyc differ
|
|
|
__pycache__/custom_theme.cpython-312.pyc
CHANGED
|
Binary files a/__pycache__/custom_theme.cpython-312.pyc and b/__pycache__/custom_theme.cpython-312.pyc differ
|
|
|
app.py
CHANGED
|
@@ -1,18 +1,22 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from classifier import LandmarkClassifier
|
| 3 |
from custom_theme import custom_theme
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
description="Upload an image to identify famous landmarks.",
|
| 15 |
-
theme=custom_theme # Apply the custom theme
|
| 16 |
-
)
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Main application entry point for the Landmark Classification system.
|
| 3 |
+
Initializes components and launches the Gradio interface.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
from classifier import LandmarkClassifier
|
| 7 |
from custom_theme import custom_theme
|
| 8 |
+
from src.utils.examples import load_categories, create_example_data
|
| 9 |
+
from src.ui.interface import create_interface
|
| 10 |
|
| 11 |
+
def main():
|
| 12 |
+
# Initialize components
|
| 13 |
+
classifier = LandmarkClassifier()
|
| 14 |
+
categories = load_categories()
|
| 15 |
+
category_examples = create_example_data(categories)
|
| 16 |
+
|
| 17 |
+
# Create and launch interface
|
| 18 |
+
iface = create_interface(classifier, category_examples, custom_theme)
|
| 19 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
if __name__ == "__main__":
|
| 22 |
+
main()
|
classifier.py
CHANGED
|
@@ -3,13 +3,21 @@ import torch
|
|
| 3 |
|
| 4 |
class LandmarkClassifier:
|
| 5 |
def __init__(self, model_name="bsvaz/landmark-classification-vit"):
|
|
|
|
| 6 |
self.model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 7 |
self.processor = AutoImageProcessor.from_pretrained(model_name)
|
| 8 |
|
| 9 |
def classify_image(self, image):
|
|
|
|
| 10 |
inputs = self.processor(image, return_tensors="pt")
|
|
|
|
|
|
|
| 11 |
with torch.no_grad():
|
| 12 |
outputs = self.model(**inputs)
|
|
|
|
|
|
|
| 13 |
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 14 |
scores = probabilities[0].tolist()
|
|
|
|
|
|
|
| 15 |
return {self.model.config.id2label[i]: score for i, score in enumerate(scores)}
|
|
|
|
| 3 |
|
| 4 |
class LandmarkClassifier:
|
| 5 |
def __init__(self, model_name="bsvaz/landmark-classification-vit"):
|
| 6 |
+
# Load pre-trained model and processor from HuggingFace hub
|
| 7 |
self.model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 8 |
self.processor = AutoImageProcessor.from_pretrained(model_name)
|
| 9 |
|
| 10 |
def classify_image(self, image):
|
| 11 |
+
# Preprocess image using the model's required format
|
| 12 |
inputs = self.processor(image, return_tensors="pt")
|
| 13 |
+
|
| 14 |
+
# Perform inference without gradient calculation for efficiency
|
| 15 |
with torch.no_grad():
|
| 16 |
outputs = self.model(**inputs)
|
| 17 |
+
|
| 18 |
+
# Convert logits to probabilities using softmax
|
| 19 |
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 20 |
scores = probabilities[0].tolist()
|
| 21 |
+
|
| 22 |
+
# Map class indices to label names and their corresponding probabilities
|
| 23 |
return {self.model.config.id2label[i]: score for i, score in enumerate(scores)}
|
custom_theme.py
CHANGED
|
@@ -1,18 +1,20 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
#
|
| 4 |
custom_theme = gr.themes.Default(
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
).set(
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
# Create custom theme with purple and yellow color scheme for visual appeal and accessibility
|
| 4 |
custom_theme = gr.themes.Default(
|
| 5 |
+
# Set main color scheme
|
| 6 |
+
primary_hue="purple", # Primary brand color
|
| 7 |
+
secondary_hue="yellow", # Accent color for contrast
|
| 8 |
+
neutral_hue="gray", # Neutral colors for text and backgrounds
|
| 9 |
).set(
|
| 10 |
+
# Customize specific UI elements
|
| 11 |
+
body_background_fill='*primary_100', # Light purple background for visual comfort
|
| 12 |
+
body_text_color='*neutral_900', # Dark text for readability
|
| 13 |
+
button_primary_background_fill='*primary_500', # Distinctive purple buttons
|
| 14 |
+
button_primary_text_color='white', # High contrast button text
|
| 15 |
+
button_primary_background_fill_hover='*primary_600', # Darker purple for hover state
|
| 16 |
+
slider_color='*primary_500', # Consistent purple for interactive elements
|
| 17 |
+
checkbox_background_color='*primary_500', # Match checkbox style with theme
|
| 18 |
+
input_background_fill='*neutral_100', # Light background for input areas
|
| 19 |
+
input_border_color='*primary_500', # Purple borders for visual hierarchy
|
| 20 |
)
|
examples/atomium.jpg
ADDED
|
examples/badlands_national_park.jpg
ADDED
|
examples/banff_national_park.jpg
ADDED
|
examples/brooklyn_bridge.jpg
ADDED
|
examples/central_park.jpg
ADDED
|
examples/changdeokgung.jpg
ADDED
|
examples/dead_sea.jpg
ADDED
|
examples/death_valley_national_park.jpg
ADDED
|
examples/delicate_arch.jpg
ADDED
|
examples/edinburgh_castle.jpg
ADDED
|
examples/eiffel_tower.jpg
ADDED
|
examples/externsteine.jpg
ADDED
|
examples/forth_bridge.jpg
ADDED
|
examples/gateway_of_india.jpg
ADDED
|
examples/golden_gate_bridge.jpg
ADDED
|
examples/grand_canyon.jpg
ADDED
|
examples/great_barrier_reef.jpg
ADDED
|
examples/great_wall_of_china.jpg
ADDED
|
examples/gullfoss_falls.jpg
ADDED
|
examples/haleakala_national_park.jpg
ADDED
|
examples/hanging_temple.jpg
ADDED
|
examples/kantanagar_temple.jpg
ADDED
|
examples/ljubljana_castle.jpg
ADDED
|
examples/london_olympic_stadium.jpg
ADDED
|
examples/machu_picchu.jpg
ADDED
|
examples/matterhorn.jpg
ADDED
|
examples/monumento_a_la_revolucion.jpg
ADDED
|
examples/moscow_raceway.jpg
ADDED
|
examples/mount_rainier_national_park.jpg
ADDED
|
examples/mount_rushmore_national_memorial.jpg
ADDED
|
examples/niagara_falls.jpg
ADDED
|
examples/petronas_towers.jpg
ADDED
|
examples/pont_du_gard.jpg
ADDED
|
examples/prague_astronomical_clock.jpg
ADDED
|
examples/seattle_japanese_garden.jpg
ADDED
|
examples/soreq_cave.jpg
ADDED
|
examples/stockholm_city_hall.jpg
ADDED
|
examples/stonehenge.jpg
ADDED
|
examples/sydney_harbour_bridge.jpg
ADDED
|
examples/sydney_opera_house.jpg
ADDED
|
examples/taj_mahal.jpg
ADDED
|