seawolf2357 commited on
Commit
791d393
ยท
verified ยท
1 Parent(s): 23708c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -10
app.py CHANGED
@@ -1,14 +1,16 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
 
3
 
4
  # ์ด๋ฏธ์ง€ ์ธ์‹ ํŒŒ์ดํ”„๋ผ์ธ ๋กœ๋“œ
5
  model = pipeline("image-classification", model="google/vit-base-patch16-224")
6
 
7
- # ์นดํ…Œ๊ณ ๋ฆฌ์— ๋”ฐ๋ฅธ ์‚ฌ์šด๋“œ ํŒŒ์ผ URL์„ ์ •์˜
8
- sound_urls = {
9
- "dog": "https://example.com/dog_bark.mp3",
10
- "cat": "https://example.com/cat_meow.mp3",
11
- # ... ๊ฐ ์นดํ…Œ๊ณ ๋ฆฌ์— ๋Œ€ํ•œ ์‚ฌ์šด๋“œ ํŒŒ์ผ URL ์ถ”๊ฐ€
12
  }
13
 
14
  def classify_image(uploaded_image):
@@ -16,17 +18,23 @@ def classify_image(uploaded_image):
16
  # ๊ฐ€์žฅ ํ™•๋ฅ ์ด ๋†’์€ ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ด
17
  top_prediction = predictions[0]['label']
18
 
19
- # ์˜ˆ์ธก ๊ฒฐ๊ณผ์— ํ•ด๋‹นํ•˜๋Š” ์‚ฌ์šด๋“œ ํŒŒ์ผ์˜ URL์„ ๋ฐ˜ํ™˜
20
- sound_url = sound_urls.get(top_prediction, None)
21
- return top_prediction, sound_url
 
 
 
 
 
 
22
 
23
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
24
  iface = gr.Interface(
25
  fn=classify_image,
26
  inputs=gr.Image(type="pil"),
27
- outputs=[gr.Label(), gr.Audio()],
28
  title="์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ฐ ์‚ฌ์šด๋“œ ์žฌ์ƒ",
29
- description="์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜๋ฉด, ์‚ฌ๋ฌผ์„ ์ธ์‹ํ•˜๊ณ  ํ•ด๋‹นํ•˜๋Š” ์‚ฌ์šด๋“œ์˜ URL์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค."
30
  )
31
 
32
  # ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import soundfile as sf
4
+ import io
5
 
6
  # ์ด๋ฏธ์ง€ ์ธ์‹ ํŒŒ์ดํ”„๋ผ์ธ ๋กœ๋“œ
7
  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
 
16
  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
+ # ํ•ด๋‹นํ•˜๋Š” ์‚ฌ์šด๋“œ ํŒŒ์ผ์ด ์—†๋Š” ๊ฒฝ์šฐ ๋นˆ ์˜ค๋””์˜ค ๋ฐ์ดํ„ฐ ๋ฐ˜ํ™˜
29
+ return top_prediction, None
30
 
31
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
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
  )
39
 
40
  # ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