seawolf2357 commited on
Commit
87c119f
Β·
verified Β·
1 Parent(s): 13ed850

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -4
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 μΈν„°νŽ˜μ΄μŠ€ 생성, type="pil"둜 μ„€μ •ν•˜μ—¬ μžλ™μœΌλ‘œ PIL.Image 객체둜 λ³€ν™˜λ˜λ„λ‘ 함
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="이미지λ₯Ό μ—…λ‘œλ“œν•˜λ©΄, 사물을 μΈμ‹ν•˜κ³  μ΅œμƒμœ„ 3개의 λΆ„λ₯˜ κ²°κ³Όλ₯Ό 좜λ ₯ν•©λ‹ˆλ‹€.")
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()