File size: 22,533 Bytes
6035bfe
a8eb718
 
5dfb15d
9470e9c
a8eb718
6035bfe
a8eb718
6035bfe
 
 
 
 
db3cf6b
6035bfe
 
 
a8eb718
 
 
 
db3cf6b
a8eb718
 
 
6035bfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9470e9c
6035bfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e8ad25
 
 
 
 
6035bfe
 
 
4dd03cf
 
 
 
f00b06e
 
 
e0713c0
 
f00b06e
 
4e8ad25
 
f00b06e
 
 
10a5dab
 
 
 
 
 
4dd03cf
f00b06e
4dd03cf
f00b06e
 
 
4dd03cf
10a5dab
a8eb718
 
 
9470e9c
a8eb718
 
9470e9c
a8eb718
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ef3920
9470e9c
a8eb718
 
 
 
 
 
 
9470e9c
a8eb718
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9470e9c
a8eb718
 
 
 
 
 
 
5dfb15d
a8eb718
5dfb15d
 
a8eb718
9470e9c
 
 
5dfb15d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9470e9c
5dfb15d
9470e9c
 
 
 
 
e2baeda
 
 
 
 
 
 
 
 
 
 
 
9470e9c
a8eb718
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9470e9c
a8eb718
e2baeda
a8eb718
9470e9c
 
 
 
 
 
a8eb718
e2baeda
 
 
 
 
 
 
a8eb718
 
 
6035bfe
 
65ee007
14e03a5
 
 
 
 
 
 
 
 
 
65ee007
14e03a5
 
 
65ee007
14e03a5
 
65ee007
14e03a5
65ee007
 
14e03a5
65ee007
 
 
14e03a5
65ee007
 
14e03a5
65ee007
 
14e03a5
65ee007
 
f20969d
65ee007
14e03a5
 
 
 
65ee007
14e03a5
65ee007
 
 
 
14e03a5
65ee007
14e03a5
 
 
65ee007
 
14e03a5
65ee007
 
 
14e03a5
65ee007
14e03a5
 
65ee007
14e03a5
65ee007
14e03a5
 
65ee007
14e03a5
65ee007
14e03a5
 
 
 
 
83e2123
65ee007
d972c46
14e03a5
e2baeda
6035bfe
15cf0dc
6035bfe
 
a8eb718
14e03a5
 
c853ff4
6035bfe
7cbbdae
c853ff4
6035bfe
7cbbdae
c853ff4
6035bfe
7cbbdae
c853ff4
14e03a5
7cbbdae
d972c46
a8eb718
6035bfe
15cf0dc
6035bfe
14e03a5
7cbbdae
 
 
 
 
 
14e03a5
7cbbdae
 
 
 
 
892de37
e2baeda
 
9470e9c
a8eb718
 
9470e9c
 
a8eb718
14c240d
14e03a5
c292539
14e03a5
c292539
 
 
6035bfe
 
 
 
 
14e03a5
 
e2baeda
6035bfe
 
 
 
4495da2
9bbcc80
 
 
d6f7a9b
9bbcc80
 
c292539
14e03a5
81ba805
 
4dd03cf
81ba805
 
 
 
4dd03cf
81ba805
 
 
 
 
58bb0eb
4dd03cf
 
b557bec
 
 
 
 
0370aef
c9c6fdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
import os
import gradio as gr
from gradio import ChatMessage
from typing import Iterator, List, Dict, Tuple, Any
import google.generativeai as genai
from huggingface_hub import HfApi
import requests
import re
import traceback
import time
import threading
import json

# HuggingFace ๊ด€๋ จ API ํ‚ค (์ŠคํŽ˜์ด์Šค ๋ถ„์„ ์šฉ)
HF_TOKEN = os.getenv("HF_TOKEN")
hf_api = HfApi(token=HF_TOKEN)

# Gemini 2.0 Flash Thinking ๋ชจ๋ธ ๊ด€๋ จ API ํ‚ค ๋ฐ ํด๋ผ์ด์–ธํŠธ (LLM ์šฉ)
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
genai.configure(api_key=GEMINI_API_KEY)
model = genai.GenerativeModel("gemini-2.0-flash-thinking-exp-01-21")

