AMDF commited on
Commit
233a83b
Β·
verified Β·
1 Parent(s): fd80b0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +172 -44
app.py CHANGED
@@ -1,52 +1,180 @@
1
-
2
- import torch
3
- from transformers import AutoTokenizer, AutoModelForCausalLM
4
  import gradio as gr
 
 
5
 
6
- # λͺ¨λΈ 및 ν† ν¬λ‚˜μ΄μ € λ‘œλ“œ
7
- MODEL_ID = "upstage/solar-pro-2-128k-instruct"
8
- tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
9
- model = AutoModelForCausalLM.from_pretrained(
10
- MODEL_ID,
11
- torch_dtype=torch.float16,
12
- device_map="auto", # GPU μ‚¬μš© κ°€λŠ₯ μ‹œ μžλ™ ν• λ‹Ή
13
- offload_folder="/offload" # λŒ€κ·œλͺ¨ λͺ¨λΈ μ‹œ λ””μŠ€ν¬ μ˜€ν”„λ‘œλ“œ
14
- )
15
 
16
- # 챗봇 ν•¨μˆ˜ μ •μ˜
17
- def chatbot(user_message, chat_history):
18
- if not chat_history:
19
- chat_history = [
20
- {"role": "system", "content": "You are a helpful, respectful and honest assistant."}
21
- ]
22
- chat_history.append({"role": "user", "content": user_message})
23
-
24
- # λŒ€ν™” 기둝을 λͺ¨λΈμ˜ μž…λ ₯ ν˜•μ‹μœΌλ‘œ λ³€ν™˜
25
- prompt = tokenizer.apply_chat_template(chat_history, tokenize=False)
26
-
27
- # 응닡 생성
28
- inputs = tokenizer(prompt, return_tensors="pt")
29
- with torch.no_grad():
30
- outputs = model.generate(
31
- **inputs,
32
- max_new_tokens=512,
33
- do_sample=True,
34
- temperature=0.7
35
- )
 
 
 
 
 
 
 
36
 
