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()