# --------------------------------------------------
# ํŒŒ์ผ ๋ฐ ์ŠคํŽ˜์ด์Šค ๋ถ„์„ ๊ด€๋ จ ํ•จ์ˆ˜๋“ค (๊ธฐ์กด ์ฝ”๋“œ ์œ ์ง€)
# --------------------------------------------------
def get_headers():
    if not HF_TOKEN:
        raise ValueError("Hugging Face token not found in environment variables")
    return {"Authorization": f"Bearer {HF_TOKEN}"}

def get_file_content(space_id: str, file_path: str) -> str:
    file_url = f"https://huggingface.co/spaces/{space_id}/raw/main/{file_path}"
    try:
        response = requests.get(file_url, headers=get_headers())
        if response.status_code == 200:
            return response.text
        else:
            return f"File not found or inaccessible: {file_path}"
    except requests.RequestException:
        return f"Error fetching content for file: {file_path}"

def get_space_structure(space_id: str) -> Dict:
    try:
        files = hf_api.list_repo_files(repo_id=space_id, repo_type="space")
        tree = {"type": "directory", "path": "", "name": space_id, "children": []}
        for file in files:
            path_parts = file.split('/')
            current = tree
            for i, part in enumerate(path_parts):
                if i == len(path_parts) - 1:  # ํŒŒ์ผ
                    current["children"].append({"type": "file", "path": file, "name": part})
                else:
                    found = False
                    for child in current["children"]:
                        if child["type"] == "directory" and child["name"] == part:
                            current = child
                            found = True
                            break
                    if not found:
                        new_dir = {"type": "directory", "path": '/'.join(path_parts[:i+1]), "name": part, "children": []}
                        current["children"].append(new_dir)
                        current = new_dir
        return tree
    except Exception as e:
        print(f"Error in get_space_structure: {str(e)}")
        return {"error": f"API request error: {str(e)}"}

def format_tree_structure(tree_data: Dict, indent: str = "") -> str:
    if "error" in tree_data:
        return tree_data["error"]
    formatted = f"{indent}{'๐Ÿ“' if tree_data.get('type') == 'directory' else '๐Ÿ“„'} {tree_data.get('name', 'Unknown')}\n"
    if tree_data.get("type") == "directory":
        for child in sorted(tree_data.get("children", []), key=lambda x: (x.get("type", "") != "directory", x.get("name", ""))):
            formatted += format_tree_structure(child, indent + "  ")
    return formatted

def adjust_lines_for_code(code_content: str, min_lines: int = 10, max_lines: int = 100) -> int:
    num_lines = len(code_content.split('\n'))
    return min(max(num_lines, min_lines), max_lines)

def analyze_space(url: str, progress=gr.Progress()):
    try:
        space_id = url.split('spaces/')[-1]
        if not re.match(r'^[\w.-]+/[\w.-]+$', space_id):
            raise ValueError(f"Invalid Space ID format: {space_id}")
        progress(0.1, desc="ํŒŒ์ผ ๊ตฌ์กฐ ๋ถ„์„ ์ค‘...")
        tree_structure = get_space_structure(space_id)
        if "error" in tree_structure:
            raise ValueError(tree_structure["error"])
        tree_view = format_tree_structure(tree_structure)
        progress(0.3, desc="app.py ๋‚ด์šฉ ๊ฐ€์ ธ์˜ค๋Š” ์ค‘...")
        app_content = get_file_content(space_id, "app.py")
        progress(0.5, desc="์ฝ”๋“œ ์š”์•ฝ ์ค‘...")
        summary = summarize_code(app_content)
        progress(0.7, desc="์ฝ”๋“œ ๋ถ„์„ ์ค‘...")
        analysis = analyze_code(app_content)
        progress(0.9, desc="์‚ฌ์šฉ๋ฒ• ์„ค๋ช… ์ƒ์„ฑ ์ค‘...")
        usage = explain_usage(app_content)
        app_py_lines = adjust_lines_for_code(app_content)
        progress(1.0, desc="์™„๋ฃŒ")
        return app_content, tree_view, tree_structure, space_id, summary, analysis, usage, app_py_lines
    except Exception as e:
        print(f"Error in analyze_space: {str(e)}")
        print(traceback.format_exc())
        return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}", "", None, "", "", "", "", 10

