Spaces:
Running
Running
Update app.py
Browse files
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 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|