Update app.py
Browse files
app.py
CHANGED
@@ -105,22 +105,26 @@ MAJOR_COUNTRIES = list(COUNTRY_LOCATIONS.keys())
|
|
105 |
def translate_query(query, country):
|
106 |
try:
|
107 |
if country in COUNTRY_LANGUAGES:
|
|
|
|
|
|
|
108 |
target_lang = COUNTRY_LANGUAGES[country]
|
109 |
prompt = f"Translate only this text to {target_lang} language without any explanation. Output only translated text: {query}"
|
110 |
|
111 |
translated = hf_client.text_generation(
|
112 |
prompt,
|
113 |
-
max_new_tokens=
|
114 |
temperature=0.1
|
115 |
)
|
116 |
-
|
|
|
117 |
print(f"Original query: {query}")
|
118 |
print(f"Translated query: {translated}")
|
119 |
return translated
|
120 |
-
return query
|
121 |
except Exception as e:
|
122 |
print(f"Translation error: {str(e)}")
|
123 |
-
return query
|
124 |
|
125 |
def search_serphouse(query, country, page=1, num_result=10):
|
126 |
url = "https://api.serphouse.com/serp/live"
|
@@ -135,19 +139,14 @@ def search_serphouse(query, country, page=1, num_result=10):
|
|
135 |
"q": translated_query,
|
136 |
"domain": "google.com",
|
137 |
"loc": COUNTRY_LOCATIONS.get(country, "United States"),
|
138 |
-
"lang": "en",
|
139 |
"device": "desktop",
|
140 |
-
"serp_type": "
|
141 |
"page": "1",
|
142 |
-
"
|
143 |
-
"verbatim": "0",
|
144 |
-
"gfilter": "0"
|
145 |
}
|
146 |
}
|
147 |
|
148 |
-
# API ์์ฒญ ๋๋ฒ๊น
์ ์ํ ์ถ๋ ฅ
|
149 |
-
print("Full payload:", json.dumps(payload, indent=2, ensure_ascii=False))
|
150 |
-
|
151 |
headers = {
|
152 |
"accept": "application/json",
|
153 |
"content-type": "application/json",
|
@@ -156,8 +155,8 @@ def search_serphouse(query, country, page=1, num_result=10):
|
|
156 |
|
157 |
try:
|
158 |
response = requests.post(url, json=payload, headers=headers)
|
|
|
159 |
print("Response status:", response.status_code)
|
160 |
-
print("Response content:", response.text[:500]) # ์๋ต ๋ด์ฉ ํ์ธ
|
161 |
|
162 |
response.raise_for_status()
|
163 |
return {"results": response.json(), "translated_query": translated_query}
|
|
|
105 |
def translate_query(query, country):
|
106 |
try:
|
107 |
if country in COUNTRY_LANGUAGES:
|
108 |
+
# ์
๋ ฅ ๊ฒ์์ด ๊ธธ์ด ์ ํ
|
109 |
+
query = query[:100] # ์
๋ ฅ ๊ฒ์์ด ๊ธธ์ด ์ ํ
|
110 |
+
|
111 |
target_lang = COUNTRY_LANGUAGES[country]
|
112 |
prompt = f"Translate only this text to {target_lang} language without any explanation. Output only translated text: {query}"
|
113 |
|
114 |
translated = hf_client.text_generation(
|
115 |
prompt,
|
116 |
+
max_new_tokens=50,
|
117 |
temperature=0.1
|
118 |
)
|
119 |
+
# ๋ฒ์ญ๋ ํ
์คํธ ์ ์ ๋ฐ ๊ธธ์ด ์ ํ
|
120 |
+
translated = translated.strip()[:255] # API ์ ํ์ ๋ง์ถฐ 255์๋ก ์ ํ
|
121 |
print(f"Original query: {query}")
|
122 |
print(f"Translated query: {translated}")
|
123 |
return translated
|
124 |
+
return query[:255] # ๋ฒ์ญํ์ง ์๋ ๊ฒฝ์ฐ์๋ ๊ธธ์ด ์ ํ
|
125 |
except Exception as e:
|
126 |
print(f"Translation error: {str(e)}")
|
127 |
+
return query[:255]
|
128 |
|
129 |
def search_serphouse(query, country, page=1, num_result=10):
|
130 |
url = "https://api.serphouse.com/serp/live"
|
|
|
139 |
"q": translated_query,
|
140 |
"domain": "google.com",
|
141 |
"loc": COUNTRY_LOCATIONS.get(country, "United States"),
|
142 |
+
"lang": COUNTRY_LANGUAGES.get(country, "en"),
|
143 |
"device": "desktop",
|
144 |
+
"serp_type": "news",
|
145 |
"page": "1",
|
146 |
+
"num": "10" # num์ผ๋ก ๋ณ๊ฒฝ
|
|
|
|
|
147 |
}
|
148 |
}
|
149 |
|
|
|
|
|
|
|
150 |
headers = {
|
151 |
"accept": "application/json",
|
152 |
"content-type": "application/json",
|
|
|
155 |
|
156 |
try:
|
157 |
response = requests.post(url, json=payload, headers=headers)
|
158 |
+
print("Request payload:", json.dumps(payload, indent=2, ensure_ascii=False))
|
159 |
print("Response status:", response.status_code)
|
|
|
160 |
|
161 |
response.raise_for_status()
|
162 |
return {"results": response.json(), "translated_query": translated_query}
|