seawolf2357 commited on
Commit
0f88190
ยท
verified ยท
1 Parent(s): 791d393

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- import soundfile as sf
4
  import io
5
 
6
  # ์ด๋ฏธ์ง€ ์ธ์‹ ํŒŒ์ดํ”„๋ผ์ธ ๋กœ๋“œ
@@ -8,8 +7,8 @@ model = pipeline("image-classification", model="google/vit-base-patch16-224")
8
 
9
  # ์นดํ…Œ๊ณ ๋ฆฌ์— ๋”ฐ๋ฅธ ์‚ฌ์šด๋“œ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ •์˜
10
  sound_files = {
11
- "dog": "path/to/dog_bark.wav",
12
- "cat": "path/to/cat_meow.wav",
13
  # ... ๊ฐ ์นดํ…Œ๊ณ ๋ฆฌ์— ๋Œ€ํ•œ ์‚ฌ์šด๋“œ ํŒŒ์ผ ๊ฒฝ๋กœ ์ถ”๊ฐ€
14
  }
15
 
@@ -18,11 +17,11 @@ def classify_image(uploaded_image):
18
  # ๊ฐ€์žฅ ํ™•๋ฅ ์ด ๋†’์€ ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ด
19
  top_prediction = predictions[0]['label']
20
 
21
- # ์˜ˆ์ธก ๊ฒฐ๊ณผ์— ํ•ด๋‹นํ•˜๋Š” ์‚ฌ์šด๋“œ ํŒŒ์ผ์„ ๋กœ๋“œ
22
- sound_path = sound_files.get(top_prediction, None)
23
- if sound_path is not None:
24
- with open(sound_path, 'rb') as file:
25
- audio_data = file.read()
26
  return top_prediction, audio_data
27
  else:
28
  # ํ•ด๋‹นํ•˜๋Š” ์‚ฌ์šด๋“œ ํŒŒ์ผ์ด ์—†๋Š” ๊ฒฝ์šฐ ๋นˆ ์˜ค๋””์˜ค ๋ฐ์ดํ„ฐ ๋ฐ˜ํ™˜
@@ -32,7 +31,7 @@ def classify_image(uploaded_image):
32
  iface = gr.Interface(
33
  fn=classify_image,
34
  inputs=gr.Image(type="pil"),
35
- outputs=[gr.Label(), gr.Audio(format="wav")],
36
  title="์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ฐ ์‚ฌ์šด๋“œ ์žฌ์ƒ",
37
  description="์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜๋ฉด, ์‚ฌ๋ฌผ์„ ์ธ์‹ํ•˜๊ณ  ํ•ด๋‹นํ•˜๋Š” ์‚ฌ์šด๋“œ๋ฅผ ์žฌ์ƒํ•ฉ๋‹ˆ๋‹ค."
38
  )
 
1
  import gradio as gr
2
  from transformers import pipeline
 
3
  import io
4
 
5
  # ์ด๋ฏธ์ง€ ์ธ์‹ ํŒŒ์ดํ”„๋ผ์ธ ๋กœ๋“œ
 
7
 
8
  # ์นดํ…Œ๊ณ ๋ฆฌ์— ๋”ฐ๋ฅธ ์‚ฌ์šด๋“œ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ •์˜
9
  sound_files = {
10
+ "dog": "/path/to/dog_bark.mp3",
11
+ "cat": "/path/to/cat_meow.mp3",
12
  # ... ๊ฐ ์นดํ…Œ๊ณ ๋ฆฌ์— ๋Œ€ํ•œ ์‚ฌ์šด๋“œ ํŒŒ์ผ ๊ฒฝ๋กœ ์ถ”๊ฐ€
13
  }
14
 
 
17
  # ๊ฐ€์žฅ ํ™•๋ฅ ์ด ๋†’์€ ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ด
18
  top_prediction = predictions[0]['label']
19
 
20
+ # ์˜ˆ์ธก ๊ฒฐ๊ณผ์— ํ•ด๋‹นํ•˜๋Š” ์‚ฌ์šด๋“œ ํŒŒ์ผ์˜ ๋ฐ”์ดํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜
21
+ sound_path = sound_files.get(top_prediction)
22
+ if sound_path:
23
+ with open(sound_path, "rb") as audio_file:
24
+ audio_data = audio_file.read()
25
  return top_prediction, audio_data
26
  else:
27
  # ํ•ด๋‹นํ•˜๋Š” ์‚ฌ์šด๋“œ ํŒŒ์ผ์ด ์—†๋Š” ๊ฒฝ์šฐ ๋นˆ ์˜ค๋””์˜ค ๋ฐ์ดํ„ฐ ๋ฐ˜ํ™˜
 
31
  iface = gr.Interface(
32
  fn=classify_image,
33
  inputs=gr.Image(type="pil"),
34
+ outputs=[gr.Label(), gr.Audio(format="mp3")],
35
  title="์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ฐ ์‚ฌ์šด๋“œ ์žฌ์ƒ",
36
  description="์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜๋ฉด, ์‚ฌ๋ฌผ์„ ์ธ์‹ํ•˜๊ณ  ํ•ด๋‹นํ•˜๋Š” ์‚ฌ์šด๋“œ๋ฅผ ์žฌ์ƒํ•ฉ๋‹ˆ๋‹ค."
37
  )