File size: 6,599 Bytes
6c31f83
 
 
 
 
 
f438be7
6c31f83
 
3db5c7b
6c31f83
 
3db5c7b
6c31f83
3db5c7b
6c31f83
 
f438be7
6c31f83
3db5c7b
6c31f83
 
3db5c7b
6c31f83
 
9855bdf
e703ff5
6c31f83
 
 
 
 
 
 
 
 
 
 
 
 
 
3db5c7b
e703ff5
3db5c7b
6c31f83
3db5c7b
6c31f83
 
 
f438be7
3db5c7b
f438be7
 
3db5c7b
6c31f83
3db5c7b
 
 
6c31f83
 
3db5c7b
6c31f83
3db5c7b
6c31f83
 
3db5c7b
 
6c31f83
 
 
3db5c7b
 
6c31f83
3db5c7b
6c31f83
3db5c7b
6c31f83
 
 
3db5c7b
 
 
6c31f83
3db5c7b
 
 
6c31f83
 
 
3db5c7b
6c31f83
3db5c7b
 
6c31f83
 
 
3db5c7b
6c31f83
 
3db5c7b
 
 
 
 
 
6c31f83
 
3db5c7b
 
 
6c31f83
3db5c7b
6c31f83
3db5c7b
6c31f83
 
 
8d26a35
 
6c31f83
 
3db5c7b
6c31f83
 
3db5c7b
 
6c31f83
 
 
3db5c7b
6c31f83
3db5c7b
6c31f83
3db5c7b
6c31f83
3db5c7b
6c31f83
 
3db5c7b
 
 
6c31f83
 
 
 
3db5c7b
 
6c31f83
3db5c7b
6c31f83
3db5c7b
6c31f83
 
3db5c7b
 
6c31f83
3db5c7b
 
6c31f83
3db5c7b
6c31f83
3db5c7b
6c31f83
3db5c7b
 
 
 
 
6c31f83
 
3db5c7b
6c31f83
3db5c7b
6c31f83
3db5c7b
 
 
 
 
6c31f83
 
 
 
3db5c7b
6c31f83
 
 
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
import gradio as gr
import os
import requests
import json

