seawolf2357 commited on
Commit
89ea00c
โ€ข
1 Parent(s): 4f8337d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -14
app.py CHANGED
@@ -1,25 +1,45 @@
 
1
  import gradio as gr
2
- from googletrans import Translator
 
 
 
 
3
 
4
- def translate_to_english_keywords(text):
5
- # Translator ๊ฐ์ฒด ์ƒ์„ฑ
6
- translator = Translator()
7
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  # ์ž…๋ ฅ๋ฐ›์€ ํ•œ๊ธ€ ๋ฌธ์žฅ์„ ์˜์–ด๋กœ ๋ฒˆ์—ญ
9
- translated_text = translator.translate(text, src='ko', dest='en')
10
-
11
- # ๋ฒˆ์—ญ๋œ ์˜์–ด ๋ฌธ์žฅ์—์„œ ํ‚ค์›Œ๋“œ ์ถ”์ถœ(์—ฌ๊ธฐ์„œ๋Š” ๊ฐ„๋‹จํžˆ ์ „์ฒด ๋ฒˆ์—ญ๋œ ๋ฌธ์žฅ์„ ๋ฐ˜ํ™˜)
12
- # ์‹ค์ œ ํ™œ์šฉ ์‹œ์—๋Š” ํ‚ค์›Œ๋“œ ์ถ”์ถœ ๋กœ์ง์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
13
- return translated_text.text
14
 
15
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
16
  interface = gr.Interface(
17
- fn=translate_to_english_keywords,
18
  inputs=gr.Textbox(lines=2, placeholder="ํ•œ๊ธ€ ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜์„ธ์š”..."),
19
  outputs="text",
20
- title="ํ•œ๊ธ€ ๋ฌธ์žฅ์„ ์˜์–ด ํ‚ค์›Œ๋“œ๋กœ ๋ฒˆ์—ญ",
21
- description="ํ•œ๊ธ€ ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜๋ฉด, ๊ทธ ์˜๋ฏธ๊ฐ€ ํฌํ•จ๋œ ๊ฐ„๋žตํ•˜๊ณ  ํ•จ์ถ•์ ์ธ ์˜์–ด๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค."
22
  )
23
 
24
  # ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰
25
- interface.launch()
 
1
+ import os
2
  import gradio as gr
3
+ from google.cloud import translate_v2 as translate
4
+ from nltk import download
5
+ from nltk.tokenize import word_tokenize
6
+ from nltk.corpus import stopwords
7
+ from nltk import pos_tag
8
 
9
+ # nltk ๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ
10
+ download('punkt')
11
+ download('averaged_perceptron_tagger')
12
+ download('stopwords')
13
+
14
+ # Google Cloud Translation ํด๋ผ์ด์–ธํŠธ ์„ค์ •
15
+ translate_client = translate.Client()
16
+
17
+ def extract_keywords(text):
18
+ # ์˜์–ด ํ…์ŠคํŠธ๋ฅผ ํ† ํฐํ™”
19
+ tokens = word_tokenize(text)
20
+ # ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ๋ฐ ์ค‘์š” ๋‹จ์–ด ์ถ”์ถœ
21
+ tokens = [word for word in tokens if word.isalnum() and word.lower() not in stopwords.words('english')]
22
+ # ํ’ˆ์‚ฌ ํƒœ๊น…
23
+ tagged = pos_tag(tokens)
24
+ # ๋ช…์‚ฌ, ๊ณ ์œ ๋ช…์‚ฌ, ๋™์‚ฌ ์ค‘์š” ํ‚ค์›Œ๋“œ ์ถ”์ถœ
25
+ keywords = [word for word, tag in tagged if tag in ['NN', 'NNP', 'NNS', 'VB', 'VBD', 'VBG', 'VBN', 'VBP', 'VBZ']]
26
+ return ' '.join(keywords)
27
+
28
+ def translate_and_extract_keywords(text):
29
  # ์ž…๋ ฅ๋ฐ›์€ ํ•œ๊ธ€ ๋ฌธ์žฅ์„ ์˜์–ด๋กœ ๋ฒˆ์—ญ
30
+ result = translate_client.translate(text, target_language='en')
31
+ translated_text = result['translatedText']
32
+ # ํ‚ค์›Œ๋“œ ์ถ”์ถœ
33
+ return extract_keywords(translated_text)
 
34
 
35
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
36
  interface = gr.Interface(
37
+ fn=translate_and_extract_keywords,
38
  inputs=gr.Textbox(lines=2, placeholder="ํ•œ๊ธ€ ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜์„ธ์š”..."),
39
  outputs="text",
40
+ title="ํ•œ๊ธ€ ๋ฌธ์žฅ์„ ์˜์–ด ํ‚ค์›Œ๋“œ๋กœ ๋ฒˆ์—ญ ๋ฐ ์ถ”์ถœ",
41
+ description="ํ•œ๊ธ€ ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜๋ฉด, ๊ทธ ์˜๋ฏธ๊ฐ€ ํฌํ•จ๋œ ์˜์–ด ํ‚ค์›Œ๋“œ๋ฅผ ์ถ”์ถœํ•˜์—ฌ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค."
42
  )
43
 
44
  # ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰
45
+ interface.launch(share=True)