openfree commited on
Commit
070daeb
ยท
verified ยท
1 Parent(s): 4121689

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -12
app.py CHANGED
@@ -6,15 +6,25 @@ import spaces
6
  import transformers
7
  from transformers import pipeline
8
 
9
- # ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ € ๋กœ๋”ฉ
10
- model_name = "Qwen/Qwen2-1.5B-Instruct"
11
- if gr.NO_RELOAD:
12
- pipe = pipeline(
13
- "text-generation",
14
- model=model_name,
15
- device_map="auto",
16
- torch_dtype="auto",
17
- )
 
 
 
 
 
 
 
 
 
 
18
 
19
  # ์ตœ์ข… ๋‹ต๋ณ€์„ ๊ฐ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ๋งˆ์ปค
20
  ANSWER_MARKER = "**๋‹ต๋ณ€**"
@@ -74,6 +84,27 @@ def rebuild_messages(history: list):
74
  return messages
75
 
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  @spaces.GPU
78
  def bot(
79
  history: list,
@@ -83,6 +114,18 @@ def bot(
83
  temperature: float,
84
  ):
85
  """๋ชจ๋ธ์ด ์งˆ๋ฌธ์— ๋‹ต๋ณ€ํ•˜๋„๋ก ํ•˜๊ธฐ"""
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
  # ๋‚˜์ค‘์— ์Šค๋ ˆ๋“œ์—์„œ ํ† ํฐ์„ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•จ
88
  streamer = transformers.TextIteratorStreamer(
@@ -140,10 +183,16 @@ def bot(
140
  yield history
141
 
142
 
143
- with gr.Blocks(fill_height=True, title="๋ชจ๋“  LLM ๋ชจ๋ธ์— ์ถ”๋ก  ๋Šฅ๋ ฅ ๋ถ€์—ฌํ•˜๊ธฐ") as demo:
 
 
 
 
 
 
144
  with gr.Row(scale=1):
145
  with gr.Column(scale=5):
146
-
147
  chatbot = gr.Chatbot(
148
  scale=1,
149
  type="messages",
@@ -156,7 +205,20 @@ with gr.Blocks(fill_height=True, title="๋ชจ๋“  LLM ๋ชจ๋ธ์— ์ถ”๋ก  ๋Šฅ๋ ฅ ๋ถ€
156
  placeholder="์—ฌ๊ธฐ์— ์งˆ๋ฌธ์„ ์ž…๋ ฅํ•˜์„ธ์š”.",
157
  autofocus=True,
158
  )
 
159
  with gr.Column(scale=1):
 
 
 
 
 
 
 
 
 
 
 
 
160
  gr.Markdown("""## ๋งค๊ฐœ๋ณ€์ˆ˜ ์กฐ์ •""")
161
  num_tokens = gr.Slider(
162
  50,
@@ -176,7 +238,18 @@ with gr.Blocks(fill_height=True, title="๋ชจ๋“  LLM ๋ชจ๋ธ์— ์ถ”๋ก  ๋Šฅ๋ ฅ ๋ถ€
176
  )
177
  do_sample = gr.Checkbox(True, label="์ƒ˜ํ”Œ๋ง ์‚ฌ์šฉ")
178
  temperature = gr.Slider(0.1, 1.0, 0.7, step=0.1, label="์˜จ๋„")
179
-
 
 
 
 
 
 
 
 
 
 
 
180
 
181
  # ์‚ฌ์šฉ์ž๊ฐ€ ๋ฉ”์‹œ์ง€๋ฅผ ์ œ์ถœํ•˜๋ฉด ๋ด‡์ด ์‘๋‹ตํ•ฉ๋‹ˆ๋‹ค
182
  msg.submit(
 
6
  import transformers
7
  from transformers import pipeline
8
 
9
+ # ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋ธ ๋ชฉ๋ก
10
+ available_models = {
11
+ "Qwen/Qwen2-1.5B-Instruct": "Qwen 2 (1.5B)",
12
+ "Qwen/Qwen2.5-72B-Instruct": "Qwen 2.5 (72B)",
13
+ "Hermes-3-Llama-3.1-8B": "Hermes 3 Llama 3.1 (8B)",
14
+ "Qwen/Qwen2.5-Coder-32B-Instruct": "Qwen 2.5 Coder (32B)",
15
+ "nvidia/Llama-3.1-Nemotron-70B-Instruct-HF": "Nvidia Nemotron (70B)",
16
+ "CohereForAI/c4ai-command-r-plus-08-2024": "Cohere Command R+ (08/2024)",
17
+ "mistralai/Mistral-Small-3.1-24B-Instruct-2503": "Mistral Small 3.1 (24B)",
18
+ "google/gemma-3-27b-it": "Google Gemma 3 (27B)",
19
+ "gemma-3-27b-it-abliterated": "Gemma 3 Abliterated (27B)",
20
+ "nvidia/Llama-3.1-Nemotron-Nano-8B-v1": "Nvidia Nemotron Nano (8B)",
21
+ "bartowski/mistralai_Mistral-Small-3.1-24B-Instruct-2503-GGUF": "Mistral Small GGUF (24B)",
22
+ "CohereForAI/c4ai-command-a-03-2025": "Cohere Command A (03/2025)",
23
+ "open-r1/OlympicCoder-32B": "Olympic Coder (32B)"
24
+ }
25
+
26
+ # ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ € ๋กœ๋”ฉ์„ ์œ„ํ•œ ์ „์—ญ ๋ณ€์ˆ˜
27
+ pipe = None
28
 
29
  # ์ตœ์ข… ๋‹ต๋ณ€์„ ๊ฐ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ๋งˆ์ปค
30
  ANSWER_MARKER = "**๋‹ต๋ณ€**"
 
84
  return messages
85
 
86
 
87
+ def load_model(model_names):
88
+ """์„ ํƒ๋œ ๋ชจ๋ธ ์ด๋ฆ„์— ๋”ฐ๋ผ ๋ชจ๋ธ ๋กœ๋“œ"""
89
+ global pipe
90
+
91
+ # ๋ชจ๋ธ์ด ์„ ํƒ๋˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’ ์ง€์ •
92
+ if not model_names:
93
+ model_name = "Qwen/Qwen2-1.5B-Instruct"
94
+ else:
95
+ # ์ฒซ ๋ฒˆ์งธ ์„ ํƒ๋œ ๋ชจ๋ธ ์‚ฌ์šฉ (๋‚˜์ค‘์— ์—ฌ๋Ÿฌ ๋ชจ๋ธ ์•™์ƒ๋ธ”๋กœ ํ™•์žฅ ๊ฐ€๋Šฅ)
96
+ model_name = model_names[0]
97
+
98
+ pipe = pipeline(
99
+ "text-generation",
100
+ model=model_name,
101
+ device_map="auto",
102
+ torch_dtype="auto",
103
+ )
104
+
105
+ return f"๋ชจ๋ธ '{model_name}'์ด(๊ฐ€) ๋กœ๋“œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
106
+
107
+
108
  @spaces.GPU
109
  def bot(
110
  history: list,
 
114
  temperature: float,
115
  ):
116
  """๋ชจ๋ธ์ด ์งˆ๋ฌธ์— ๋‹ต๋ณ€ํ•˜๋„๋ก ํ•˜๊ธฐ"""
117
+ global pipe
118
+
119
+ # ๋ชจ๋ธ์ด ๋กœ๋“œ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ํ‘œ์‹œ
120
+ if pipe is None:
121
+ history.append(
122
+ gr.ChatMessage(
123
+ role="assistant",
124
+ content="๋ชจ๋ธ์ด ๋กœ๋“œ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜ ์ด์ƒ์˜ ๋ชจ๋ธ์„ ์„ ํƒํ•ด ์ฃผ์„ธ์š”.",
125
+ )
126
+ )
127
+ yield history
128
+ return
129
 
130
  # ๋‚˜์ค‘์— ์Šค๋ ˆ๋“œ์—์„œ ํ† ํฐ์„ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•จ
131
  streamer = transformers.TextIteratorStreamer(
 
183
  yield history
184
 
185
 
186
+ with gr.Blocks(fill_height=True, title="ThinkFlow - Step-by-step Reasoning Service") as demo:
187
+ # ์ƒ๋‹จ์— ํƒ€์ดํ‹€๊ณผ ์„ค๋ช… ์ถ”๊ฐ€
188
+ gr.Markdown("""
189
+ # ThinkFlow
190
+ ## A thought amplification service that implants step-by-step reasoning abilities into LLMs without model modification
191
+ """)
192
+
193
  with gr.Row(scale=1):
194
  with gr.Column(scale=5):
195
+ # ์ฑ„ํŒ… ์ธํ„ฐํŽ˜์ด์Šค
196
  chatbot = gr.Chatbot(
197
  scale=1,
198
  type="messages",
 
205
  placeholder="์—ฌ๊ธฐ์— ์งˆ๋ฌธ์„ ์ž…๋ ฅํ•˜์„ธ์š”.",
206
  autofocus=True,
207
  )
208
+
209
  with gr.Column(scale=1):
210
+ # ๋ชจ๋ธ ์„ ํƒ ์„น์…˜ ์ถ”๊ฐ€
211
+ gr.Markdown("""## ๋ชจ๋ธ ์„ ํƒ""")
212
+ model_selector = gr.CheckboxGroup(
213
+ choices=list(available_models.values()),
214
+ value=[available_models["Qwen/Qwen2-1.5B-Instruct"]], # ๊ธฐ๋ณธ๊ฐ’
215
+ label="์‚ฌ์šฉํ•  LLM ๋ชจ๋ธ ์„ ํƒ (๋ณต์ˆ˜ ์„ ํƒ ๊ฐ€๋Šฅ)",
216
+ )
217
+
218
+ # ๋ชจ๋ธ ๋กœ๋“œ ๋ฒ„ํŠผ
219
+ load_model_btn = gr.Button("๋ชจ๋ธ ๋กœ๋“œ")
220
+ model_status = gr.Textbox(label="๋ชจ๋ธ ์ƒํƒœ", interactive=False)
221
+
222
  gr.Markdown("""## ๋งค๊ฐœ๋ณ€์ˆ˜ ์กฐ์ •""")
223
  num_tokens = gr.Slider(
224
  50,
 
238
  )
239
  do_sample = gr.Checkbox(True, label="์ƒ˜ํ”Œ๋ง ์‚ฌ์šฉ")
240
  temperature = gr.Slider(0.1, 1.0, 0.7, step=0.1, label="์˜จ๋„")
241
+
242
+ # ์„ ํƒ๋œ ๋ชจ๋ธ ๋กœ๋“œ ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
243
+ def get_model_names(selected_models):
244
+ # ํ‘œ์‹œ ์ด๋ฆ„์—์„œ ์›๋ž˜ ๋ชจ๋ธ ์ด๋ฆ„์œผ๋กœ ๋ณ€ํ™˜
245
+ inverse_map = {v: k for k, v in available_models.items()}
246
+ return [inverse_map[model] for model in selected_models]
247
+
248
+ load_model_btn.click(
249
+ lambda selected: load_model(get_model_names(selected)),
250
+ inputs=[model_selector],
251
+ outputs=[model_status]
252
+ )
253
 
254
  # ์‚ฌ์šฉ์ž๊ฐ€ ๋ฉ”์‹œ์ง€๋ฅผ ์ œ์ถœํ•˜๋ฉด ๋ด‡์ด ์‘๋‹ตํ•ฉ๋‹ˆ๋‹ค
255
  msg.submit(