molecularmax commited on
Commit
180415e
·
verified ·
1 Parent(s): b009e8b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import torch
4
+ import os
5
+ from PIL import Image
6
+ import io
7
+
8
+ # Check if CUDA is available (Hugging Face Spaces supports GPU acceleration)
9
+ device = 0 if torch.cuda.is_available() else -1
10
+ print(f"Using device: {'CUDA' if device == 0 else 'CPU'}")
11
+
12
+ # Initialize the image processing pipeline
13
+ # You can replace this with any Hugging Face model that processes images
14
+ model_name = "google/vit-base-patch16-224"
15
+ image_processor = pipeline("image-classification", model=model_name, device=device)
16
+
17
+ def process_image(input_image):
18
+ """
19
+ Process the uploaded image through the model and return results
20
+ """
21
+ if input_image is None:
22
+ return [{"label": "No image provided", "score": 0.0}]
23
+
24
+ # Run the image through the model
25
+ results = image_processor(input_image)
26
+
27
+ # Return top 5 predictions
28
+ return results[:5]
29
+
30
+ def save_output(results):
31
+ """
32
+ Convert results to a downloadable format
33
+ """
34
+ if not results or len(results) == 0:
35
+ return None
36
+
37
+ output_text = "Model Predictions:\n\n"
38
+ for result in results:
39
+ output_text += f"Label: {result['label']}, Score: {result['score']:.4f}\n"
40
+
41
+ # Create a file for download
42
+ with open("results.txt", "w") as f:
43
+ f.write(output_text)
44
+
45
+ return "results.txt"
46
+
47
+ # Create the Gradio interface with a more polished design
48
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
49
+ gr.Markdown("# Image Classification Demo")
50
+ gr.Markdown("Upload an image and get classification results from the model.")
51
+
52
+ with gr.Row():
53
+ with gr.Column(scale=1):
54
+ # Input components
55
+ input_image = gr.Image(type="pil", label="Upload Image")
56
+ with gr.Row():
57
+ submit_btn = gr.Button("Process Image", variant="primary")
58
+ clear_btn = gr.Button("Clear")
59
+
60
+ with gr.Column(scale=1):
61
+ # Output components
62
+ output_results = gr.JSON(label="Model Predictions")
63
+ download_btn = gr.Button("Download Results")
64
+ download_output = gr.File(label="Download Output")
65
+
66
+ # Set up the processing flow
67
+ submit_btn.click(
68
+ fn=process_image,
69
+ inputs=[input_image],
70
+ outputs=[output_results]
71
+ )
72
+
73
+ clear_btn.click(
74
+ fn=lambda: (None, None, None),
75
+ inputs=[],
76
+ outputs=[input_image, output_results, download_output]
77
+ )
78
+
79
+ download_btn.click(
80
+ fn=save_output,
81
+ inputs=[output_results],
82
+ outputs=[download_output]
83
+ )
84
+
85
+ # Add example images
86
+ gr.Examples(
87
+ examples=[
88
+ os.path.join(os.path.dirname(__file__), "examples/cat.jpg"),
89
+ os.path.join(os.path.dirname(__file__), "examples/dog.jpg"),
90
+ ],
91
+ inputs=input_image,
92
+ label="Example Images"
93
+ )
94
+
95
+ # Add information footer
96
+ gr.Markdown("""
97
+ ### About this demo
98
+ - Model: [google/vit-base-patch16-224](https://huggingface.co/google/vit-base-patch16-224)
99
+ - This demo classifies images into 1000 ImageNet categories
100
+ - Created with Gradio and Hugging Face Transformers
101
+ """)
102
+
103
+ # For Hugging Face Spaces, we use the Gradio app directly
104
+ demo.launch()