37
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
38
- chat_history.append({"role": "assistant", "content": response})
39
- return response, chat_history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  # Gradio μΈν„°νŽ˜μ΄μŠ€ 생성
42
- iface = gr.ChatInterface(
43
- fn=chatbot,
44
- title="🫐 Solar Pro 2 Chatbot",
45
- description="Upstage의 Solar Pro 2 λͺ¨λΈκ³Ό λŒ€ν™”ν•΄λ³΄μ„Έμš”.",
46
- theme="compact",
47
- css="body { background-color: #f5f5f5 }"
48
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
- # 둜컬 μ„œλ²„ μ‹€ν–‰
51
  if __name__ == "__main__":
52
- iface.launch(server_name="0.0.0.0", share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import requests
3
+ import json
4
 
5
+ # Solar Pro2 API μ„€μ •
6
+ API_KEY = "up_SkyYXejeSSvB4tGZMfPTNe4uJ3yPO"
7
+ API_URL = "https://api.upstage.ai/v1/solar/chat/completions"
 
 
 
 
 
 
8
 
9
+ def chat_with_solar(message, history):
10
+ """Solar Pro2와 λŒ€ν™”ν•˜λŠ” ν•¨μˆ˜"""
11
+
12
+ # λŒ€ν™” νžˆμŠ€ν† λ¦¬λ₯Ό API ν˜•μ‹μœΌλ‘œ λ³€ν™˜
13
+ messages = []
14
+
15
+ # μ‹œμŠ€ν…œ λ©”μ‹œμ§€ μΆ”κ°€
16
+ messages.append({
17
+ "role": "system",
18
+ "content": "당신은 도움이 λ˜λŠ” AI μ–΄μ‹œμŠ€ν„΄νŠΈμž…λ‹ˆλ‹€. ν•œκ΅­μ–΄λ‘œ μΉœκ·Όν•˜κ³  μ •ν™•ν•˜κ²Œ λ‹΅λ³€ν•΄μ£Όμ„Έμš”."
19
+ })
20
+
21
+ # 이전 λŒ€ν™” νžˆμŠ€ν† λ¦¬ μΆ”κ°€
22
+ for human_msg, ai_msg in history:
23
+ messages.append({"role": "user", "content": human_msg})
24
+ messages.append({"role": "assistant", "content": ai_msg})
25
+
26
+ # ν˜„μž¬ μ‚¬μš©μž λ©”μ‹œμ§€ μΆ”κ°€
27
+ messages.append({"role": "user", "content": message})
28
+
29
+ # API μš”μ²­ 데이터
30
+ data = {
31
+ "model": "solar-pro-2",
32
+ "messages": messages,
33
+ "max_tokens": 1000,
34
+ "temperature": 0.7
35
+ }
36
 
37
+ # API 헀더
38
+ headers = {
39
+ "Authorization": f"Bearer {API_KEY}",
40
+ "Content-Type": "application/json"
41
+ }
42
+
43
+ try:
44
+ # API 호좜
45
+ response = requests.post(API_URL, headers=headers, json=data)
46
+ response.raise_for_status()
47
+
48
+ # 응닡 νŒŒμ‹±
49
+ result = response.json()
50
+ ai_response = result['choices'][0]['message']['content']
51
+
52
+ return ai_response
53
+
54
+ except requests.exceptions.RequestException as e:
55
+ return f"API μš”μ²­ 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
56
+ except KeyError as e:
57
+ return f"응닡 νŒŒμ‹± 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
58
+ except Exception as e:
59
+ return f"μ˜ˆμƒμΉ˜ λͺ»ν•œ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
60
 
61
  # Gradio μΈν„°νŽ˜μ΄μŠ€ 생성
62
+ def create_chatbot():
63
+ """챗봇 μΈν„°νŽ˜μ΄μŠ€ 생성"""
64
+
65
+ with gr.Blocks(
66
+ title="Solar Pro2 챗봇",
67
+ theme=gr.themes.Soft(),
68
+ css="""
69
+ .container {
70
+ max-width: 900px;
71
+ margin: auto;
72
+ }
73
+ .header {
74
+ text-align: center;
75
+ padding: 20px;
76
+ background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
77
+ color: white;
78
+ border-radius: 10px;
79
+ margin-bottom: 20px;
80
+ }
81
+ """
82
+ ) as demo:
83
+
84
+ # 헀더
85
+ gr.HTML("""
86
+ <div class="header">
87
+ <h1>🌞 Solar Pro2 챗봇</h1>
88
+ <p>Upstage의 Solar Pro2 λͺ¨λΈκ³Ό λŒ€ν™”ν•΄λ³΄μ„Έμš”!</p>
89
+ </div>
90
+ """)
91
+
92
+ # μ±„νŒ… μΈν„°νŽ˜μ΄μŠ€
93
+ chatbot = gr.Chatbot(
94
+ height=500,
95
+ bubble_full_width=False,
96
+ avatar_images=("πŸ‘€", "πŸ€–"),
97
+ show_copy_button=True
98
+ )
99
+
100
+ # μž…λ ₯ μ˜μ—­
101
+ with gr.Row():
102
+ msg = gr.Textbox(
103
+ placeholder="λ©”μ‹œμ§€λ₯Ό μž…λ ₯ν•˜μ„Έμš”...",
104
+ container=False,
105
+ scale=4,
106
+ show_label=False
107
+ )
108
+ submit_btn = gr.Button("전솑", scale=1, variant="primary")
109
+ clear_btn = gr.Button("λŒ€ν™” μ΄ˆκΈ°ν™”", scale=1)
110
+
111
+ # μ˜ˆμ‹œ μ§ˆλ¬Έλ“€
112
+ gr.Examples(
113
+ examples=[
114
+ "μ•ˆλ…•ν•˜μ„Έμš”! μžκΈ°μ†Œκ°œλ₯Ό ν•΄μ£Όμ„Έμš”.",
115
+ "파이썬으둜 κ°„λ‹¨ν•œ 계산기λ₯Ό λ§Œλ“œλŠ” 방법을 μ•Œλ €μ£Όμ„Έμš”.",
116
+ "였늘 날씨가 μ’‹λ„€μš”. μ–΄λ–€ ν™œλ™μ„ μΆ”μ²œν•˜μ‹œλ‚˜μš”?",
117
+ "AI에 λŒ€ν•΄ κ°„λ‹¨νžˆ μ„€λͺ…ν•΄μ£Όμ„Έμš”.",
118
+ "λ§›μžˆλŠ” ν•œκ΅­ μŒμ‹ λ ˆμ‹œν”Όλ₯Ό ν•˜λ‚˜ μΆ”μ²œν•΄μ£Όμ„Έμš”."
119
+ ],
120
+ inputs=msg,
121
+ label="μ˜ˆμ‹œ μ§ˆλ¬Έλ“€"
122
+ )
123
+
124
+ # 정보 μ„Ήμ…˜
125
+ with gr.Accordion("ℹ️ μ‚¬μš© 정보", open=False):
126
+ gr.Markdown("""
127
+ ### Solar Pro2 챗봇에 λŒ€ν•΄
128
+
129
+ - **λͺ¨λΈ**: Upstage Solar Pro2
130
+ - **κΈ°λŠ₯**: ν•œκ΅­μ–΄ λŒ€ν™”, 질문 λ‹΅λ³€, μ½”λ“œ 생성 λ“±
131
+ - **νŠΉμ§•**: 높은 μ„±λŠ₯의 ν•œκ΅­μ–΄ μ–Έμ–΄λͺ¨λΈ
132
+
133
+ ### μ‚¬μš© 방법
134
+ 1. μœ„μ˜ ν…μŠ€νŠΈ λ°•μŠ€μ— μ§ˆλ¬Έμ΄λ‚˜ λŒ€ν™”λ₯Ό μž…λ ₯ν•˜μ„Έμš”
135
+ 2. '전솑' λ²„νŠΌμ„ ν΄λ¦­ν•˜κ±°λ‚˜ Enterλ₯Ό λˆ„λ₯΄μ„Έμš”
136
+ 3. AI의 응닡을 ν™•μΈν•˜κ³  계속 λŒ€ν™”ν•˜μ„Έμš”
137
+
138
+ ### μ£Όμ˜μ‚¬ν•­
139
+ - API ν‚€κ°€ μ˜¬λ°”λ₯΄κ²Œ μ„€μ •λ˜μ–΄ μžˆλŠ”μ§€ ν™•μΈν•΄μ£Όμ„Έμš”
140
+ - 인터넷 연결이 ν•„μš”ν•©λ‹ˆλ‹€
141
+ """)
142
+
143
+ # 이벀트 ν•Έλ“€λŸ¬
144
+ def respond(message, chat_history):
145
+ if not message.strip():
146
+ return "", chat_history
147
+
148
+ # AI 응닡 생성
149
+ bot_message = chat_with_solar(message, chat_history)
150
+
151
+ # μ±„νŒ… νžˆμŠ€ν† λ¦¬μ— μΆ”κ°€
152
+ chat_history.append((message, bot_message))
153
+
154
+ return "", chat_history
155
+
156
+ # λ©”μ‹œμ§€ 전솑 이벀트
157
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
158
+ submit_btn.click(respond, [msg, chatbot], [msg, chatbot])
159
+
160
+ # λŒ€ν™” μ΄ˆκΈ°ν™” 이벀트
161
+ clear_btn.click(lambda: ([], ""), outputs=[chatbot, msg])
162
+
163
+ # μ—”ν„° ν‚€λ‘œ 전솑 κ°€λŠ₯ν•˜λ„λ‘ μ„€μ •
164
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
165
+
166
+ return demo
167
 
168
+ # 메인 μ‹€ν–‰
169
  if __name__ == "__main__":
170
+ # 챗봇 생성 및 μ‹€ν–‰
171
+ demo = create_chatbot()
172
+
173
+ # λ‘œμ»¬μ—μ„œ μ‹€ν–‰
174
+ demo.launch(
175
+ share=True, # 곡유 링크 생성
176
+ server_name="0.0.0.0", # λͺ¨λ“  IPμ—μ„œ μ ‘κ·Ό κ°€λŠ₯
177
+ server_port=7860, # 포트 μ„€μ •
178
+ show_error=True, # μ—λŸ¬ ν‘œμ‹œ
179
+ debug=True # 디버그 λͺ¨λ“œ
180
+ )