openfree commited on
Commit
81be010
Β·
verified Β·
1 Parent(s): bdd397f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -35
app.py CHANGED
@@ -69,31 +69,34 @@ def create_deepseek_interface():
69
  if not serphouse_api_key:
70
  return "SERPHOUSE_API_KEYκ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€."
71
 
72
- # ν‚€μ›Œλ“œ μΆ”μΆœ
73
- extraction_result, search_query = extract_keywords_with_llm(query)
74
- print(f"원본 쿼리: {query}")
75
- print(extraction_result)
76
-
77
- url = "https://api.serphouse.com/serp/live"
78
- payload = {
79
- "q": search_query,
80
- "domain": "google.com",
81
- "loc": "us",
82
- "lang": "en",
83
- "device": "desktop",
84
- "serp_type": "web",
85
- "page": 1,
86
- "num": 5 # μƒμœ„ 5개 결과만 κ°€μ Έμ˜€κΈ°
87
- }
88
- headers = {
89
- "Content-Type": "application/json",
90
- "Authorization": f"Bearer {serphouse_api_key}"
91
- }
92
-
93
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  response = requests.post(url, headers=headers, json=payload)
95
  response.raise_for_status()
96
 
 
97
  search_results = response.json()
98
 
99
  # 검색 κ²°κ³Ό νŒŒμ‹± 및 ν¬λ§·νŒ…
@@ -112,7 +115,9 @@ def create_deepseek_interface():
112
  return f"검색어 '{search_query}'에 λŒ€ν•œ 검색 κ²°κ³Όκ°€ μ—†μŠ΅λ‹ˆλ‹€."
113
 
114
  except Exception as e:
115
- return f"검색 쀑 였λ₯˜ λ°œμƒ: {str(e)}"
 
 
116
 
117
  # 슀트리밍 λ°©μ‹μœΌλ‘œ DeepSeek API 호좜 ν•¨μˆ˜
118
  def query_deepseek_streaming(message, history, use_deep_research):
@@ -123,13 +128,17 @@ def create_deepseek_interface():
123
  search_context = ""
124
  search_info = ""
125
  if use_deep_research:
126
- # 검색 μˆ˜ν–‰ (첫 λ©”μ‹œμ§€ 전달)
127
- yield history + [(message, "πŸ” 졜적의 ν‚€μ›Œλ“œ μΆ”μΆœ 및 μ›Ή 검색 쀑...")], ""
128
-
129
- # 검색 μ‹€ν–‰
130
- search_results = search_with_serphouse(message)
131
- if not search_results.startswith("검색 쀑 였λ₯˜ λ°œμƒ") and not search_results.startswith("SERPHOUSE_API_KEY"):
132
- search_context = f"""
 
 
 
 
133
  λ‹€μŒμ€ μ‚¬μš©μž 질문과 κ΄€λ ¨λœ μ΅œμ‹  검색 κ²°κ³Όμž…λ‹ˆλ‹€. 이 정보λ₯Ό μ°Έκ³ ν•˜μ—¬ μ •ν™•ν•˜κ³  μ΅œμ‹  정보가 반영된 응닡을 μ œκ³΅ν•˜μ„Έμš”:
134
 
135
  {search_results}
@@ -137,7 +146,12 @@ def create_deepseek_interface():
137
  μœ„ 검색 κ²°κ³Όλ₯Ό 기반으둜 μ‚¬μš©μžμ˜ λ‹€μŒ μ§ˆλ¬Έμ— λ‹΅λ³€ν•˜μ„Έμš”. 검색 κ²°κ³Όμ—μ„œ λͺ…ν™•ν•œ 닡변을 찾을 수 μ—†λŠ” 경우, λ‹Ήμ‹ μ˜ 지식을 ν™œμš©ν•˜μ—¬ μ΅œμ„ μ˜ 닡변을 μ œκ³΅ν•˜μ„Έμš”.
138
  검색 κ²°κ³Όλ₯Ό μΈμš©ν•  λ•ŒλŠ” 좜처λ₯Ό λͺ…μ‹œν•˜κ³ , 닡변이 μ΅œμ‹  정보λ₯Ό λ°˜μ˜ν•˜λ„λ‘ ν•˜μ„Έμš”.
139
  """