# --------------------------------------------------
# Gemini 2.0 Flash Thinking ๋ชจ๋ธ ๊ด€๋ จ ํ—ฌํผ ํ•จ์ˆ˜๋“ค
# --------------------------------------------------
def format_chat_history(messages: List[ChatMessage]) -> List[Dict]:
    formatted_history = []
    for message in messages:
        # thinking ๋ฉ”์‹œ์ง€(๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์žˆ๋Š” ๋ฉ”์‹œ์ง€)๋Š” ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.
        if not (hasattr(message, "metadata") and message.metadata):
            formatted_history.append({
                "role": "user" if message.role == "user" else "assistant",
                "parts": [message.content or ""]
            })
    return formatted_history

def gemini_chat_completion(system_message: str, user_message: str, max_tokens: int = 200, temperature: float = 0.7) -> str:
    initial_messages = [
        ChatMessage(role="system", content=system_message),
        ChatMessage(role="user", content=user_message)
    ]
    chat_history = format_chat_history(initial_messages)
    chat = model.start_chat(history=chat_history)
    final_response = ""
    try:
        for chunk in chat.send_message(user_message, stream=True):
            parts = chunk.candidates[0].content.parts
            if len(parts) == 2:
                final_response += parts[1].text
            else:
                final_response += parts[0].text
        return final_response.strip()
    except Exception as e:
        return f"LLM ํ˜ธ์ถœ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"

def summarize_code(app_content: str) -> str:
    system_message = "๋‹น์‹ ์€ Python ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜๊ณ  ์š”์•ฝํ•˜๋Š” AI ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ฝ”๋“œ๋ฅผ 3์ค„ ์ด๋‚ด๋กœ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์š”์•ฝํ•ด์ฃผ์„ธ์š”."
    user_message = f"๋‹ค์Œ Python ์ฝ”๋“œ๋ฅผ 3์ค„ ์ด๋‚ด๋กœ ์š”์•ฝํ•ด์ฃผ์„ธ์š”:\n\n{app_content}"
    try:
        return gemini_chat_completion(system_message, user_message, max_tokens=200, temperature=0.7)
    except Exception as e:
        return f"์š”์•ฝ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"

def analyze_code(app_content: str) -> str:
    system_message = (
        "๋‹น์‹ ์€ Python ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜๋Š” AI ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ๋‹ค์Œ ํ•ญ๋ชฉ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”:\n"
        "A. ๋ฐฐ๊ฒฝ ๋ฐ ํ•„์š”์„ฑ\n"
        "B. ๊ธฐ๋Šฅ์  ํšจ์šฉ์„ฑ ๋ฐ ๊ฐ€์น˜\n"
        "C. ํŠน์žฅ์ \n"
        "D. ์ ์šฉ ๋Œ€์ƒ ๋ฐ ํƒ€๊ฒŸ\n"
        "E. ๊ธฐ๋Œ€ํšจ๊ณผ\n"
        "๊ธฐ์กด ๋ฐ ์œ ์‚ฌ ํ”„๋กœ์ ํŠธ์™€ ๋น„๊ตํ•˜์—ฌ ๋ถ„์„ํ•ด์ฃผ์„ธ์š”. Markdown ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅํ•˜์„ธ์š”."
    )
    user_message = f"๋‹ค์Œ Python ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•ด์ฃผ์„ธ์š”:\n\n{app_content}"
    try:
        return gemini_chat_completion(system_message, user_message, max_tokens=1000, temperature=0.7)
    except Exception as e:
        return f"๋ถ„์„ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"

