Spaces:
openfree
/
Running on CPU Upgrade

seawolf2357 commited on
Commit
9d3056e
ยท
verified ยท
1 Parent(s): c12b763

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -7
app.py CHANGED
@@ -5,6 +5,9 @@ import os
5
  from datetime import datetime, timedelta
6
  from huggingface_hub import InferenceClient
7
 
 
 
 
8
  API_KEY = os.getenv("SERPHOUSE_API_KEY")
9
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN"))
10
 
@@ -116,23 +119,38 @@ COUNTRY_LOCATIONS = {
116
 
117
  MAJOR_COUNTRIES = list(COUNTRY_LOCATIONS.keys())
118
 
 
119
  def translate_query(query, country):
120
  try:
121
  if country in COUNTRY_LANGUAGES:
 
122
  target_lang = COUNTRY_LANGUAGES[country]
123
- prompt = f"Translate the following English text to {target_lang} language. Only output the translated text without any explanations or quotes: {query}"
124
 
125
- translated = hf_client.text_generation(
126
- prompt,
127
- max_new_tokens=250,
128
- temperature=0.3
129
- )
130
- return translated.strip()
 
 
 
 
 
 
 
 
131
  return query
132
  except Exception as e:
133
  print(f"Translation error: {str(e)}")
134
  return query
135
 
 
 
 
 
 
 
136
  def search_serphouse(query, country, page=1, num_result=100):
137
  url = "https://api.serphouse.com/serp/live"
138
 
 
5
  from datetime import datetime, timedelta
6
  from huggingface_hub import InferenceClient
7
 
8
+ from googletrans import Translator
9
+
10
+
11
  API_KEY = os.getenv("SERPHOUSE_API_KEY")
12
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN"))
13
 
 
119
 
120
  MAJOR_COUNTRIES = list(COUNTRY_LOCATIONS.keys())
121
 
122
+
123
  def translate_query(query, country):
124
  try:
125
  if country in COUNTRY_LANGUAGES:
126
+ translator = Translator()
127
  target_lang = COUNTRY_LANGUAGES[country]
 
128
 
129
+ # ์˜์–ด๋กœ ๋œ ์ž…๋ ฅ์€ ๋ฒˆ์—ญํ•˜์ง€ ์•Š์Œ
130
+ if is_english(query):
131
+ return query
132
+
133
+ # ํ•œ๊ตญ์–ด ์ž…๋ ฅ์— ๋Œ€ํ•ด South Korea๊ฐ€ ์„ ํƒ๋œ ๊ฒฝ์šฐ ๋ฒˆ์—ญํ•˜์ง€ ์•Š์Œ
134
+ if country == "South Korea" and is_korean(query):
135
+ return query
136
+
137
+ # ๊ตฌ๊ธ€ ๋ฒˆ์—ญ ์‹คํ–‰
138
+ translated = translator.translate(query, dest=target_lang)
139
+ print(f"Original query: {query}")
140
+ print(f"Translated query: {translated.text}")
141
+ return translated.text
142
+
143
  return query
144
  except Exception as e:
145
  print(f"Translation error: {str(e)}")
146
  return query
147
 
148
+ def is_english(text):
149
+ return all(ord(char) < 128 for char in text.replace(' ', ''))
150
+
151
+ def is_korean(text):
152
+ return any('\uAC00' <= char <= '\uD7A3' for char in text)
153
+
154
  def search_serphouse(query, country, page=1, num_result=100):
155
  url = "https://api.serphouse.com/serp/live"
156