File size: 8,539 Bytes
2344ef4
 
 
 
 
 
6e055ee
2344ef4
 
 
 
 
9bf8481
2344ef4
 
29eefff
 
 
2344ef4
29eefff
 
2344ef4
 
 
29eefff
 
6e055ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2344ef4
 
 
 
 
29eefff
 
2344ef4
 
 
 
 
 
 
 
29eefff
 
2344ef4
 
 
4857a89
2344ef4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48bb067
 
2344ef4
48bb067
2344ef4
48bb067
2344ef4
48bb067
2344ef4
48bb067
 
2344ef4
e3cd84d
2344ef4
 
 
 
16d0938
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bf8481
6e055ee
441caa0
16d0938
 
9bf8481
16d0938
9bf8481
 
 
16d0938
9bf8481
 
 
16d0938
 
 
 
6e055ee
16d0938
 
 
 
 
 
 
 
48bb067
 
16d0938
48bb067
16d0938
48bb067
16d0938
48bb067
16d0938
48bb067
 
16d0938
 
 
 
 
2344ef4
 
 
 
 
 
 
 
 
 
 
 
 
77bd856
9bf8481
 
53de8d7
6e055ee
499c452
441caa0
53de8d7
16d0938
2344ef4
 
 
 
 
 
 
6e055ee
 
 
 
e3cd84d
6e055ee
 
 
 
 
2344ef4
 
6e055ee
 
 
 
4857a89
6e055ee
 
 
4857a89
6e055ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2344ef4
 
6e055ee
 
2344ef4
6e055ee
29eefff
 
 
2344ef4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# app.py
# Naver News Search API -> Gradio chat-like UI (HF Spaces friendly)
#
# HF Spaces Secrets μ„€μ •:
#   NAVER_CLIENT_ID     = λ°œκΈ‰λ°›μ€ Client ID
#   NAVER_CLIENT_SECRET = λ°œκΈ‰λ°›μ€ Client Secret
#   ALLOWED_IPS         = 123.123.123.123,2406:xxxx:xxxx::1234

import os
import html
import re
from datetime import datetime
from typing import Dict, Any, List, Tuple

import requests
import gradio as gr


NAVER_NEWS_ENDPOINT = "https://openapi.naver.com/v1/search/news.json"


def _get_env(name: str) -> str:
    v = os.getenv(name, "").strip()
    return v


# βœ… ν—ˆμš© IP λͺ©λ‘ λ‘œλ“œ
raw_ips = _get_env("ALLOWED_IPS")
ALLOWED_IPS = [ip.strip() for ip in raw_ips.split(",") if ip.strip()]


# βœ… ν”„λ‘μ‹œ ν™˜κ²½ κ³ λ €: μ‹€μ œ ν΄λΌμ΄μ–ΈνŠΈ IP μΆ”μΆœ
def _get_client_ip(request: gr.Request) -> str:
    xff = request.headers.get("x-forwarded-for")
    if xff:
        return xff.split(",")[0].strip()

    xri = request.headers.get("x-real-ip")
    if xri:
        return xri.strip()

    return request.client.host


# βœ… IP μ ‘κ·Ό 차단
def gatekeeper(request: gr.Request):
    if not ALLOWED_IPS:
        return gr.update(visible=True), ""

    client_ip = _get_client_ip(request)

    if client_ip not in ALLOWED_IPS:
        return (
            gr.update(visible=False),
            f"β›” 접근이 ν—ˆμš©λ˜μ§€ μ•Šμ€ IPμž…λ‹ˆλ‹€. (ν˜„μž¬ IP: {client_ip})",
        )

    return gr.update(visible=True), ""


def _strip_tags(text: str) -> str:
    if not text:
        return ""
    text = re.sub(r"<[^>]+>", "", text)
    return html.unescape(text).strip()


def _format_pubdate(pub_date: str) -> str:
    if not pub_date:
        return ""
    try:
        dt = datetime.strptime(pub_date, "%a, %d %b %Y %H:%M:%S %z")
        return dt.strftime("%Y-%m-%d %H:%M:%S %z")
    except Exception:
        return pub_date


def naver_news_search(
    query: str,
    display: int = 10,
    sort: str = "sim",
    start: int = 1,
    timeout: int = 10,
) -> Dict[str, Any]:
    client_id = _get_env("NAVER_CLIENT_ID")
    client_secret = _get_env("NAVER_CLIENT_SECRET")

    if not client_id or not client_secret:
        raise RuntimeError(
            "ν™˜κ²½λ³€μˆ˜ NAVER_CLIENT_ID / NAVER_CLIENT_SECRET 이 μ„€μ •λ˜μ–΄ μžˆμ§€ μ•ŠμŠ΅λ‹ˆλ‹€. "
            "Hugging Face Spaces의 Secrets에 μΆ”κ°€ν•΄ μ£Όμ„Έμš”."
        )

    headers = {
        "X-Naver-Client-Id": client_id,
        "X-Naver-Client-Secret": client_secret,
    }

    params = {
        "query": query,
        "display": int(display),
        "start": int(start),
        "sort": sort,
    }

    r = requests.get(
        NAVER_NEWS_ENDPOINT,
        headers=headers,
        params=params,
        timeout=timeout,
    )

    if r.status_code != 200:
        try:
            detail = r.json()
        except Exception:
            detail = {"text": r.text}
        raise RuntimeError(f"넀이버 API 호좜 μ‹€νŒ¨ (HTTP {r.status_code}): {detail}")

    return r.json()


