Edit app.py
Browse files
app.py
CHANGED
|
@@ -2,27 +2,72 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
| 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 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
label = result[0].get("label", "Unknown")
|
| 16 |
-
score = result[0].get("score", 0)
|
|
|
|
| 17 |
return f"{label} ({score:.2f})"
|
| 18 |
-
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
print("Gradio app script started.") # ์คํฌ๋ฆฝํธ ์์ ๋ก๊ทธ
|
| 6 |
|
| 7 |
+
MODEL_PATH = "./" # ๋ชจ๋ธ ํ์ผ์ด ์ปจํ
์ด๋ ๋ด app.py์ ๋์ผํ ๋๋ ํ ๋ฆฌ์ ์๋ค๊ณ ๊ฐ์
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
pipeline_instance = None # ํ์ดํ๋ผ์ธ ์ธ์คํด์ค๋ฅผ ์ ์ญ์ ์ผ๋ก ์ ์ธ
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
print(f"Loading tokenizer from: {MODEL_PATH}")
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 14 |
+
print(f"Loading model from: {MODEL_PATH}")
|
| 15 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
|
| 16 |
+
device_to_use = 0 if torch.cuda.is_available() else -1 # GPU ์ฐ์ ์ฌ์ฉ, ์์ผ๋ฉด CPU
|
| 17 |
+
print(f"Using device: {'cuda:0' if device_to_use == 0 else 'cpu'}")
|
| 18 |
+
pipeline_instance = TextClassificationPipeline(model=model, tokenizer=tokenizer, device=device_to_use)
|
| 19 |
+
print("Pipeline created successfully.")
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print(f"Error loading model, tokenizer, or creating pipeline: {e}")
|
| 22 |
+
# ํ์ดํ๋ผ์ธ ์์ฑ ์คํจ ์ ์ฑ์ด ๊ณ์ ์คํ๋๋๋ก ํ์ง๋ง, predict ํจ์์์ ์ฒ๋ฆฌ
|
| 23 |
+
|
| 24 |
+
def predict(lyrics: str): # ์
๋ ฅ ํ์
๋ช
์ (Python 3.9+ ์์ ๊ถ์ฅ)
|
| 25 |
+
print(f"--- PREDICT FUNCTION CALLED ---")
|
| 26 |
+
print(f"Received lyrics: '{lyrics}' (Type: {type(lyrics)})")
|
| 27 |
+
|
| 28 |
+
if pipeline_instance is None:
|
| 29 |
+
print("Pipeline is not initialized. Cannot predict.")
|
| 30 |
+
return "์ค๋ฅ: ๋ชจ๋ธ ํ์ดํ๋ผ์ธ์ด ์ด๊ธฐํ๋์ง ์์์ต๋๋ค. (0.00)"
|
| 31 |
+
|
| 32 |
+
if not lyrics or not isinstance(lyrics, str) or lyrics.strip() == "":
|
| 33 |
+
print("Lyrics are empty, not a string, or whitespace only.")
|
| 34 |
+
return "์
๋ ฅ ๊ฐ์ฌ๊ฐ ๋น์ด์๊ฑฐ๋ ์ ํจํ์ง ์์ต๋๋ค. (0.00)"
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
print("Performing prediction with pipeline...")
|
| 38 |
+
result = pipeline_instance(lyrics) # ์ ์ญ pipeline_instance ์ฌ์ฉ
|
| 39 |
+
print(f"Pipeline raw result: {result}")
|
| 40 |
+
except Exception as e:
|
| 41 |
+
print(f"Error during pipeline prediction: {e}")
|
| 42 |
+
return f"์์ธก ์ค ์ค๋ฅ ๋ฐ์: {str(e)} (0.00)" # ์ค๋ฅ ๋ฉ์์ง ํฌํจ
|
| 43 |
+
|
| 44 |
+
# ๊ฒฐ๊ณผ ์ถ์ถ ๋ก์ง (์ด์ ๊ณผ ๋์ผ)
|
| 45 |
+
if isinstance(result, list) and len(result) > 0 and isinstance(result[0], dict):
|
| 46 |
label = result[0].get("label", "Unknown")
|
| 47 |
+
score = result[0].get("score", 0.0) # ์ ์๊ฐ float์ธ์ง ํ์ธ
|
| 48 |
+
print(f"Extracted prediction: Label='{label}', Score={score}")
|
| 49 |
return f"{label} ({score:.2f})"
|
| 50 |
+
else:
|
| 51 |
+
print(f"Pipeline returned no result or unexpected format: {result}")
|
| 52 |
+
return "๊ฒฐ๊ณผ๋ฅผ ์ถ์ถํ ์ ์๊ฑฐ๋ ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค. (0.00)"
|
| 53 |
|
| 54 |
+
print("Creating Gradio Interface...")
|
| 55 |
demo = gr.Interface(
|
| 56 |
fn=predict,
|
| 57 |
+
inputs=gr.Textbox(label="Lyrics", placeholder="์ฌ๊ธฐ์ ๊ฐ์ฌ๋ฅผ ์
๋ ฅํ์ธ์..."),
|
| 58 |
outputs=gr.Textbox(label="Predicted Genre"),
|
| 59 |
+
title="Lyrics Genre Predictor (Local Docker)",
|
| 60 |
+
description="๊ฐ์ฌ๋ฅผ ์
๋ ฅํ๋ฉด ๋ก์ปฌ Docker ์ปจํ
์ด๋์์ ์คํ ์ค์ธ ๋ชจ๋ธ์ด ์ฅ๋ฅด๋ฅผ ์์ธกํฉ๋๋ค."
|
| 61 |
+
# api_name="predict" # ๋ช
์์ ์ผ๋ก API ์ด๋ฆ์ ์ค์ ํ๋ฉด /api/predict ์๋ํฌ์ธํธ๊ฐ ํ์คํ ์์ฑ๋ฉ๋๋ค.
|
| 62 |
)
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
+
print("Launching Gradio app...")
|
| 66 |
+
# API ์ ๊ทผ์ ์ํด์๋ queue()๋ฅผ ์ฌ์ฉํ๊ณ server_name์ ์ค์ ํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
|
| 67 |
+
# server_name="0.0.0.0"์ Docker ์ปจํ
์ด๋ ์ธ๋ถ(์: ํธ์คํธ์ Next.js)์์ ์ ๊ทผ ํ์ฉ
|
| 68 |
+
# server_port=7860์ Gradio ๊ธฐ๋ณธ ํฌํธ
|
| 69 |
+
# queue() ์ฌ์ฉ ์ api_open=True๊ฐ ๊ธฐ๋ณธ๊ฐ์ธ ๊ฒฝ์ฐ๊ฐ ๋ง์ /api/predict ์๋ํฌ์ธํธ๊ฐ ํ์ฑํ๋ ๊ฐ๋ฅ์ฑ์ด ๋์
|
| 70 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860)
|
| 71 |
+
# ๋๋, queue() ์์ด api_name์ ๋ช
์:
|
| 72 |
+
# demo.launch(server_name="0.0.0.0", server_port=7860, api_name="predict")
|
| 73 |
+
print(f"Gradio app launched. Access UI at http://localhost:7860. API (likely) at http://localhost:7860/api/predict")
|