seawolf2357 commited on
Commit
23708c8
ยท
verified ยท
1 Parent(s): 87c119f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -21
app.py CHANGED
@@ -1,18 +1,14 @@
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):
@@ -20,20 +16,18 @@ def classify_image(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()
 
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
  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
  # ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
33
  iface.launch()