File size: 16,854 Bytes
2284074
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import gradio as gr
import os
import requests
import json
import time
from dotenv import load_dotenv

# .env ํŒŒ์ผ ๋กœ๋“œ (์žˆ๋Š” ๊ฒฝ์šฐ)
load_dotenv()

def create_deepseek_interface():
    # ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ API ํ‚ค ๊ฐ€์ ธ์˜ค๊ธฐ
    api_key = os.getenv("FW_API_KEY")
    serphouse_api_key = os.getenv("SERPHOUSE_API_KEY")
    
    if not api_key:
        print("๊ฒฝ๊ณ : FW_API_KEY ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
    if not serphouse_api_key:
        print("๊ฒฝ๊ณ : SERPHOUSE_API_KEY ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
    
    # ํ‚ค์›Œ๋“œ ์ถ”์ถœ ํ•จ์ˆ˜ (LLM ๊ธฐ๋ฐ˜)
    def extract_keywords_with_llm(query):
        if not api_key:
            return "LLM ํ‚ค์›Œ๋“œ ์ถ”์ถœ์„ ์œ„ํ•œ FW_API_KEY๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.", query
        
        # LLM์„ ์‚ฌ์šฉํ•˜์—ฌ ํ‚ค์›Œ๋“œ ์ถ”์ถœ (DeepSeek ๋ชจ๋ธ ์‚ฌ์šฉ)
        url = "https://api.fireworks.ai/inference/v1/chat/completions"
        payload = {
            "model": "accounts/fireworks/models/deepseek-v3-0324",
            "max_tokens": 200,
            "temperature": 0.1,  # ์ผ๊ด€๋œ ๊ฒฐ๊ณผ๋ฅผ ์œ„ํ•ด ๋‚ฎ์€ ์˜จ๋„ ์‚ฌ์šฉ
            "messages": [
                {
                    "role": "system",
                    "content": "์‚ฌ์šฉ์ž์˜ ์งˆ๋ฌธ์—์„œ ์›น ๊ฒ€์ƒ‰์— ํšจ๊ณผ์ ์ธ ํ•ต์‹ฌ ํ‚ค์›Œ๋“œ๋ฅผ ์ถ”์ถœํ•˜์„ธ์š”. ํ‚ค์›Œ๋“œ ์‚ฌ์ด์— ์‰ผํ‘œ๋‚˜ ๊ณต๋ฐฑ์„ ๋„ฃ์ง€ ๋ง๊ณ  ํ•˜๋‚˜์˜ ๊ฒ€์ƒ‰์–ด์ฒ˜๋Ÿผ ์ˆ˜์ •ํ•ด์„œ ์ œ๊ณตํ•˜์„ธ์š”. ์˜ˆ๋ฅผ ๋“ค์–ด 'ํ•œ๋•์ˆ˜ ๊ตญ๋ฌด์ด๋ฆฌ ํƒ„ํ•ต ๊ฒฐ๊ณผ'์ฒ˜๋Ÿผ ๊ณต๋ฐฑ์œผ๋กœ๋งŒ ๊ตฌ๋ถ„ํ•˜์„ธ์š”."
                },
                {
                    "role": "user",
                    "content": query
                }
            ]
        }
        headers = {
            "Accept": "application/json",
            "Content-Type": "application/json",
            "Authorization": f"Bearer {api_key}"
        }
        
        try:
            response = requests.post(url, headers=headers, json=payload)
            response.raise_for_status()
            result = response.json()
            
            # ์‘๋‹ต์—์„œ ํ‚ค์›Œ๋“œ ์ถ”์ถœ
            keywords = result["choices"][0]["message"]["content"].strip()
            
            # ํ‚ค์›Œ๋“œ๊ฐ€ ๋„ˆ๋ฌด ๊ธธ๊ฑฐ๋‚˜ ํ˜•์‹์ด ์ž˜๋ชป๋œ ๊ฒฝ์šฐ ์›๋ณธ ์ฟผ๋ฆฌ ์‚ฌ์šฉ
            if len(keywords) > 100:
                return f"์ถ”์ถœ๋œ ํ‚ค์›Œ๋“œ: {keywords}", query
                
            return f"์ถ”์ถœ๋œ ํ‚ค์›Œ๋“œ: {keywords}", keywords
            
        except Exception as e:
            print(f"ํ‚ค์›Œ๋“œ ์ถ”์ถœ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}")
            return f"ํ‚ค์›Œ๋“œ ์ถ”์ถœ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}", query
    
    # SerpHouse API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ƒ‰ ์ˆ˜ํ–‰ ํ•จ์ˆ˜
    def search_with_serphouse(query):
        if not serphouse_api_key:
            return "SERPHOUSE_API_KEY๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค."
        
        try:
            # ํ‚ค์›Œ๋“œ ์ถ”์ถœ
            extraction_result, search_query = extract_keywords_with_llm(query)
            print(f"์›๋ณธ ์ฟผ๋ฆฌ: {query}")
            print(extraction_result)
            
            # ๋ฌธ์„œ ์ฝ”๋“œ๋ฅผ ๋” ์ž์„ธํžˆ ๋ถ„์„ํ•ด ๋ณด๋‹ˆ ๊ธฐ๋ณธ GET ๋ฐฉ์‹ ํ™œ์šฉ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
            url = "https://api.serphouse.com/serp/live"
            
            # ํ•œ๊ธ€ ๊ฒ€์ƒ‰์–ด์ธ์ง€ ํ™•์ธ
            is_korean = any('\uAC00' <= c <= '\uD7A3' for c in search_query)
            
            # ๊ฐ„์†Œํ™”๋œ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์‹œ๋„
            params = {
                "q": search_query,
                "domain": "google.com",
                "serp_type": "web",  # ๊ธฐ๋ณธ ์›น ๊ฒ€์ƒ‰์œผ๋กœ ๋ณ€๊ฒฝ
                "device": "desktop",
                "lang": "ko" if is_korean else "en"
            }
            
            headers = {
                "Authorization": f"Bearer {serphouse_api_key}"
            }
            
            print(f"SerpHouse API ํ˜ธ์ถœ ์ค‘... ๊ธฐ๋ณธ GET ๋ฐฉ์‹์œผ๋กœ ์‹œ๋„")
            print(f"๊ฒ€์ƒ‰์–ด: {search_query}")
            print(f"์š”์ฒญ URL: {url} - ํŒŒ๋ผ๋ฏธํ„ฐ: {params}")
            
            # GET ์š”์ฒญ ์ˆ˜ํ–‰
            response = requests.get(url, headers=headers, params=params)
            response.raise_for_status()
            
            print(f"SerpHouse API ์‘๋‹ต ์ƒํƒœ ์ฝ”๋“œ: {response.status_code}")
            search_results = response.json()
            
            # ์‘๋‹ต ๊ตฌ์กฐ ํ™•์ธ
            print(f"์‘๋‹ต ๊ตฌ์กฐ: {list(search_results.keys()) if isinstance(search_results, dict) else '๋”•์…”๋„ˆ๋ฆฌ ์•„๋‹˜'}")
            
            # ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ํŒŒ์‹ฑ ๋ฐ ํฌ๋งทํŒ…
            formatted_results = []
            formatted_results.append(f"๊ฒ€์ƒ‰์–ด: {search_query}\n\n")
            
            # ๋‹ค์–‘ํ•œ ๊ฐ€๋Šฅํ•œ ์‘๋‹ต ๊ตฌ์กฐ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ
            organic_results = None
            
            # ๊ฐ€๋Šฅํ•œ ์‘๋‹ต ๊ตฌ์กฐ 1
            if "results" in search_results and "organic" in search_results["results"]:
                organic_results = search_results["results"]["organic"]
                
            # ๊ฐ€๋Šฅํ•œ ์‘๋‹ต ๊ตฌ์กฐ 2
            elif "organic" in search_results:
                organic_results = search_results["organic"]
                
            # ๊ฐ€๋Šฅํ•œ ์‘๋‹ต ๊ตฌ์กฐ 3 (์ค‘์ฒฉ๋œ results)
            elif "results" in search_results and "results" in search_results["results"]:
                if "organic" in search_results["results"]["results"]:
                    organic_results = search_results["results"]["results"]["organic"]
            
            # organic_results๊ฐ€ ์žˆ์œผ๋ฉด ์ฒ˜๋ฆฌ
            if organic_results and len(organic_results) > 0:
                # ์‘๋‹ต ๊ตฌ์กฐ ์ถœ๋ ฅ
                print(f"์ฒซ๋ฒˆ์งธ organic ๊ฒฐ๊ณผ ๊ตฌ์กฐ: {organic_results[0].keys() if len(organic_results) > 0 else 'empty'}")
                
                for result in organic_results[:5]:  # ์ƒ์œ„ 5๊ฐœ ๊ฒฐ๊ณผ๋งŒ ํ‘œ์‹œ
                    title = result.get("title", "์ œ๋ชฉ ์—†์Œ")
                    snippet = result.get("snippet", "๋‚ด์šฉ ์—†์Œ")
                    link = result.get("link", "#")
                    
                    formatted_results.append(
                        f"์ œ๋ชฉ: {title}\n"
                        f"๋‚ด์šฉ: {snippet}\n"
                        f"๋งํฌ: {link}\n\n"
                    )
                
                print(f"๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ {len(organic_results)}๊ฐœ ์ฐพ์Œ")
                return "".join(formatted_results)
            
            # ๊ฒฐ๊ณผ๊ฐ€ ์—†๊ฑฐ๋‚˜ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๊ตฌ์กฐ์ธ ๊ฒฝ์šฐ
            print("๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ์—†์Œ ๋˜๋Š” ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์‘๋‹ต ๊ตฌ์กฐ")
            print(f"์‘๋‹ต ๊ตฌ์กฐ ์ƒ์„ธ: {search_results.keys() if hasattr(search_results, 'keys') else '๋ถˆ๋ช…ํ™•ํ•œ ๊ตฌ์กฐ'}")
            
            # ์‘๋‹ต ๋‚ด์šฉ์—์„œ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ์ฐพ๊ธฐ
            error_msg = "๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†๊ฑฐ๋‚˜ ์‘๋‹ต ํ˜•์‹์ด ์˜ˆ์ƒ๊ณผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค"
            if "error" in search_results:
                error_msg = search_results["error"]
            elif "message" in search_results:
                error_msg = search_results["message"]
                
            return f"๊ฒ€์ƒ‰์–ด '{search_query}'์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ: {error_msg}"
            
        except Exception as e:
            error_msg = f"๊ฒ€์ƒ‰ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
            print(error_msg)
            import traceback
            print(traceback.format_exc())
            
            # ๋””๋ฒ„๊น… ๋ชฉ์ ์œผ๋กœ API ์š”์ฒญ ์ƒ์„ธ ์ •๋ณด ์ถ”๊ฐ€
            return f"๊ฒ€์ƒ‰ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}\n\n" + \
                   f"API ์š”์ฒญ ์ƒ์„ธ ์ •๋ณด:\n" + \
                   f"- URL: {url}\n" + \
                   f"- ๊ฒ€์ƒ‰์–ด: {search_query}\n" + \
                   f"- ํŒŒ๋ผ๋ฏธํ„ฐ: {params}\n"
    
    # ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐฉ์‹์œผ๋กœ DeepSeek API ํ˜ธ์ถœ ํ•จ์ˆ˜
    def query_deepseek_streaming(message, history, use_deep_research):
        if not api_key:
            yield history, "ํ™˜๊ฒฝ ๋ณ€์ˆ˜ FW_API_KEY๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์„œ๋ฒ„์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”."
            return
        
        search_context = ""
        search_info = ""
        if use_deep_research:
            try:
                # ๊ฒ€์ƒ‰ ์ˆ˜ํ–‰ (์ฒซ ๋ฉ”์‹œ์ง€ ์ „๋‹ฌ)
                yield history + [(message, "๐Ÿ” ์ตœ์ ์˜ ํ‚ค์›Œ๋“œ ์ถ”์ถœ ๋ฐ ์›น ๊ฒ€์ƒ‰ ์ค‘...")], ""
                
                # ๊ฒ€์ƒ‰ ์‹คํ–‰ - ๋””๋ฒ„๊น…์„ ์œ„ํ•œ ๋กœ๊ทธ ์ถ”๊ฐ€
                print(f"Deep Research ํ™œ์„ฑํ™”๋จ: ๋ฉ”์‹œ์ง€ '{message}'์— ๋Œ€ํ•œ ๊ฒ€์ƒ‰ ์‹œ์ž‘")
                search_results = search_with_serphouse(message)
                print(f"๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ์ˆ˜์‹  ์™„๋ฃŒ: {search_results[:100]}...")  # ๊ฒฐ๊ณผ ์•ž๋ถ€๋ถ„๋งŒ ์ถœ๋ ฅ
                
                if not search_results.startswith("๊ฒ€์ƒ‰ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ") and not search_results.startswith("SERPHOUSE_API_KEY"):
                    search_context = f"""
๋‹ค์Œ์€ ์‚ฌ์šฉ์ž ์งˆ๋ฌธ๊ณผ ๊ด€๋ จ๋œ ์ตœ์‹  ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. ์ด ์ •๋ณด๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ •ํ™•ํ•˜๊ณ  ์ตœ์‹  ์ •๋ณด๊ฐ€ ๋ฐ˜์˜๋œ ์‘๋‹ต์„ ์ œ๊ณตํ•˜์„ธ์š”:
{search_results}
์œ„ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์‚ฌ์šฉ์ž์˜ ๋‹ค์Œ ์งˆ๋ฌธ์— ๋‹ต๋ณ€ํ•˜์„ธ์š”. ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์—์„œ ๋ช…ํ™•ํ•œ ๋‹ต๋ณ€์„ ์ฐพ์„ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ, ๋‹น์‹ ์˜ ์ง€์‹์„ ํ™œ์šฉํ•˜์—ฌ ์ตœ์„ ์˜ ๋‹ต๋ณ€์„ ์ œ๊ณตํ•˜์„ธ์š”.
๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ์ธ์šฉํ•  ๋•Œ๋Š” ์ถœ์ฒ˜๋ฅผ ๋ช…์‹œํ•˜๊ณ , ๋‹ต๋ณ€์ด ์ตœ์‹  ์ •๋ณด๋ฅผ ๋ฐ˜์˜ํ•˜๋„๋ก ํ•˜์„ธ์š”.
"""
                    search_info = f"๐Ÿ” Deep Research ๊ธฐ๋Šฅ ํ™œ์„ฑํ™”: ๊ด€๋ จ ์›น ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์‘๋‹ต ์ƒ์„ฑ ์ค‘..."
                else:
                    print(f"๊ฒ€์ƒ‰ ์‹คํŒจ ๋˜๋Š” ๊ฒฐ๊ณผ ์—†์Œ: {search_results}")
            except Exception as e:
                print(f"Deep Research ์ฒ˜๋ฆฌ ์ค‘ ์˜ˆ์™ธ ๋ฐœ์ƒ: {str(e)}")
                search_info = f"๐Ÿ” Deep Research ๊ธฐ๋Šฅ ์˜ค๋ฅ˜: {str(e)}"
        
        # API ์š”์ฒญ์„ ์œ„ํ•œ ๋Œ€ํ™” ๊ธฐ๋ก ์ค€๋น„
        messages = []
        for user, assistant in history:
            messages.append({"role": "user", "content": user})
            messages.append({"role": "assistant", "content": assistant})
        
        # ๊ฒ€์ƒ‰ ์ปจํ…์ŠคํŠธ๊ฐ€ ์žˆ์œผ๋ฉด ์‹œ์Šคํ…œ ๋ฉ”์‹œ์ง€ ์ถ”๊ฐ€
        if search_context:
            # DeepSeek ๋ชจ๋ธ์€ ์‹œ์Šคํ…œ ๋ฉ”์‹œ์ง€๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค
            messages.insert(0, {"role": "system", "content": search_context})
        
        # ์ƒˆ ์‚ฌ์šฉ์ž ๋ฉ”์‹œ์ง€ ์ถ”๊ฐ€
        messages.append({"role": "user", "content": message})
        
        # API ์š”์ฒญ ์ค€๋น„
        url = "https://api.fireworks.ai/inference/v1/chat/completions"
        payload = {
            "model": "accounts/fireworks/models/deepseek-v3-0324",
            "max_tokens": 20480,
            "top_p": 1,
            "top_k": 40,
            "presence_penalty": 0,
            "frequency_penalty": 0,
            "temperature": 0.6,
            "messages": messages,
            "stream": True  # ์ŠคํŠธ๋ฆฌ๋ฐ ํ™œ์„ฑํ™”
        }
        headers = {
            "Accept": "application/json",
            "Content-Type": "application/json",
            "Authorization": f"Bearer {api_key}"
        }
        
        try:
            # ์ŠคํŠธ๋ฆฌ๋ฐ ์‘๋‹ต ์š”์ฒญ
            response = requests.request("POST", url, headers=headers, data=json.dumps(payload), stream=True)
            response.raise_for_status()  # HTTP ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ ์˜ˆ์™ธ ๋ฐœ์ƒ
            
            # ๋ฉ”์‹œ์ง€๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์ดˆ๊ธฐ ์‘๋‹ต์œผ๋กœ ์‹œ์ž‘
            new_history = history.copy()
            
            # search_info๊ฐ€ ์žˆ์œผ๋ฉด ์‹œ์ž‘ ๋ฉ”์‹œ์ง€์— ํฌํ•จ
            start_msg = search_info if search_info else ""
            new_history.append((message, start_msg))
            
            # ์‘๋‹ต ์ „์ฒด ํ…์ŠคํŠธ
            full_response = start_msg
            
            # ์ŠคํŠธ๋ฆฌ๋ฐ ์‘๋‹ต ์ฒ˜๋ฆฌ
            for line in response.iter_lines():
                if line:
                    line_text = line.decode('utf-8')
                    
                    # 'data: ' ์ ‘๋‘์‚ฌ ์ œ๊ฑฐ
                    if line_text.startswith("data: "):
                        line_text = line_text[6:]
                    
                    # ์ŠคํŠธ๋ฆผ ์ข…๋ฃŒ ๋ฉ”์‹œ์ง€ ํ™•์ธ
                    if line_text == "[DONE]":
                        break
                    
                    try:
                        # JSON ํŒŒ์‹ฑ
                        chunk = json.loads(line_text)
                        chunk_content = chunk.get("choices", [{}])[0].get("delta", {}).get("content", "")
                        
                        if chunk_content:
                            full_response += chunk_content
                            # ์ฑ„ํŒ… ๊ธฐ๋ก ์—…๋ฐ์ดํŠธ
                            new_history[-1] = (message, full_response)
                            yield new_history, ""
                    except json.JSONDecodeError:
                        continue
            
            # ์ตœ์ข… ์‘๋‹ต ๋ฐ˜ํ™˜
            yield new_history, ""
            
        except requests.exceptions.RequestException as e:
            error_msg = f"API ์˜ค๋ฅ˜: {str(e)}"
            if hasattr(e, 'response') and e.response and e.response.status_code == 401:
                error_msg = "์ธ์ฆ ์‹คํŒจ. ํ™˜๊ฒฝ ๋ณ€์ˆ˜ FW_API_KEY๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”."
            yield history, error_msg
    
    # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
    with gr.Blocks(theme="soft", fill_height=True) as demo:
        # ํ—ค๋” ์„น์…˜
        gr.Markdown(
            """
            # ๐Ÿค– DeepSeek V3 ์ŠคํŠธ๋ฆฌ๋ฐ ์ธํ„ฐํŽ˜์ด์Šค
            ### Fireworks AI๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ณ ๊ธ‰ AI ๋ชจ๋ธ - ์‹ค์‹œ๊ฐ„ ์‘๋‹ต ์ง€์›
            """
        )
        
        # ๋ฉ”์ธ ๋ ˆ์ด์•„์›ƒ
        with gr.Row():
            # ๋ฉ”์ธ ์ฝ˜ํ…์ธ  ์˜์—ญ
            with gr.Column():
                # ์ฑ„ํŒ… ์ธํ„ฐํŽ˜์ด์Šค
                chatbot = gr.Chatbot(
                    height=500,
                    show_label=False,
                    container=True
                )
                
                # Deep Research ํ† ๊ธ€ ๋ฐ ์ƒํƒœ ํ‘œ์‹œ ์ถ”๊ฐ€
                with gr.Row():
                    with gr.Column(scale=3):
                        use_deep_research = gr.Checkbox(
                            label="Deep Research ํ™œ์„ฑํ™”",
                            info="์ตœ์ ์˜ ํ‚ค์›Œ๋“œ ์ถ”์ถœ ๋ฐ ์›น ๊ฒ€์ƒ‰์„ ํ†ตํ•œ ์ตœ์‹  ์ •๋ณด ํ™œ์šฉ",
                            value=False
                        )
                    with gr.Column(scale=1):
                        api_status = gr.Markdown("API ์ƒํƒœ: ์ค€๋น„๋จ")
                
                # API ํ‚ค ์ƒํƒœ ํ™•์ธ ๋ฐ ํ‘œ์‹œ
                if not serphouse_api_key:
                    api_status.value = "โš ๏ธ SERPHOUSE_API_KEY๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค"
                if not api_key:
                    api_status.value = "โš ๏ธ FW_API_KEY๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค"
                if api_key and serphouse_api_key:
                    api_status.value = "โœ… API ํ‚ค ์„ค์ • ์™„๋ฃŒ"
                
                # ์ž…๋ ฅ ์˜์—ญ
                with gr.Row():
                    msg = gr.Textbox(
                        label="๋ฉ”์‹œ์ง€",
                        placeholder="์—ฌ๊ธฐ์— ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”...",
                        show_label=False,
                        scale=9
                    )
                    submit = gr.Button("์ „์†ก", variant="primary", scale=1)
                
                # ๋Œ€ํ™” ์ดˆ๊ธฐํ™” ๋ฒ„ํŠผ
                with gr.Row():
                    clear = gr.ClearButton([msg, chatbot], value="๐Ÿงน ๋Œ€ํ™” ์ดˆ๊ธฐํ™”")
                    
                # ์˜ˆ์ œ ์ฟผ๋ฆฌ
                gr.Examples(
                    examples=[
                        "๋”ฅ๋Ÿฌ๋‹์—์„œ ํŠธ๋žœ์Šคํฌ๋จธ์™€ RNN์˜ ์ฐจ์ด์ ์„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.",
                        "ํŠน์ • ๋ฒ”์œ„ ๋‚ด์˜ ์†Œ์ˆ˜๋ฅผ ์ฐพ๋Š” ํŒŒ์ด์ฌ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.",
                        "๊ฐ•ํ™”ํ•™์Šต์˜ ์ฃผ์š” ๊ฐœ๋…์„ ์š”์•ฝํ•ด์ฃผ์„ธ์š”."
                    ],
                    inputs=msg
                )
                
                # ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ํ‘œ์‹œ
                error_box = gr.Markdown("")
        
        # ๋ฒ„ํŠผ๊ณผ ๊ธฐ๋Šฅ ์—ฐ๊ฒฐ
        submit.click(
            query_deepseek_streaming,
            inputs=[msg, chatbot, use_deep_research],
            outputs=[chatbot, error_box]
        ).then(
            lambda: "",
            None,
            [msg]
        )
        
        # Enter ํ‚ค ์ œ์ถœ ํ—ˆ์šฉ
        msg.submit(
            query_deepseek_streaming,
            inputs=[msg, chatbot, use_deep_research],
            outputs=[chatbot, error_box]
        ).then(
            lambda: "",
            None,
            [msg]
        )
        
    return demo

# ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
if __name__ == "__main__":
    demo = create_deepseek_interface()
    demo.launch(debug=True)