blockenters commited on
Commit
e910ec9
ยท
1 Parent(s): bbe6e89
Files changed (1) hide show
  1. app.py +19 -27
app.py CHANGED
@@ -1,33 +1,25 @@
1
  import gradio as gr
2
- from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
3
 
4
- # 1. ๊ฐ์ • ๋ถ„์„ ๋ชจ๋ธ ๋กœ๋“œ
5
- model_name = "kykim/bert-kor-base"
6
- tokenizer = AutoTokenizer.from_pretrained(model_name)
7
- model = AutoModelForSequenceClassification.from_pretrained(model_name)
8
- sentiment_analyzer = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
9
 
10
- # 2. ๊ฐ์ • ๋ถ„์„ ํ•จ์ˆ˜ ์ •์˜
11
- def analyze_sentiment_korean(text):
12
- # ๊ฐ์ • ๋ถ„์„ ์ˆ˜ํ–‰
13
- results = sentiment_analyzer(text)
14
- # ๊ฒฐ๊ณผ ๊ฐ€๊ณต (label: 0=๋ถ€์ •, 1=๊ธ์ •)
15
- label = "๊ธ์ •" if results[0]['label'] == "1" else "๋ถ€์ •"
16
- score = results[0]['score']
17
- return f"์˜ˆ์ธก: {label} (ํ™•๋ฅ : {score:.2f})"
18
 
19
- # 3. Gradio ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ
20
- with gr.Blocks() as demo:
21
- gr.Markdown("# ํ•œ๊ธ€ ๊ฐ์ • ๋ถ„์„ ๋ฐ๋ชจ")
22
- gr.Markdown("ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๊ฐ์ •์„ ๋ถ„์„ํ•ฉ๋‹ˆ๋‹ค. (๊ธ์ •/๋ถ€์ •)")
 
 
 
 
 
 
23
 
24
- with gr.Row():
25
- input_text = gr.Textbox(label="ํ•œ๊ธ€ ํ…์ŠคํŠธ ์ž…๋ ฅ", placeholder="ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.")
26
- output_text = gr.Textbox(label="๊ฒฐ๊ณผ")
27
-
28
- analyze_button = gr.Button("๋ถ„์„ํ•˜๊ธฐ")
29
- analyze_button.click(analyze_sentiment_korean, inputs=input_text, outputs=output_text)
30
-
31
- # 4. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰
32
  if __name__ == "__main__":
33
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
 
4
+ # 1. ์‚ฌ์ „ํ•™์Šต๋œ ๊ฐ์ • ๋ถ„์„ ํŒŒ์ดํ”„๋ผ์ธ ๋กœ๋“œ
5
+ sentiment_pipeline = pipeline("sentiment-analysis")
 
 
 
6
 
7
+ # 2. ์˜ˆ์ธก ํ•จ์ˆ˜ ์ •์˜
8
+ def analyze_sentiment(text):
9
+ results = sentiment_pipeline(text)
10
+ return results
 
 
 
 
11
 
12
+ # 3. Gradio UI ๊ตฌ์„ฑ
13
+ # - inputs: ํ…์ŠคํŠธ ์ž…๋ ฅ
14
+ # - outputs: ๊ฒฐ๊ณผ(JSON ํ˜•์‹ ํ‘œ์‹œ)
15
+ demo = gr.Interface(
16
+ fn=analyze_sentiment,
17
+ inputs=gr.Textbox(lines=3, placeholder="ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”"),
18
+ outputs="json",
19
+ title="๊ฐ์ • ๋ถ„์„ ๋ฐ๋ชจ",
20
+ description="Hugging Face Transformers์˜ ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ฌ์šฉํ•ด ๊ฐ์ •(๊ธ์ •/๋ถ€์ •)์„ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค."
21
+ )
22
 
23
+ # 4. ์‹คํ–‰
 
 
 
 
 
 
 
24
  if __name__ == "__main__":
25
+ demo.launch()