Update app.py
Browse files
app.py
CHANGED
@@ -75,123 +75,103 @@ def create_deepseek_interface():
|
|
75 |
print(f"์๋ณธ ์ฟผ๋ฆฌ: {query}")
|
76 |
print(extraction_result)
|
77 |
|
78 |
-
#
|
79 |
-
is_korean = any('\uAC00' <= c <= '\uD7A3' for c in search_query)
|
80 |
-
|
81 |
-
# MoneyRadar ๋ฐฉ์ ์ ํํ ๋ฐ๋ฅด๊ธฐ
|
82 |
-
from datetime import datetime, timedelta
|
83 |
-
now = datetime.utcnow()
|
84 |
-
yesterday = now - timedelta(days=1)
|
85 |
-
date_range = f"{yesterday.strftime('%Y-%m-%d')},{now.strftime('%Y-%m-%d')}"
|
86 |
-
|
87 |
-
# SerpHouse API ํธ์ถ ์คํ - POST ๋ฉ์๋ ์ฌ์ฉ (MoneyRadar ์ฝ๋ ์ ํํ ๋ฐ๋ฅด๊ธฐ)
|
88 |
url = "https://api.serphouse.com/serp/live"
|
89 |
|
90 |
-
#
|
91 |
-
|
92 |
-
|
93 |
-
# COUNTRY_LOCATIONS๊ณผ COUNTRY_LANGUAGES ์ ์ (MoneyRadar์์ ์ฌ์ฉ๋๋ ๋งคํ)
|
94 |
-
COUNTRY_LOCATIONS = {"United States": "United States", "KOREA": "kr"}
|
95 |
-
COUNTRY_LANGUAGES = {"United States": "en", "KOREA": "ko"}
|
96 |
|
97 |
-
#
|
98 |
-
|
99 |
-
"q": search_query
|
100 |
"domain": "google.com",
|
101 |
-
"
|
102 |
-
"lang": COUNTRY_LANGUAGES.get(country, "en"),
|
103 |
"device": "desktop",
|
104 |
-
"
|
105 |
-
"page": "1",
|
106 |
-
"num": "100",
|
107 |
-
"date_range": date_range,
|
108 |
-
"sort_by": "date"
|
109 |
}
|
110 |
|
111 |
headers = {
|
112 |
-
"
|
113 |
-
"content-type": "application/json",
|
114 |
-
"authorization": f"Bearer {serphouse_api_key}"
|
115 |
}
|
116 |
|
117 |
-
print(f"SerpHouse API ํธ์ถ ์ค...
|
118 |
-
print(f"
|
119 |
-
|
120 |
-
# POST ์์ฒญ ์ํ (์ธ์
๊ณผ ์ฌ์๋ ๋ก์ง ์ถ๊ฐ)
|
121 |
-
import requests
|
122 |
-
from requests.adapters import HTTPAdapter
|
123 |
-
from requests.packages.urllib3.util.retry import Retry
|
124 |
-
|
125 |
-
session = requests.Session()
|
126 |
-
retries = Retry(
|
127 |
-
total=5,
|
128 |
-
backoff_factor=1,
|
129 |
-
status_forcelist=[500, 502, 503, 504, 429],
|
130 |
-
allowed_methods=["POST"]
|
131 |
-
)
|
132 |
-
adapter = HTTPAdapter(max_retries=retries)
|
133 |
-
session.mount('http://', adapter)
|
134 |
-
session.mount('https://', adapter)
|
135 |
-
|
136 |
-
response = session.post(
|
137 |
-
url,
|
138 |
-
json=payload,
|
139 |
-
headers=headers,
|
140 |
-
timeout=(30, 30)
|
141 |
-
)
|
142 |
|
|
|
|
|
143 |
response.raise_for_status()
|
144 |
|
145 |
print(f"SerpHouse API ์๋ต ์ํ ์ฝ๋: {response.status_code}")
|
146 |
-
|
147 |
|
148 |
-
#
|
149 |
-
search_results = {
|
150 |
-
"results": response_data,
|
151 |
-
"translated_query": search_query
|
152 |
-
}
|
153 |
-
|
154 |
-
# ์๋ต ๊ตฌ์กฐ ๋ก๊น
|
155 |
print(f"์๋ต ๊ตฌ์กฐ: {list(search_results.keys()) if isinstance(search_results, dict) else '๋์
๋๋ฆฌ ์๋'}")
|
156 |
|
157 |
-
# ๊ฒ์ ๊ฒฐ๊ณผ ํ์ฑ ๋ฐ ํฌ๋งทํ
|
158 |
formatted_results = []
|
159 |
formatted_results.append(f"๊ฒ์์ด: {search_query}\n\n")
|
160 |
|
161 |
-
#
|
162 |
-
|
163 |
-
if "results" in search_results["results"]["results"]:
|
164 |
-
# ๋ด์ค ๊ฒฐ๊ณผ ํ์ฑ
|
165 |
-
news_results = search_results["results"]["results"]["results"].get("news", [])
|
166 |
-
if news_results:
|
167 |
-
for result in news_results[:5]: # ์์ 5๊ฐ๋ง ํ์
|
168 |
-
title = result.get("title", "์ ๋ชฉ ์์")
|
169 |
-
snippet = result.get("snippet", "๋ด์ฉ ์์")
|
170 |
-
url = result.get("url", result.get("link", "#"))
|
171 |
-
source = result.get("source", result.get("channel", "์ ์ ์์"))
|
172 |
-
time = result.get("time", result.get("date", "์๊ฐ ์ ๋ณด ์์"))
|
173 |
-
|
174 |
-
formatted_results.append(
|
175 |
-
f"์ ๋ชฉ: {title}\n"
|
176 |
-
f"์ถ์ฒ: {source}\n"
|
177 |
-
f"์๊ฐ: {time}\n"
|
178 |
-
f"๋ด์ฉ: {snippet}\n"
|
179 |
-
f"๋งํฌ: {url}\n\n"
|
180 |
-
)
|
181 |
-
|
182 |
-
print(f"๊ฒ์ ๊ฒฐ๊ณผ {len(news_results)}๊ฐ ์ฐพ์")
|
183 |
-
return "".join(formatted_results)
|
184 |
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
except Exception as e:
|
190 |
error_msg = f"๊ฒ์ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
191 |
print(error_msg)
|
192 |
import traceback
|
193 |
print(traceback.format_exc())
|
194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
|
196 |
# ์คํธ๋ฆฌ๋ฐ ๋ฐฉ์์ผ๋ก DeepSeek API ํธ์ถ ํจ์
|
197 |
def query_deepseek_streaming(message, history, use_deep_research):
|
|
|
75 |
print(f"์๋ณธ ์ฟผ๋ฆฌ: {query}")
|
76 |
print(extraction_result)
|
77 |
|
78 |
+
# ๋ฌธ์ ์ฝ๋๋ฅผ ๋ ์์ธํ ๋ถ์ํด ๋ณด๋ ๊ธฐ๋ณธ GET ๋ฐฉ์ ํ์ฉ์ด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
url = "https://api.serphouse.com/serp/live"
|
80 |
|
81 |
+
# ํ๊ธ ๊ฒ์์ด์ธ์ง ํ์ธ
|
82 |
+
is_korean = any('\uAC00' <= c <= '\uD7A3' for c in search_query)
|
|
|
|
|
|
|
|
|
83 |
|
84 |
+
# ๊ฐ์ํ๋ ํ๋ผ๋ฏธํฐ๋ก ์๋
|
85 |
+
params = {
|
86 |
+
"q": search_query,
|
87 |
"domain": "google.com",
|
88 |
+
"serp_type": "web", # ๊ธฐ๋ณธ ์น ๊ฒ์์ผ๋ก ๋ณ๊ฒฝ
|
|
|
89 |
"device": "desktop",
|
90 |
+
"lang": "ko" if is_korean else "en"
|
|
|
|
|
|
|
|
|
91 |
}
|
92 |
|
93 |
headers = {
|
94 |
+
"Authorization": f"Bearer {serphouse_api_key}"
|
|
|
|
|
95 |
}
|
96 |
|
97 |
+
print(f"SerpHouse API ํธ์ถ ์ค... ๊ธฐ๋ณธ GET ๋ฐฉ์์ผ๋ก ์๋")
|
98 |
+
print(f"๊ฒ์์ด: {search_query}")
|
99 |
+
print(f"์์ฒญ URL: {url} - ํ๋ผ๋ฏธํฐ: {params}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
+
# GET ์์ฒญ ์ํ
|
102 |
+
response = requests.get(url, headers=headers, params=params)
|
103 |
response.raise_for_status()
|
104 |
|
105 |
print(f"SerpHouse API ์๋ต ์ํ ์ฝ๋: {response.status_code}")
|
106 |
+
search_results = response.json()
|
107 |
|
108 |
+
# ์๋ต ๊ตฌ์กฐ ํ์ธ
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
print(f"์๋ต ๊ตฌ์กฐ: {list(search_results.keys()) if isinstance(search_results, dict) else '๋์
๋๋ฆฌ ์๋'}")
|
110 |
|
111 |
+
# ๊ฒ์ ๊ฒฐ๊ณผ ํ์ฑ ๋ฐ ํฌ๋งทํ
|
112 |
formatted_results = []
|
113 |
formatted_results.append(f"๊ฒ์์ด: {search_query}\n\n")
|
114 |
|
115 |
+
# ๋ค์ํ ๊ฐ๋ฅํ ์๋ต ๊ตฌ์กฐ์ ๋ํ ์ฒ๋ฆฌ
|
116 |
+
organic_results = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
+
# ๊ฐ๋ฅํ ์๋ต ๊ตฌ์กฐ 1
|
119 |
+
if "results" in search_results and "organic" in search_results["results"]:
|
120 |
+
organic_results = search_results["results"]["organic"]
|
121 |
|
122 |
+
# ๊ฐ๋ฅํ ์๋ต ๊ตฌ์กฐ 2
|
123 |
+
elif "organic" in search_results:
|
124 |
+
organic_results = search_results["organic"]
|
125 |
+
|
126 |
+
# ๊ฐ๋ฅํ ์๋ต ๊ตฌ์กฐ 3 (์ค์ฒฉ๋ results)
|
127 |
+
elif "results" in search_results and "results" in search_results["results"]:
|
128 |
+
if "organic" in search_results["results"]["results"]:
|
129 |
+
organic_results = search_results["results"]["results"]["organic"]
|
130 |
+
|
131 |
+
# organic_results๊ฐ ์์ผ๋ฉด ์ฒ๋ฆฌ
|
132 |
+
if organic_results and len(organic_results) > 0:
|
133 |
+
# ์๋ต ๊ตฌ์กฐ ์ถ๋ ฅ
|
134 |
+
print(f"์ฒซ๋ฒ์งธ organic ๊ฒฐ๊ณผ ๊ตฌ์กฐ: {organic_results[0].keys() if len(organic_results) > 0 else 'empty'}")
|
135 |
+
|
136 |
+
for result in organic_results[:5]: # ์์ 5๊ฐ ๊ฒฐ๊ณผ๋ง ํ์
|
137 |
+
title = result.get("title", "์ ๋ชฉ ์์")
|
138 |
+
snippet = result.get("snippet", "๋ด์ฉ ์์")
|
139 |
+
link = result.get("link", "#")
|
140 |
+
|
141 |
+
formatted_results.append(
|
142 |
+
f"์ ๋ชฉ: {title}\n"
|
143 |
+
f"๋ด์ฉ: {snippet}\n"
|
144 |
+
f"๋งํฌ: {link}\n\n"
|
145 |
+
)
|
146 |
+
|
147 |
+
print(f"๊ฒ์ ๊ฒฐ๊ณผ {len(organic_results)}๊ฐ ์ฐพ์")
|
148 |
+
return "".join(formatted_results)
|
149 |
+
|
150 |
+
# ๊ฒฐ๊ณผ๊ฐ ์๊ฑฐ๋ ์์์น ๋ชปํ ๊ตฌ์กฐ์ธ ๊ฒฝ์ฐ
|
151 |
+
print("๊ฒ์ ๊ฒฐ๊ณผ ์์ ๋๋ ์์์น ๋ชปํ ์๋ต ๊ตฌ์กฐ")
|
152 |
+
print(f"์๋ต ๊ตฌ์กฐ ์์ธ: {search_results.keys() if hasattr(search_results, 'keys') else '๋ถ๋ช
ํํ ๊ตฌ์กฐ'}")
|
153 |
+
|
154 |
+
# ์๋ต ๋ด์ฉ์์ ์ค๋ฅ ๋ฉ์์ง ์ฐพ๊ธฐ
|
155 |
+
error_msg = "๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์๊ฑฐ๋ ์๋ต ํ์์ด ์์๊ณผ ๋ค๋ฆ
๋๋ค"
|
156 |
+
if "error" in search_results:
|
157 |
+
error_msg = search_results["error"]
|
158 |
+
elif "message" in search_results:
|
159 |
+
error_msg = search_results["message"]
|
160 |
+
|
161 |
+
return f"๊ฒ์์ด '{search_query}'์ ๋ํ ๊ฒฐ๊ณผ: {error_msg}"
|
162 |
+
|
163 |
except Exception as e:
|
164 |
error_msg = f"๊ฒ์ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
165 |
print(error_msg)
|
166 |
import traceback
|
167 |
print(traceback.format_exc())
|
168 |
+
|
169 |
+
# ๋๋ฒ๊น
๋ชฉ์ ์ผ๋ก API ์์ฒญ ์์ธ ์ ๋ณด ์ถ๊ฐ
|
170 |
+
return f"๊ฒ์ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}\n\n" + \
|
171 |
+
f"API ์์ฒญ ์์ธ ์ ๋ณด:\n" + \
|
172 |
+
f"- URL: {url}\n" + \
|
173 |
+
f"- ๊ฒ์์ด: {search_query}\n" + \
|
174 |
+
f"- ํ๋ผ๋ฏธํฐ: {params}\n"
|
175 |
|
176 |
# ์คํธ๋ฆฌ๋ฐ ๋ฐฉ์์ผ๋ก DeepSeek API ํธ์ถ ํจ์
|
177 |
def query_deepseek_streaming(message, history, use_deep_research):
|