def create_deepseek_interface():
    # ์ฑ„ํŒ… ๊ธฐ๋ก ์ดˆ๊ธฐํ™” (๋ฉ”์‹œ์ง€ ํ˜•์‹)
    chat_history = []
    
    # DeepSeek API ํ˜ธ์ถœ ํ•จ์ˆ˜
    def query_deepseek(message, history, api_key):
        if not api_key:
            return history, "Fireworks AI API ํ‚ค๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”"
        
        # API ์š”์ฒญ์„ ์œ„ํ•œ ๋Œ€ํ™” ๊ธฐ๋ก ์ค€๋น„
        messages = []
        for h in history:
            messages.append(h)
        
        # ์ƒˆ ์‚ฌ์šฉ์ž ๋ฉ”์‹œ์ง€ ์ถ”๊ฐ€
        messages.append({"role": "user", "content": message})
        
        # API ์š”์ฒญ ์ค€๋น„
        url = "https://api.fireworks.ai/inference/v1/chat/completions"
        payload = {
            "model": "accounts/ginipicks-88506d/deployedModels/qwq-32b-9d79e454",
            "max_tokens": 20480,
            "top_p": 1,
            "top_k": 40,
            "presence_penalty": 0,
            "frequency_penalty": 0,
            "temperature": 0.6,
            "messages": messages
        }
        headers = {
            "Accept": "application/json",
            "Content-Type": "application/json",
            "Authorization": f"Bearer {api_key}"
        }
        
        try:
            # API ์š”์ฒญ ์ „์†ก
            response = requests.request("POST", url, headers=headers, data=json.dumps(payload))
            response.raise_for_status()  # HTTP ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ ์˜ˆ์™ธ ๋ฐœ์ƒ
            
            # ์‘๋‹ต ์ถ”์ถœ
            result = response.json()
            assistant_response = result.get("choices", [{}])[0].get("message", {}).get("content", "")
            
            # ๋Œ€ํ™” ๊ธฐ๋ก ์—…๋ฐ์ดํŠธ (๋ฉ”์‹œ์ง€ ํ˜•์‹์œผ๋กœ)
            new_history = history.copy()
            new_history.append({"role": "user", "content": message})
            new_history.append({"role": "assistant", "content": assistant_response})
            return new_history, ""
        except requests.exceptions.RequestException as e:
            error_msg = f"API ์˜ค๋ฅ˜: {str(e)}"
            if hasattr(response, 'status_code') and response.status_code == 401:
                error_msg = "์ธ์ฆ ์‹คํŒจ. API ํ‚ค๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”."
            return history, error_msg
    
    # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
    with gr.Blocks(theme="soft", fill_height=True) as demo:
        # ํ—ค๋” ์„น์…˜
        gr.Markdown(
            """
            # ๐Ÿค– DeepSeek V3 ์ถ”๋ก  ์ธํ„ฐํŽ˜์ด์Šค
            ### Fireworks AI๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ณ ๊ธ‰ AI ๋ชจ๋ธ
            """
        )
        
        # ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ์ƒํƒœ
        error_state = gr.State("")
        
        # ๋ฉ”์ธ ๋ ˆ์ด์•„์›ƒ (๋‘ ๊ฐœ์˜ ์—ด)
        with gr.Row():
            # ์‚ฌ์ด๋“œ๋ฐ” - ๋ชจ๋ธ ์ •๋ณด ๋ฐ API ํ‚ค
            with gr.Column(scale=1):
                gr.Markdown(
                    """
                    ## ๐Ÿ”‘ ์ ‘๊ทผ ์ œ์–ด
                    ### ์ถ”๋ก  ์ œ๊ณต์ž
                    ์ด ์ธํ„ฐํŽ˜์ด์Šค๋Š” Fireworks AI API๋ฅผ ํ†ตํ•ด ์ œ๊ณต๋˜๋Š” DeepSeek-V3 ๋ชจ๋ธ์— ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค.
                    
                    #### ์ธ์ฆ
                    - ์•„๋ž˜์— Fireworks AI API ํ‚ค๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
                    - ์—”๋“œ-ํˆฌ-์—”๋“œ ์•”ํ˜ธํ™”๋กœ ์•ˆ์ „ํ•œ API ์ ‘๊ทผ
                    """
                )
                
                # API ํ‚ค ์ž…๋ ฅ
                api_key = gr.Textbox(
                    label="Fireworks AI API ํ‚ค",
                    placeholder="API ํ‚ค๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”...",
                    type="password"
                )
                
                # ๋ชจ๋ธ ์„ธ๋ถ€ ์ •๋ณด ์„น์…˜
                gr.Markdown(
                    """
                    ### ๐Ÿ“Š ๋ชจ๋ธ ์„ธ๋ถ€ ์ •๋ณด
                    - **๋ชจ๋ธ**: DeepSeek-V3-0324
                    - **์ œ๊ณต์ž**: Fireworks AI
                    - **์ตœ๋Œ€ ํ† ํฐ**: 20,480
                    - **์˜จ๋„**: 0.6
                    - **๊ธฐ๋Šฅ**: ๊ณ ๊ธ‰ ์–ธ์–ด ์ดํ•ด
                    """
                )
                
                # ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ํ‘œ์‹œ
                error_box = gr.Markdown("")
            
            # ๋ฉ”์ธ ์ฝ˜ํ…์ธ  ์˜์—ญ
            with gr.Column(scale=2):
                # ์ฑ„ํŒ… ์ธํ„ฐํŽ˜์ด์Šค
                chatbot = gr.Chatbot(
                    height=500,
                    show_label=False,
                    container=True,
                    type="messages"
                )
                
                # ์ž…๋ ฅ ์˜์—ญ
                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
                )
        
        # ํผ ์ œ์ถœ ์ฒ˜๋ฆฌ
        def process_query(message, history, api_key):
            if not message.strip():
                return history
            
            updated_history, error = query_deepseek(message, history, api_key)
            
            if error:
                error_box.value = f"**์˜ค๋ฅ˜:** {error}"
                return history
            else:
                error_box.value = ""
                return updated_history
        
        # ๋ฒ„ํŠผ๊ณผ ๊ธฐ๋Šฅ ์—ฐ๊ฒฐ
        submit.click(
            process_query,
            inputs=[msg, chatbot, api_key],
            outputs=[chatbot]
        ).then(
            lambda: "",
            None,
            [msg]
        )
        
        # Enter ํ‚ค ์ œ์ถœ ํ—ˆ์šฉ
        msg.submit(
            process_query,
            inputs=[msg, chatbot, api_key],
            outputs=[chatbot]
        ).then(
            lambda: "",
            None,
            [msg]
        )
        
    return demo

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