def explain_usage(app_content: str) -> str:
    system_message = "๋‹น์‹ ์€ Python ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ์‚ฌ์šฉ๋ฒ•์„ ์„ค๋ช…ํ•˜๋Š” AI ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ฝ”๋“œ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋งˆ์น˜ ํ™”๋ฉด์„ ๋ณด๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์‚ฌ์šฉ๋ฒ•์„ ์ƒ์„ธํžˆ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”. Markdown ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅํ•˜์„ธ์š”."
    user_message = f"๋‹ค์Œ Python ์ฝ”๋“œ์˜ ์‚ฌ์šฉ๋ฒ•์„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”:\n\n{app_content}"
    try:
        return gemini_chat_completion(system_message, user_message, max_tokens=800, temperature=0.7)
    except Exception as e:
        return f"์‚ฌ์šฉ๋ฒ• ์„ค๋ช… ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"

def convert_chat_history(messages: List[Any]) -> List[Tuple[str, str]]:
    """
    ๋ฉ”์‹œ์ง€ ๋ชฉ๋ก์˜ ๊ฐ ํ•ญ๋ชฉ์ด ChatMessage ๊ฐ์ฒด๋ผ๋ฉด (user, assistant) ํŠœํ”Œ๋กœ,
    ์ด๋ฏธ ํŠœํ”Œ์ธ ๊ฒฝ์šฐ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    """
    conv = []
    i = 0
    while i < len(messages):
        # ๋งŒ์•ฝ ์ด๋ฏธ ํŠœํ”Œ์ด๋ฉด ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ
        if isinstance(messages[i], tuple):
            conv.append(messages[i])
            i += 1
        # ๋งŒ์•ฝ ChatMessage ๊ฐ์ฒด๋ผ๋ฉด
        elif hasattr(messages[i], "role"):
            if messages[i].role == "user":
                user_text = messages[i].content
                bot_text = ""
                if i + 1 < len(messages) and hasattr(messages[i+1], "role") and messages[i+1].role == "assistant":
                    bot_text = messages[i+1].content
                    i += 2
                else:
                    i += 1
                conv.append((user_text, bot_text))
            else:
                conv.append(("", messages[i].content))
                i += 1
        else:
            i += 1
    return conv

def convert_to_chatmessage(history: List[Tuple[str, str]]) -> List[ChatMessage]:
    """
    ํŠœํ”Œ ๋ชฉ๋ก์„ ChatMessage ๊ฐ์ฒด ๋ชฉ๋ก์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    """
    new_history = []
    for tup in history:
        if tup[0]:
            new_history.append(ChatMessage(role="user", content=tup[0]))
        if tup[1]:
            new_history.append(ChatMessage(role="assistant", content=tup[1]))
    return new_history

def stream_gemini_response(user_message: str, messages: List[ChatMessage]) -> Iterator[List[ChatMessage]]:
    if not user_message.strip():
        messages.append(ChatMessage(role="assistant", content="Please provide a non-empty text message. Empty input is not allowed."))
        yield messages
        return

    try:
        print(f"\n=== New Request (Text) ===")
        print(f"User message: {user_message}")
        chat_history = format_chat_history(messages)
        chat = model.start_chat(history=chat_history)
        response = chat.send_message(user_message, stream=True)
        thought_buffer = ""
        response_buffer = ""
        thinking_complete = False

        messages.append(
            ChatMessage(
                role="assistant",
                content="",
                metadata={"title": "โš™๏ธ Thinking: *The thoughts produced by the model are experimental"}
            )
        )

        for chunk in response:
            parts = chunk.candidates[0].content.parts
            current_chunk = parts[0].text

            if len(parts) == 2 and not thinking_complete:
                thought_buffer += current_chunk
                print(f"\n=== Complete Thought ===\n{thought_buffer}")
                messages[-1] = ChatMessage(
                    role="assistant",
                    content=thought_buffer,
                    metadata={"title": "โš™๏ธ Thinking: *The thoughts produced by the model are experimental"}
                )
                yield messages

                response_buffer = parts[1].text
                print(f"\n=== Starting Response ===\n{response_buffer}")
                messages.append(
                    ChatMessage(
                        role="assistant",
                        content=response_buffer
                    )
                )
                thinking_complete = True

            elif thinking_complete:
                response_buffer += current_chunk
                print(f"\n=== Response Chunk ===\n{current_chunk}")
                messages[-1] = ChatMessage(
                    role="assistant",
                    content=response_buffer
                )

            else:
                thought_buffer += current_chunk
                print(f"\n=== Thinking Chunk ===\n{current_chunk}")
                messages[-1] = ChatMessage(
                    role="assistant",
                    content=thought_buffer,
                    metadata={"title": "โš™๏ธ Thinking: *The thoughts produced by the model are experimental"}
                )
            yield messages

        print(f"\n=== Final Response ===\n{response_buffer}")

    except Exception as e:
        print(f"\n=== Error ===\n{str(e)}")
        messages.append(
            ChatMessage(
                role="assistant",
                content=f"I apologize, but I encountered an error: {str(e)}"
            )
        )
        yield messages

