young476 commited on
Commit
23935d2
ยท
1 Parent(s): 481b5ad

Edit app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -21
app.py CHANGED
@@ -1,28 +1,34 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
3
- import torch
4
 
5
- MODEL_PATH = "./"
 
 
 
 
6
 
7
- tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
8
- model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
9
- pipeline = TextClassificationPipeline(model=model, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1)
 
 
 
10
 
11
- def predict(lyrics):
12
- result = pipeline(lyrics)
13
- # ์˜ˆ์˜๊ฒŒ ๊ฒฐ๊ณผ๋งŒ ์ถ”์ถœ
14
- if isinstance(result, list) and len(result) > 0:
15
- label = result[0].get("label", "Unknown")
16
- score = result[0].get("score", 0)
17
- return f"{label} ({score:.2f})"
18
- return "No result"
19
-
20
- demo = gr.Interface(
21
- fn=predict,
22
- inputs=gr.Textbox(label="Lyrics"),
23
- outputs=gr.Textbox(label="Predicted Genre"),
24
- title="Lyrics Genre Predictor"
25
  )
26
 
 
 
 
27
  if __name__ == "__main__":
28
- demo.launch()
 
 
 
 
 
1
  import gradio as gr
2
+ # ... (ํ† ํฌ๋‚˜์ด์ €, ๋ชจ๋ธ ๋กœ๋”ฉ ์ฝ”๋“œ) ...
 
3
 
4
+ def predict_lyrics(lyrics_text):
5
+ print(f"API๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ๊ฐ€์‚ฌ: '{lyrics_text}'") # โ˜…โ˜…โ˜… ์ค‘์š”: ๋กœ๊ทธ ํ™•์ธ์šฉ โ˜…โ˜…โ˜…
6
+ if not lyrics_text or not isinstance(lyrics_text, str) or lyrics_text.strip() == "":
7
+ print("์ž…๋ ฅ ๊ฐ€์‚ฌ๊ฐ€ ๋น„์–ด์žˆ๊ฑฐ๋‚˜ ์œ ํšจํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")
8
+ return "์ž…๋ ฅ ๊ฐ€์‚ฌ๊ฐ€ ๋น„์–ด์žˆ์Šต๋‹ˆ๋‹ค. (์‹ ๋ขฐ๋„: 0%)" # ๋˜๋Š” ๋‹ค๋ฅธ ๊ธฐ๋ณธ ์‘๋‹ต
9
 
10
+ # ... (์‹ค์ œ ๋ชจ๋ธ ์˜ˆ์ธก ๋กœ์ง) ...
11
+ # ์˜ˆ์‹œ: result = pipeline(lyrics_text)
12
+ # label = result[0]['label']
13
+ # score = result[0]['score']
14
+ # return f"{label} (์‹ ๋ขฐ๋„: {score:.2f})"
15
+ return f"์˜ˆ์ธก๋œ ๊ฒฐ๊ณผ: {lyrics_text[::-1]}" # ์ž„์‹œ ๋ฐ˜ํ™˜๊ฐ’ (์‹ค์ œ ๋กœ์ง์œผ๋กœ ๋Œ€์ฒด)
16
 
17
+ iface = gr.Interface(
18
+ fn=predict_lyrics,
19
+ inputs=gr.Textbox(lines=5, placeholder="๊ฐ€์‚ฌ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”..."),
20
+ outputs=gr.Textbox(),
21
+ title="๊ฐ€์‚ฌ ์žฅ๋ฅด ์˜ˆ์ธก",
22
+ description="๊ฐ€์‚ฌ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์žฅ๋ฅด๋ฅผ ์˜ˆ์ธกํ•ด์ค๋‹ˆ๋‹ค.",
23
+ # api_name="predict" # ๋ช…์‹œ์ ์œผ๋กœ API ์ด๋ฆ„์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’๋„ "predict"์ผ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค.
 
 
 
 
 
 
 
24
  )
25
 
26
+ # ํ ์„ค์ •์„ ํ†ตํ•ด API๋ฅผ ์—ด์–ด๋‘˜ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
27
+ # iface.queue(api_open=True) # ์ด ์ค„์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜, ์•„๋ž˜ launch()์— queue()๋ฅผ ์ง์ ‘ ํ˜ธ์ถœ
28
+
29
  if __name__ == "__main__":
30
+ # iface.launch(share=True) # share=True๋Š” ์™ธ๋ถ€ ์ ‘์†์šฉ์ด์ง€๋งŒ, hf spaces์—์„œ๋Š” ๋ถˆํ•„์š”
31
+ # queue()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ API๋ฅผ ์—ด์–ด๋ณด์„ธ์š”.
32
+ iface.queue().launch() # ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด api_open=True๊ฐ€ ๊ธฐ๋ณธ๊ฐ’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
33
+ # ๋˜๋Š” iface.launch(api_open=True) ๊ฐ™์€ ์˜ต์…˜์€ Gradio ๋ฒ„์ „์— ๋”ฐ๋ผ ๋‹ค๋ฆ„
34
+ # ๋ช…์‹œ์ ์œผ๋กœ iface.queue(api_open=True).launch()๋ฅผ ์‹œ๋„ํ•ด๋ณด์„ธ์š”.