seawolf2357 commited on
Commit
b1a1529
ยท
verified ยท
1 Parent(s): 9b87dc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -5
app.py CHANGED
@@ -102,16 +102,32 @@ COUNTRY_LOCATIONS = {
102
 
103
  MAJOR_COUNTRIES = list(COUNTRY_LOCATIONS.keys())
104
 
 
 
 
 
105
  def translate_query(query, country):
106
  try:
107
- # ํ•œ๊ธ€ ์ž…๋ ฅ์ด๊ณ  South Korea๊ฐ€ ์„ ํƒ๋œ ๊ฒฝ์šฐ ์›๋ฌธ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜
 
 
 
 
 
108
  if country == "South Korea":
109
- return query[:255] # ๊ธธ์ด ์ œํ•œ๋งŒ ์ ์šฉ
110
 
111
  if country in COUNTRY_LANGUAGES:
112
  query = query[:100]
113
  target_lang = COUNTRY_LANGUAGES[country]
114
- prompt = f"Translate only this text to {target_lang} language without any explanation. Output only translated text: {query}"
 
 
 
 
 
 
 
115
 
116
  translated = hf_client.text_generation(
117
  prompt,
@@ -242,9 +258,11 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as
242
  def search_and_display(query, country, articles_state, progress=gr.Progress()):
243
  progress(0, desc="๊ฒ€์ƒ‰ ์‹œ์ž‘...")
244
 
245
- # ๋ฒˆ์—ญ ์ฒ˜๋ฆฌ (South Korea ์„ ํƒ ์‹œ ๋ฒˆ์—ญํ•˜์ง€ ์•Š์Œ)
246
  translated_query = translate_query(query, country)
247
- if country == "South Korea":
 
 
248
  translated_display_text = f"๊ฒ€์ƒ‰์–ด: {query}"
249
  elif translated_query != query:
250
  translated_display_text = f"์›๋ณธ ๊ฒ€์ƒ‰์–ด: {query}\n๋ฒˆ์—ญ๋œ ๊ฒ€์ƒ‰์–ด: {translated_query}"
 
102
 
103
  MAJOR_COUNTRIES = list(COUNTRY_LOCATIONS.keys())
104
 
105
+ def is_english(text):
106
+ # ์˜์–ด๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ
107
+ return all(ord(char) < 128 for char in text.replace(' ', ''))
108
+
109
  def translate_query(query, country):
110
  try:
111
+ # ์˜์–ด ์ž…๋ ฅ์ธ ๊ฒฝ์šฐ ๋ฒˆ์—ญํ•˜์ง€ ์•Š๊ณ  ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ
112
+ if is_english(query):
113
+ print(f"English query detected, using original: {query}")
114
+ return query[:255]
115
+
116
+ # ํ•œ๊ธ€ ์ž…๋ ฅ์ด๊ณ  South Korea๊ฐ€ ์„ ํƒ๋œ ๊ฒฝ์šฐ
117
  if country == "South Korea":
118
+ return query[:255]
119
 
120
  if country in COUNTRY_LANGUAGES:
121
  query = query[:100]
122
  target_lang = COUNTRY_LANGUAGES[country]
123
+
124
+ # ๋ฒˆ์—ญ ํ”„๋กฌํ”„ํŠธ ๊ฐœ์„ 
125
+ prompt = f"""Translate this text to {target_lang} language.
126
+ For Japanese, use Kanji and Kana.
127
+ For Chinese, use Simplified Chinese.
128
+ For Korean, use Hangul.
129
+ Only output the translated text without any explanation.
130
+ Text to translate: {query}"""
131
 
132
  translated = hf_client.text_generation(
133
  prompt,
 
258
  def search_and_display(query, country, articles_state, progress=gr.Progress()):
259
  progress(0, desc="๊ฒ€์ƒ‰ ์‹œ์ž‘...")
260
 
261
+ # ๋ฒˆ์—ญ ๋ฐ ํ‘œ์‹œ ํ…์ŠคํŠธ ์ฒ˜๋ฆฌ
262
  translated_query = translate_query(query, country)
263
+ if is_english(query):
264
+ translated_display_text = f"์˜์–ด ๊ฒ€์ƒ‰์–ด: {query}"
265
+ elif country == "South Korea":
266
  translated_display_text = f"๊ฒ€์ƒ‰์–ด: {query}"
267
  elif translated_query != query:
268
  translated_display_text = f"์›๋ณธ ๊ฒ€์ƒ‰์–ด: {query}\n๋ฒˆ์—ญ๋œ ๊ฒ€์ƒ‰์–ด: {translated_query}"