140
- search_info = f"πŸ” Deep Research κΈ°λŠ₯ ν™œμ„±ν™”: κ΄€λ ¨ μ›Ή 검색 κ²°κ³Όλ₯Ό 기반으둜 응닡 생성 쀑..."
 
 
 
 
 
141
 
142
  # API μš”μ²­μ„ μœ„ν•œ λŒ€ν™” 기둝 μ€€λΉ„
143
  messages = []
@@ -243,12 +257,24 @@ def create_deepseek_interface():
243
  container=True
244
  )
245
 
246
- # Deep Research ν† κΈ€ μΆ”κ°€
247
  with gr.Row():
248
- use_deep_research = gr.Checkbox(
249
- label="Deep Research ν™œμ„±ν™”",
250
- info="졜적의 ν‚€μ›Œλ“œ μΆ”μΆœ 및 μ›Ή 검색을 ν†΅ν•œ μ΅œμ‹  정보 ν™œμš©"
251
- )
 
 
 
 
 
 
 
 
 
 
 
 
252
 
253
  # μž…λ ₯ μ˜μ—­
254
  with gr.Row():
 
69
  if not serphouse_api_key:
70
  return "SERPHOUSE_API_KEYκ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€."
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  try:
73
+ # ν‚€μ›Œλ“œ μΆ”μΆœ
74
+ extraction_result, search_query = extract_keywords_with_llm(query)
75
+ print(f"원본 쿼리: {query}")
76
+ print(extraction_result)
77
+
78
+ # SerpHouse API 호좜 μ‹€ν–‰
79
+ url = "https://api.serphouse.com/serp/live"
80
+ payload = {
81
+ "q": search_query,
82
+ "domain": "google.com",
83
+ "loc": "us",
84
+ "lang": "en",
85
+ "device": "desktop",
86
+ "serp_type": "web",
87
+ "page": 1,
88
+ "num": 5 # μƒμœ„ 5개 결과만 κ°€μ Έμ˜€κΈ°
89
+ }
90
+ headers = {
91
+ "Content-Type": "application/json",
92
+ "Authorization": f"Bearer {serphouse_api_key}"
93
+ }
94
+
95
+ print(f"SerpHouse API 호좜 쀑... 검색어: {search_query}")
96
  response = requests.post(url, headers=headers, json=payload)
97
  response.raise_for_status()
98
 
99
+ print("SerpHouse API 응닡 μˆ˜μ‹  μ™„λ£Œ")
100
  search_results = response.json()
101
 
102
  # 검색 κ²°κ³Ό νŒŒμ‹± 및 ν¬λ§·νŒ…
 
115
  return f"검색어 '{search_query}'에 λŒ€ν•œ 검색 κ²°κ³Όκ°€ μ—†μŠ΅λ‹ˆλ‹€."
116
 
117
  except Exception as e:
118
+ error_msg = f"검색 쀑 였λ₯˜ λ°œμƒ: {str(e)}"
119
+ print(error_msg)
120
+ return error_msg
121
 
122
  # 슀트리밍 λ°©μ‹μœΌλ‘œ DeepSeek API 호좜 ν•¨μˆ˜
123
  def query_deepseek_streaming(message, history, use_deep_research):
 
128
  search_context = ""
129
  search_info = ""
130
  if use_deep_research:
