Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,39 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# μ΄λ―Έμ§ μΈμ νμ΄νλΌμΈ λ‘λ
|
| 6 |
model = pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def classify_image(uploaded_image):
|
| 9 |
-
# μ΄μ uploaded_imageλ μλμΌλ‘ PIL.Image κ°μ²΄μ
λλ€.
|
| 10 |
predictions = model(uploaded_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
return {prediction['label']: prediction['score'] for prediction in predictions}
|
| 12 |
|
| 13 |
-
# Gradio μΈν°νμ΄μ€
|
| 14 |
iface = gr.Interface(fn=classify_image,
|
| 15 |
inputs=gr.Image(type="pil"),
|
| 16 |
outputs=gr.Label(num_top_classes=3),
|
| 17 |
-
title="μ΄λ―Έμ§
|
| 18 |
-
description="μ΄λ―Έμ§λ₯Ό μ
λ‘λνλ©΄, μ¬λ¬Όμ μΈμνκ³
|
| 19 |
|
| 20 |
# μΈν°νμ΄μ€ μ€ν
|
| 21 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
+
from pydub import AudioSegment
|
| 5 |
+
from pydub.playback import play
|
| 6 |
+
import io
|
| 7 |
|
| 8 |
# μ΄λ―Έμ§ μΈμ νμ΄νλΌμΈ λ‘λ
|
| 9 |
model = pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 10 |
|
| 11 |
+
# μΉ΄ν
κ³ λ¦¬μ λ°λ₯Έ μ¬μ΄λ νμΌ κ²½λ‘λ₯Ό μ μ
|
| 12 |
+
sound_files = {
|
| 13 |
+
"dog": "dog_bark.mp3",
|
| 14 |
+
"cat": "cat_meow.mp3",
|
| 15 |
+
# ... κ° μΉ΄ν
κ³ λ¦¬μ λν μ¬μ΄λ νμΌ κ²½λ‘ μΆκ°
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
def classify_image(uploaded_image):
|
|
|
|
| 19 |
predictions = model(uploaded_image)
|
| 20 |
+
# κ°μ₯ νλ₯ μ΄ λμ μμΈ‘ κ²°κ³Όλ₯Ό κ°μ Έμ΄
|
| 21 |
+
top_prediction = predictions[0]['label']
|
| 22 |
+
|
| 23 |
+
# μμΈ‘ κ²°κ³Όμ ν΄λΉνλ μ¬μ΄λ νμΌμ μ¬μ
|
| 24 |
+
if top_prediction in sound_files:
|
| 25 |
+
sound_path = sound_files[top_prediction]
|
| 26 |
+
sound = AudioSegment.from_file(sound_path)
|
| 27 |
+
play(sound)
|
| 28 |
+
|
| 29 |
return {prediction['label']: prediction['score'] for prediction in predictions}
|
| 30 |
|
| 31 |
+
# Gradio μΈν°νμ΄μ€ μμ±
|
| 32 |
iface = gr.Interface(fn=classify_image,
|
| 33 |
inputs=gr.Image(type="pil"),
|
| 34 |
outputs=gr.Label(num_top_classes=3),
|
| 35 |
+
title="μ΄λ―Έμ§ λΆλ₯ λ° μ¬μ΄λ μ¬μ",
|
| 36 |
+
description="μ΄λ―Έμ§λ₯Ό μ
λ‘λνλ©΄, μ¬λ¬Όμ μΈμνκ³ ν΄λΉνλ μν₯μ μ¬μν©λλ€.")
|
| 37 |
|
| 38 |
# μΈν°νμ΄μ€ μ€ν
|
| 39 |
iface.launch()
|