Spaces:
Sleeping
Sleeping
Genis
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import zipfile
|
3 |
+
from glob import glob
|
4 |
+
|
5 |
+
from PIL import Image
|
6 |
+
from collections import Counter
|
7 |
+
|
8 |
+
from huggingface_hub import hf_hub_download, login
|
9 |
+
login(token=os.getenv('LOGIN_TOKEN'))
|
10 |
+
hf_hub_download(repo_id="giniwini/model_creator", filename="ModelCreator.py", )
|
11 |
+
|
12 |
+
from ModelCreator import Model
|
13 |
+
|
14 |
+
m = Model()
|
15 |
+
|
16 |
+
markdown_head = """# <center>🚀👁️ Create your own Classifier 👁️🚀</center>
|
17 |
+
### By Elok Quence 🦾
|
18 |
+
|
19 |
+
This space is intended to give high level tools so everyone can make his own image classification model and use it for any purpose.
|
20 |
+
|
21 |
+
## How to create it and test it
|
22 |
+
|
23 |
+
1. Put some images in a folder classified by subfolders indicating the classes. Like: \n
|
24 |
+
- img_folder/cat/image_of_cat_0.png
|
25 |
+
- img_folder/dog/image_of_dog_4.jpeg \n
|
26 |
+
I recomment around 5 images per class. \n
|
27 |
+
2. Right click in the folder and press "compress..." in ".zip" mode. This will create you a zip file. \n
|
28 |
+
3. Upload the file on the space. \n
|
29 |
+
4. Once uploaded, try with some images to test that everything is going well and have fun.
|
30 |
+
|
31 |
+
---
|
32 |
+
## ⬇️⬇️⬇️ Do you want to export the model or thank my effort? 🫀 Look at the bottom to see the options. ⬇️⬇️⬇️
|
33 |
+
---
|
34 |
+
"""
|
35 |
+
|
36 |
+
markdown_tail = """
|
37 |
+
---
|
38 |
+
|
39 |
+
**Are you grateful?**
|
40 |
+
Consider buying me a coffee subscribing to my [patreon](https://patreon.com/elokquentia?utm_medium=unknown&utm_source=join_link&utm_campaign=creatorshare_creator&utm_content=copyLink) coffee tier \n
|
41 |
+
or make a donation trough [paypal](https://www.paypal.com/donate/?hosted_button_id=QD2W2G34GWQ4J)
|
42 |
+
---
|
43 |
+
|
44 |
+
## Export the model
|
45 |
+
Subscribe to patreon Brunch tier to get the password. [patreon](https://patreon.com/elokquentia?utm_medium=unknown&utm_source=join_link&utm_campaign=creatorshare_creator&utm_content=copyLink)
|
46 |
+
|
47 |
+
---
|
48 |
+
## Have you already exported the model? Here's how to use it
|
49 |
+
|
50 |
+
### 1. Install the requirements:
|
51 |
+
Open the terminal and go to the directory where you placed the requirements.txt file. \n
|
52 |
+
Now with this command you can install the dependencies at once.
|
53 |
+
```bash
|
54 |
+
pip install -r requirements.txt
|
55 |
+
```
|
56 |
+
Has anything gone wrong? Look inside the requirements.txt and install the dependencies one by one.
|
57 |
+
|
58 |
+
### 2. Use ModelCreator's Model class
|
59 |
+
Place the ModelCreator.py file on the directory you want to code.
|
60 |
+
- some_folder/ModelCreator.py
|
61 |
+
- your_python_script.py
|
62 |
+
|
63 |
+
### 3. Use your model
|
64 |
+
In your_python_script.py, try something like:
|
65 |
+
```python
|
66 |
+
from ModelCreator import Model
|
67 |
+
|
68 |
+
model_path = 'model.pickle'
|
69 |
+
m2 = Model().load_model(model_path)
|
70 |
+
|
71 |
+
image_path = 'some_image_path/image_1.png'
|
72 |
+
result = m2(image_path)
|
73 |
+
print(result)
|
74 |
+
```
|
75 |
+
### 4. DO YOU HAVE ANY DOUBTS ON HOW THIS WORKS OR PROBLEMS USING THE MODEL?
|
76 |
+
Contact me on huggingface or via e-mail [email protected]
|
77 |
+
"""
|
78 |
+
|
79 |
+
|
80 |
+
def fit_model(zip_file_path):
|
81 |
+
path = 'tmp/'
|
82 |
+
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
|
83 |
+
zip_ref.extractall(path)
|
84 |
+
|
85 |
+
images_path = glob(f'{path}**/*/*.*')
|
86 |
+
labels_train = [i.split('/')[-2] for i in images_path]
|
87 |
+
print(labels_train)
|
88 |
+
images_train = [Image.open(i) for i in images_path]
|
89 |
+
|
90 |
+
m.train(images_train, labels_train)
|
91 |
+
return (f"Model Fitted \n"
|
92 |
+
f"Classes: {dict(Counter(labels_train))}")
|
93 |
+
|
94 |
+
|
95 |
+
def predict(image):
|
96 |
+
l = m.predict(image)
|
97 |
+
return f'Predicted Class: {l}'
|
98 |
+
|
99 |
+
|
100 |
+
def export(password):
|
101 |
+
if password == os.getenv('EXPORT_PASSWORD'):
|
102 |
+
m.export_model()
|
103 |
+
return [[gr.update(visible=True), file] for file in [f'model.pickle', f'requirements.txt', f'ModelCreator.py']]
|
104 |
+
else:
|
105 |
+
return [None, f'Subscribe to Patreon to Download']*3
|
106 |
+
|
107 |
+
with gr.Blocks() as demo:
|
108 |
+
gr.Markdown(markdown_head)
|
109 |
+
|
110 |
+
with gr.Row() as g1:
|
111 |
+
inp = gr.File(label="zip file")
|
112 |
+
out = gr.Textbox()
|
113 |
+
btn = gr.Button("Submit Zip File")
|
114 |
+
btn.click(fn=fit_model, inputs=inp, outputs=out)
|
115 |
+
|
116 |
+
with gr.Row() as g2:
|
117 |
+
inp2 = gr.Image(label="Input Image", type='pil')
|
118 |
+
out2 = gr.Textbox()
|
119 |
+
btn2 = gr.Button("Predict/Test on an Image")
|
120 |
+
btn2.click(fn=predict, inputs=inp2, outputs=out2)
|
121 |
+
|
122 |
+
with gr.Row() as g3:
|
123 |
+
inp3 = gr.Textbox()
|
124 |
+
out3 = gr.File(label='Download link', visible=True, height=30, interactive=False)
|
125 |
+
out4 = gr.File(label='Download link', visible=True, height=30, interactive=False)
|
126 |
+
out5 = gr.File(label='Download link', visible=True, height=30, interactive=False)
|
127 |
+
btn3 = gr.Button("Export Fitted Model")
|
128 |
+
btn3.click(fn=export, inputs=inp3, outputs=[[out3, out3],[out4, out4],[out5, out5]])
|
129 |
+
gr.Markdown(markdown_tail)
|
130 |
+
demo.launch()
|