def respond(message: str, history: List[ChatMessage]) -> Iterator[List[Tuple[str, str]]]:
    """
    stream_gemini_response()๋ฅผ ํ˜ธ์ถœํ•œ ํ›„, ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ํŠœํ”Œ ๋ชฉ๋ก์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    """
    for updated_messages in stream_gemini_response(message, history):
        yield convert_chat_history(updated_messages)

def user_message(msg: str, history: List[ChatMessage]) -> Tuple[str, List[ChatMessage]]:
    history.append(ChatMessage(role="user", content=msg))
    return "", history

def respond_wrapper(message, chat_history, max_tokens, temperature, top_p):
    # chat_history๊ฐ€ ํŠœํ”Œ ๋ชฉ๋ก์ด๋ผ๋ฉด ChatMessage ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜
    if chat_history and isinstance(chat_history[0], tuple):
        chat_history = convert_to_chatmessage(chat_history)
    for updated in stream_gemini_response(message, chat_history):
        yield "", convert_chat_history(updated)

# --------------------------------------------------
# Gradio UI ๊ตฌ์„ฑ
# --------------------------------------------------
def create_ui():
    try:
        css = """
        /* ์ „์ฒด ๋ฐฐ๊ฒฝ ๋ฐ ๊ธฐ๋ณธ ๊ธ€๊ผด ์„ค์ • */
        body {
            background-color: #f9f9f9;
            font-family: 'Helvetica Neue', Arial, sans-serif;
            color: #333;
        }
        /* ํ•˜๋‹จ ํ‘ธํ„ฐ ์ˆจ๊น€ */
        footer { visibility: hidden; }
        
        /* ์ถœ๋ ฅ ๊ทธ๋ฃน ์Šคํƒ€์ผ: ๋ฐ์€ ๋ฐฐ๊ฒฝ, ๋ถ€๋“œ๋Ÿฌ์šด ํ…Œ๋‘๋ฆฌ์™€ ๊ทธ๋ฆผ์ž */
        .output-group {
            border: 1px solid #ccc;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 20px;
            background-color: #ffffff;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        /* ์Šคํฌ๋กค ์˜์—ญ ์„ค์ • */
        .scroll-lock {
            overflow-y: auto !important;
            max-height: 300px !important;
        }
        .tree-view-scroll {
            overflow-y: auto !important;
            max-height: 400px !important;
        }
        .full-height {
            height: 80vh !important;
            overflow-y: auto !important;
        }
        /* ์ฝ”๋“œ ๋ฐ•์Šค ์Šคํƒ€์ผ: ๋ชจ๋…ธ์ŠคํŽ˜์ด์Šค ํฐํŠธ์™€ ๋ฐ์€ ๋ฐฐ๊ฒฝ */
        .code-box {
            overflow-x: auto !important;
            overflow-y: auto !important;
            white-space: pre !important;
            background-color: #f5f5f5;
            border-radius: 4px;
            padding: 10px;
            font-family: 'Courier New', Courier, monospace;
        }
        .code-box > div { min-width: 100% !important; }
        .code-box > div > textarea {
            word-break: normal !important;
            overflow-wrap: normal !important;
        }
        /* ํƒญ ๋‚ด๋น„๊ฒŒ์ด์…˜ ์Šคํƒ€์ผ: ๋‹จ์ˆœํ•˜๊ณ  ๊น”๋”ํ•œ ๋””์ž์ธ */
        .tab-nav {
            background-color: #ffffff;
            border-bottom: 1px solid #ccc;
            display: flex;
        }
        .tab-nav button {
            background: none;
            border: none;
            padding: 10px 20px;
            margin: 0;
            cursor: pointer;
            font-size: 16px;
            color: #555;
            transition: color 0.3s, border-bottom 0.3s;
        }
        .tab-nav button:hover,
        .tab-nav button.selected {
            color: #000;
            border-bottom: 2px solid #007BFF;
        }
        /* ์ž…๋ ฅ์ฐฝ ๋ฐ ํ…์ŠคํŠธ ์˜์—ญ ์Šคํƒ€์ผ */
        input[type="text"], textarea {
            color: #333;
            background-color: #fff;
            border: 1px solid #ccc;
            border-radius: 4px;
            padding: 8px;
        }
        """

        with gr.Blocks(theme="default", css=css) as demo:
            gr.Markdown("# MOUSE: Space Research Thinking", elem_classes="header-markdown")
            
            with gr.Tabs() as tabs:
                with gr.TabItem("๋ถ„์„"):
                    with gr.Row():
                        with gr.Column(scale=6):
                            url_input = gr.Textbox(label="HuggingFace Space URL", placeholder="์˜ˆ: https://huggingface.co/spaces/username/space_name")
                            analyze_button = gr.Button("๋ถ„์„", variant="primary")
    
                            with gr.Group(elem_classes="output-group scroll-lock"):
                                summary_output = gr.Markdown(label="์š”์•ฝ (3์ค„ ์ด๋‚ด)")
    
                            with gr.Group(elem_classes="output-group scroll-lock"):
                                analysis_output = gr.Markdown(label="๋ถ„์„")
    
                            with gr.Group(elem_classes="output-group scroll-lock"):
                                usage_output = gr.Markdown(label="์‚ฌ์šฉ๋ฒ•")
    
                            with gr.Group(elem_classes="output-group tree-view-scroll"):
                                tree_view_output = gr.Textbox(label="ํŒŒ์ผ ๊ตฌ์กฐ (Tree View)", lines=30)

                        with gr.Column(scale=4):
                            with gr.Group(elem_classes="output-group full-height"):
                                code_tabs = gr.Tabs()
                                with code_tabs:
                                    with gr.TabItem("app.py"):
                                        app_py_content = gr.Code(
                                            language="python",
                                            label="app.py",
                                            lines=200,
                                            elem_classes="full-height code-box"
                                        )
                                    with gr.TabItem("requirements.txt"):
                                        requirements_content = gr.Textbox(
                                            label="requirements.txt",
                                            lines=200,
                                            elem_classes="full-height code-box"
                                        )

                with gr.TabItem("AI ์ฝ”๋“œ์ฑ—"):
                    gr.Markdown("## : ์˜ˆ์ œ๋ฅผ ์ž…๋ ฅ/์„ ํƒํ•˜๊ณ , ์ด์–ด์„œ ๋ณต์‚ฌํ•œ app.py ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๋ถ™์—ฌ ๋„ฃ์œผ์„ธ์š”", elem_classes="header-markdown")                    
                    # ์ฑ„ํŒ… ๋ฐ•์Šค ๋†’์ด๋ฅผ 400px๋กœ ์ง€์ •ํ•˜์—ฌ ํ™”๋ฉด ๋†’์ด์— ๋งž๊ฒŒ ์ค„์ž„.
                    chatbot = gr.Chatbot(
                        label="๋Œ€ํ™”",
                        elem_classes="output-group",
                        height=400
                    )

                    msg = gr.Textbox(label="๋ฉ”์‹œ์ง€", placeholder="๋ฉ”์‹œ์ง€๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”...")
                    
                    # ์ˆจ๊ฒจ์ง„ ํŒŒ๋ผ๋ฏธํ„ฐ
                    max_tokens = gr.Slider(minimum=1, maximum=8000, value=4000, label="Max Tokens", visible=False)
                    temperature = gr.Slider(minimum=0, maximum=1, value=0.7, label="Temperature", visible=False)
                    top_p = gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P", visible=False)
                    
                    examples = [
                        ["์ƒ์„ธํ•œ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ๋งˆ์น˜ ํ™”๋ฉด์„ ๋ณด๋ฉด์„œ ์„ค๋ช…ํ•˜๋“ฏ์ด 4000 ํ† ํฐ ์ด์ƒ ์ž์„ธํžˆ ์„ค๋ช…ํ•˜๋ผ"],
                        ["FAQ 20๊ฑด์„ ์ƒ์„ธํ•˜๊ฒŒ ์ž‘์„ฑํ•˜๋ผ. 4000ํ† ํฐ ์ด์ƒ ์‚ฌ์šฉํ•˜๋ผ."],
                        ["์‚ฌ์šฉ ๋ฐฉ๋ฒ•๊ณผ ์ฐจ๋ณ„์ , ํŠน์ง•, ๊ฐ•์ ์„ ์ค‘์‹ฌ์œผ๋กœ 4000 ํ† ํฐ ์ด์ƒ ์œ ํŠœ๋ธŒ ์˜์ƒ ์Šคํฌ๋ฆฝํŠธ ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•˜๋ผ"],
                        ["๋ณธ ์„œ๋น„์Šค๋ฅผ SEO ์ตœ์ ํ™”ํ•˜์—ฌ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ๋กœ 4000 ํ† ํฐ ์ด์ƒ ์ž‘์„ฑํ•˜๋ผ"],
                        ["ํŠนํ—ˆ ์ถœ์›์— ํ™œ์šฉํ•  ํ˜์‹ ์ ์ธ ์ฐฝ์˜ ๋ฐœ๋ช… ๋‚ด์šฉ์„ ์ค‘์‹ฌ์œผ๋กœ 4000 ํ† ํฐ ์ด์ƒ ์ž‘์„ฑํ•˜๋ผ."],
                        ["ํ˜์‹ ์ ์ด๊ณ  ๋…ผ๋ฆฌ์ ์ธ ์ „๋ฌธ ๋…ผ๋ฌธ์˜ ํ˜•์‹์œผ๋กœ 4000 ํ† ํฐ ์ด์ƒ ์ž‘์„ฑํ•˜๋ผ."],                        
                        ["๊ณ„์† ์ด์–ด์„œ ๋‹ต๋ณ€ํ•˜๋ผ"],
                    ]
                    gr.Examples(examples, inputs=msg)

                    msg.submit(respond_wrapper, [msg, chatbot, max_tokens, temperature, top_p], [msg, chatbot])
                    
                with gr.TabItem("Recommended Best"):
                    gr.Markdown(
                        "Discover the best recommended HuggingFace Spaces [here](https://huggingface.co/spaces/openfree/Korean-Leaderboard).",
                        elem_id="recommended-best"
                    )

            # ์ƒํƒœ ์ €์žฅ์šฉ ๋ณ€์ˆ˜
            space_id_state = gr.State()
            tree_structure_state = gr.State()
            app_py_content_lines = gr.State()

            analyze_button.click(
                analyze_space,
                inputs=[url_input],
                outputs=[app_py_content, tree_view_output, tree_structure_state, space_id_state, summary_output, analysis_output, usage_output, app_py_content_lines]
            ).then(
                lambda space_id: get_file_content(space_id, "requirements.txt"),
                inputs=[space_id_state],
                outputs=[requirements_content]
            )

            app_py_content.change(lambda lines: gr.update(lines=lines), inputs=[app_py_content_lines], outputs=[app_py_content])

        return demo

    except Exception as e:
        print(f"Error in create_ui: {str(e)}")
        print(traceback.format_exc())
        raise

if __name__ == "__main__":
    try:
        print("Starting HuggingFace Space Analyzer...")
        demo = create_ui()
        print("UI created successfully.")
        print("Configuring Gradio queue...")
        demo.queue()
        print("Gradio queue configured.")
        print("Launching Gradio app...")
        demo.launch(
            server_name="0.0.0.0",
            server_port=7860,
            share=False,
            debug=True,
            show_api=False
        )
        print("Gradio app launched successfully.")
    except Exception as e:
        print(f"Error in main: {str(e)}")
        print("Detailed error information:")
        print(traceback.format_exc())
        raise