File size: 6,006 Bytes
4acdad0
1748922
4acdad0
 
 
 
 
 
 
 
 
 
4af25eb
4acdad0
 
 
1748922
4acdad0
 
 
 
 
 
c2606ca
 
2d79140
c2606ca
 
 
 
 
 
 
4acdad0
daedbb3
 
 
c2606ca
 
 
 
 
 
 
daedbb3
c2606ca
 
 
4acdad0
daedbb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4acdad0
daedbb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4acdad0
daedbb3
4acdad0
c2606ca
4acdad0
c2606ca
 
 
 
daedbb3
 
 
c2606ca
daedbb3
4acdad0
c2606ca
 
daedbb3
4acdad0
 
 
 
 
 
 
 
 
 
 
95bb429
 
 
 
4acdad0
383b3b6
4acdad0
c2606ca
 
 
daedbb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2606ca
 
 
 
daedbb3
c2606ca
4acdad0
 
 
 
 
 
 
 
 
 
 
 
 
 
daedbb3
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
import gradio as gr
from huggingface_hub import InferenceClient, HfApi
import os
import requests
from typing import List, Dict, Union
import traceback
from PIL import Image
from io import BytesIO
import asyncio
from gradio_client import Client
import time
import threading
import json

HF_TOKEN = os.getenv("HF_TOKEN")
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
hf_api = HfApi(token=HF_TOKEN)

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_app_py_content(space_id: str) -> str:
    app_py_url = f"https://huggingface.co/spaces/{space_id}/raw/main/app.py"
    try:
        response = requests.get(app_py_url, headers=get_headers())
        if response.status_code == 200:
            return response.text
        else:
            return f"app.py file not found or inaccessible for space: {space_id}"
    except requests.RequestException:
        return f"Error fetching app.py content for space: {space_id}"

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

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

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

def analyze_space(url: str):
    try:
        # URL์—์„œ space_id ์ถ”์ถœ
        space_id = url.split('spaces/')[-1]
        
        app_content = get_app_py_content(space_id)
        summary = summarize_code(app_content)
        analysis = analyze_code(app_content)
        usage = explain_usage(app_content)
        
        return summary, analysis, usage, app_content
    except Exception as e:
        print(f"Error in analyze_space: {str(e)}")
        print(traceback.format_exc())
        return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}", "", "", ""

def create_ui():
    try:
        css = """
        footer {visibility: hidden;}
        .output-group {
            border: 1px solid #ddd;
            border-radius: 5px;
            padding: 10px;
            margin-bottom: 20px;
        }
        .scroll-lock {
            overflow: auto !important;
            max-height: 400px !important;
        }
        """

        with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
            gr.Markdown("# HuggingFace Space Analyzer")
            
            with gr.Row():
                with gr.Column(scale=6):  # ์™ผ์ชฝ 60%
                    url_input = gr.Textbox(label="HuggingFace Space URL")
                    analyze_button = gr.Button("๋ถ„์„")
                    
                    with gr.Group(elem_classes="output-group scroll-lock"):
                        summary_output = gr.Textbox(label="์š”์•ฝ (3์ค„ ์ด๋‚ด)", lines=3)
                    
                    with gr.Group(elem_classes="output-group scroll-lock"):
                        analysis_output = gr.Textbox(label="๋ถ„์„", lines=15)
                    
                    with gr.Group(elem_classes="output-group scroll-lock"):
                        usage_output = gr.Textbox(label="์‚ฌ์šฉ๋ฒ•", lines=10)
                
                with gr.Column(scale=4):  # ์˜ค๋ฅธ์ชฝ 40%
                    with gr.Group(elem_classes="output-group scroll-lock"):
                        app_py_content = gr.Code(language="python", label="๋ฉ”์ธ ์†Œ์Šค์ฝ”๋“œ", lines=30)

            analyze_button.click(
                analyze_space,
                inputs=[url_input],
                outputs=[summary_output, analysis_output, usage_output, 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:
        demo = create_ui()
        demo.launch()
    except Exception as e:
        print(f"Error in main: {str(e)}")
        print(traceback.format_exc())