def render_results(data: Dict[str, Any], max_items: int = 10) -> str:
    items = data.get("items", [])[:max_items]
    total = data.get("total", None)

    lines: List[str] = []
    if total is not None:
        lines.append(f"- 총 검색결과: {total:,}건")
    lines.append(f"- λ°˜ν™˜ 개수: {len(items)}건")
    lines.append("")

    for i, it in enumerate(items, start=1):
        title = _strip_tags(it.get("title", ""))
        desc = _strip_tags(it.get("description", ""))
        link = it.get("link", "")
        origin = it.get("originallink", "")
        pub = _format_pubdate(it.get("pubDate", ""))

        lines.append(f"{i}. **{title}**")

        if pub:
            lines.append(f"    - λ°œν–‰: {pub}")
        if origin:
            lines.append(f"    - 원문: {origin}")
        if link:
            lines.append(f"    - 링크: {link}")
        if desc:
            lines.append(f"    - μš”μ•½: {desc}")

        lines.append("")
        lines.append("")

    return "\n".join(lines).strip()


def dedup_items(all_items: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
    seen = set()
    out = []
    for it in all_items:
        origin = (it.get("originallink") or "").strip()
        link = (it.get("link") or "").strip()
        title = _strip_tags(it.get("title", ""))
        pub = it.get("pubDate", "")

        key = origin or link or f"{title}|{pub}"
        if not key:
            continue
        if key in seen:
            continue
        seen.add(key)
        out.append(it)
    return out


def aggregate_search(
    sentence: str,
    display: int,
    sort: str,
) -> Tuple[List[str], List[Dict[str, Any]], int]:

    queries = [sentence]

    all_items: List[Dict[str, Any]] = []
    total: int = 0

    data = naver_news_search(query=sentence, display=int(display), sort=sort, start=1)
    total = int(data.get("total", 0) or 0)
    all_items.extend(data.get("items", []))

    merged = dedup_items(all_items)
    final_items = merged[:display]
    return queries, final_items, total


def render_results_from_items(items: List[Dict[str, Any]]) -> str:
    lines: List[str] = []
    lines.append("")

    for i, it in enumerate(items, start=1):
        title = _strip_tags(it.get("title", ""))
        desc = _strip_tags(it.get("description", ""))
        link = it.get("link", "")
        origin = it.get("originallink", "")
        pub = _format_pubdate(it.get("pubDate", ""))

        lines.append(f"{i}. **{title}**")

        if pub:
            lines.append(f"    - λ°œν–‰: {pub}")
        if origin:
            lines.append(f"    - 원문: {origin}")
        if link:
            lines.append(f"    - 링크: {link}")
        if desc:
            lines.append(f"    - μš”μ•½: {desc}")

        lines.append("")

    return "\n".join(lines).strip()


def handle_search(
    user_query: str,
    chat_history: List[Dict[str, str]],
    display: int,
    sort: str,
) -> Tuple[List[Dict[str, str]], str]:
    q = (user_query or "").strip()
    if not q:
        return chat_history, ""

    chat_history = chat_history + [{"role": "user", "content": q}]

    try:
        _, items, total = aggregate_search(sentence=q, display=int(display), sort=sort)

        lines: List[str] = []
        lines.append("")
        lines.append(render_results_from_items(items))
        lines.append("")

        assistant_text = "\n".join(lines).strip()

    except Exception as e:
        assistant_text = f"였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.\n\n- {e}"

    chat_history = chat_history + [{"role": "assistant", "content": assistant_text}]
    return chat_history, ""


with gr.Blocks(title="Naver News Search (Chat UI)") as demo:

    warning = gr.Markdown("")
    main_ui = gr.Column(visible=True)

    # βœ… νŽ˜μ΄μ§€ λ‘œλ“œμ‹œ IP 검사
    demo.load(
        fn=gatekeeper,
        inputs=None,
        outputs=[main_ui, warning],
    )

    with main_ui:
        with gr.Accordion("검색 옡μ…₯ - λ‰΄μŠ€ κ°œμˆ˜μ™€ μ •λ ¬ 방식을 μ„ νƒν•˜μ„Έμš”.", open=False):
            with gr.Row():
                display = gr.Slider(
                    minimum=1, maximum=100, value=10, step=1, label="λ‰΄μŠ€ 개수"
                )
                sort = gr.Dropdown(
                    choices=[("μ΅œμ‹ μˆœ", "date"), ("μ •ν™•λ„μˆœ(μ—°κ΄€λ„μˆœ)", "sim")],
                    value="sim",
                    label="μ •λ ¬ 방식",
                )

        chatbot = gr.Chatbot(
            value=[],
            label="NewsChat_v0.1",
            type="messages",
            height=600,
        )

        with gr.Row():
            query_in = gr.Textbox(
                label="검색어λ₯Ό μž…λ ₯ν•˜μ„Έμš”.",
                placeholder="(예: ν–‰μ•ˆλΆ€μ˜ 인곡지λŠ₯ 사업)",
                scale=8,
            )
            with gr.Column(scale=2):
                search_btn = gr.Button("검색", variant="primary")
                clear_btn = gr.Button("λŒ€ν™” μ§€μš°κΈ°", variant="secondary")

        search_btn.click(
            fn=handle_search,
            inputs=[query_in, chatbot, display, sort],
            outputs=[chatbot, query_in],
            api_name="search",
        )
        query_in.submit(
            fn=handle_search,
            inputs=[query_in, chatbot, display, sort],
            outputs=[chatbot, query_in],
            api_name="search_submit",
        )

        def clear_chat():
            return []

        clear_btn.click(fn=clear_chat, inputs=[], outputs=[chatbot], api_name="clear")


if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)