131
+ try:
132
+ # 검색 μˆ˜ν–‰ (첫 λ©”μ‹œμ§€ 전달)
133
+ yield history + [(message, "πŸ” 졜적의 ν‚€μ›Œλ“œ μΆ”μΆœ 및 μ›Ή 검색 쀑...")], ""
134
+
135
+ # 검색 μ‹€ν–‰ - 디버깅을 μœ„ν•œ 둜그 μΆ”κ°€
136
+ print(f"Deep Research ν™œμ„±ν™”λ¨: λ©”μ‹œμ§€ '{message}'에 λŒ€ν•œ 검색 μ‹œμž‘")
137
+ search_results = search_with_serphouse(message)
138
+ print(f"검색 κ²°κ³Ό μˆ˜μ‹  μ™„λ£Œ: {search_results[:100]}...") # κ²°κ³Ό μ•žλΆ€λΆ„λ§Œ 좜λ ₯
139
+
140
+ if not search_results.startswith("검색 쀑 였λ₯˜ λ°œμƒ") and not search_results.startswith("SERPHOUSE_API_KEY"):
141
+ search_context = f"""
142
  λ‹€μŒμ€ μ‚¬μš©μž 질문과 κ΄€λ ¨λœ μ΅œμ‹  검색 κ²°κ³Όμž…λ‹ˆλ‹€. 이 정보λ₯Ό μ°Έκ³ ν•˜μ—¬ μ •ν™•ν•˜κ³  μ΅œμ‹  정보가 반영된 응닡을 μ œκ³΅ν•˜μ„Έμš”:
143
 
144
  {search_results}
 
146
  μœ„ 검색 κ²°κ³Όλ₯Ό 기반으둜 μ‚¬μš©μžμ˜ λ‹€μŒ μ§ˆλ¬Έμ— λ‹΅λ³€ν•˜μ„Έμš”. 검색 κ²°κ³Όμ—μ„œ λͺ…ν™•ν•œ 닡변을 찾을 수 μ—†λŠ” 경우, λ‹Ήμ‹ μ˜ 지식을 ν™œμš©ν•˜μ—¬ μ΅œμ„ μ˜ 닡변을 μ œκ³΅ν•˜μ„Έμš”.
147
  검색 κ²°κ³Όλ₯Ό μΈμš©ν•  λ•ŒλŠ” 좜처λ₯Ό λͺ…μ‹œν•˜κ³ , 닡변이 μ΅œμ‹  정보λ₯Ό λ°˜μ˜ν•˜λ„λ‘ ν•˜μ„Έμš”.
148
  """
149
+ search_info = f"πŸ” Deep Research κΈ°λŠ₯ ν™œμ„±ν™”: κ΄€λ ¨ μ›Ή 검색 κ²°κ³Όλ₯Ό 기반으둜 응닡 생성 쀑..."
150
+ else:
151
+ print(f"검색 μ‹€νŒ¨ λ˜λŠ” κ²°κ³Ό μ—†μŒ: {search_results}")
152
+ except Exception as e:
153
+ print(f"Deep Research 처리 쀑 μ˜ˆμ™Έ λ°œμƒ: {str(e)}")
154
+ search_info = f"πŸ” Deep Research κΈ°λŠ₯ 였λ₯˜: {str(e)}"
155
 
156
  # API μš”μ²­μ„ μœ„ν•œ λŒ€ν™” 기둝 μ€€λΉ„
157
  messages = []
 
257
  container=True
258
  )
259
 
260
+ # Deep Research ν† κΈ€ 및 μƒνƒœ ν‘œμ‹œ μΆ”κ°€
261
  with gr.Row():
262
+ with gr.Column(scale=3):
263
+ use_deep_research = gr.Checkbox(
264
+ label="Deep Research ν™œμ„±ν™”",
265
+ info="졜적의 ν‚€μ›Œλ“œ μΆ”μΆœ 및 μ›Ή 검색을 ν†΅ν•œ μ΅œμ‹  정보 ν™œμš©",
266
+ value=False
267
+ )
268
+ with gr.Column(scale=1):
269
+ api_status = gr.Markdown("API μƒνƒœ: 쀀비됨")
270
+
271
+ # API ν‚€ μƒνƒœ 확인 및 ν‘œμ‹œ
272
+ if not serphouse_api_key:
273
+ api_status.value = "⚠️ SERPHOUSE_API_KEYκ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€"
274
+ if not api_key:
275
+ api_status.value = "⚠️ FW_API_KEYκ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€"
276
+ if api_key and serphouse_api_key:
277
+ api_status.value = "βœ… API ν‚€ μ„€μ • μ™„λ£Œ"
278
 
279
  # μž…λ ₯ μ˜μ—­
280
  with gr.Row():