jegilj commited on
Commit
8be1d11
·
verified ·
1 Parent(s): da40366

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import from_pretrained_fastai
2
+ import gradio as gr
3
+ from fastai.vision.all import *
4
+ from PIL import Image
5
+ import numpy as np
6
+ import librosa, librosa.display
7
+
8
+ # repo_id = "YOUR_USERNAME/YOUR_LEARNER_NAME"
9
+ repo_id = "jegilj/intel-image-classification"
10
+
11
+ learner = from_pretrained_fastai(repo_id)
12
+ labels = learner.dls.vocab
13
+
14
+
15
+
16
+ def crear_espectrograma(ruta_wav):
17
+ sonido_espectrograma, _ = librosa.load(os.path.join(BASE_FOLDER, ruta_wav))
18
+ matriz_con_float = librosa.amplitude_to_db(librosa.stft(sonido_espectrograma)) + 100 # sumo 100 para que los valores sean positivos, antes estaban entre -50 y 40 aprox
19
+ matriz_con_int = matriz_con_float.astype(np.uint8)
20
+ imagen = Image.fromarray(matriz_con_int)
21
+ return imagen
22
+
23
+
24
+
25
+ # Funcion para predecir:
26
+
27
+ def predecir(ruta_wav_predecir):
28
+ imagen = crear_espectrograma(ruta_wav_predecir)
29
+ imagen.save("imagen_pedecir.png")
30
+ pred,pred_idx,probs = learn_inf.predict('imagen_pedecir.png')
31
+ return {labels[i]: float(probs[i]) for i in range(len(labels))}
32
+
33
+ # Creamos la interfaz y la lanzamos.
34
+ gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(128, 128)), outputs=gr.outputs.Label(num_top_classes=3),examples=['00625137.jpg','2001242496.jpg']).launch(share=False)