File size: 11,235 Bytes
86da8ad
 
 
 
 
 
 
 
 
4161fbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86da8ad
 
 
 
 
 
 
 
 
 
 
 
 
4161fbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86da8ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4161fbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86da8ad
4161fbd
 
86da8ad
4161fbd
86da8ad
4161fbd
 
 
86da8ad
4161fbd
86da8ad
 
4161fbd
 
 
 
 
 
 
 
 
 
86da8ad
 
4161fbd
 
 
 
 
86da8ad
4161fbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86da8ad
4161fbd
86da8ad
4161fbd
 
 
 
 
 
 
 
 
 
 
 
 
 
86da8ad
 
4161fbd
 
 
 
 
 
 
 
86da8ad
 
4161fbd
 
86da8ad
 
 
 
4161fbd
86da8ad
4161fbd
 
 
 
 
 
86da8ad
 
4161fbd
 
 
 
 
 
 
 
86da8ad
 
 
 
 
4161fbd
 
86da8ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4161fbd
 
86da8ad
4161fbd
86da8ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import asyncio
import os
import re
import uuid
import threading
from typing import Generator

import gradio as gr

# Detect available GPUs
def get_num_gpus() -> int:
    """Detect the number of available GPUs."""
    try:
        import torch
        if torch.cuda.is_available():
            return torch.cuda.device_count()
    except ImportError:
        pass

    # Fallback: check CUDA_VISIBLE_DEVICES
    cuda_devices = os.environ.get("CUDA_VISIBLE_DEVICES", "")
    if cuda_devices:
        return len(cuda_devices.split(","))

    return 1  # Default to 1

NUM_GPUS = get_num_gpus()
MAX_PARALLEL_REQUESTS = 8  # UI supports up to 8 parallel inputs

print(f"Detected {NUM_GPUS} GPU(s)")

# Stop strings for generation
STOP_STRINGS = [
    "\nUser:", "\nユーザ:", "\nユーザー:",
    "\nAssistant:", "\nアシスタント:",
    "\nHuman:", "\nHuman:"
]
# Regex for post-processing cleanup
STOP_RE = re.compile(
    r"(?:^|\n)(?:User|ユーザ|ユーザー|Assistant|アシスタント)[::].*",
    re.MULTILINE
)

# Global vLLM engine (single instance, handles concurrent requests internally)
_engine = None
_engine_lock = threading.Lock()
_loop = None


def get_engine():
    """Get or create the global vLLM engine."""
    global _engine, _loop
    if _engine is None:
        with _engine_lock:
            if _engine is None:
                from vllm import AsyncLLMEngine, AsyncEngineArgs

                engine_args = AsyncEngineArgs(
                    model="EQUES/JPharmatron-7B-chat",
                    enforce_eager=True,
                    gpu_memory_utilization=0.85,
                    tensor_parallel_size=NUM_GPUS,  # Use all available GPUs
                )
                _engine = AsyncLLMEngine.from_engine_args(engine_args)
                _loop = asyncio.new_event_loop()
                asyncio.set_event_loop(_loop)
    return _engine, _loop


def build_prompt(user_input: str, mode: list[str]) -> str:
    """Build the prompt with system instructions and mode settings."""
    base_prompt = "あなたは製薬に関する専門家です。製薬に関するユーザーの質問に親切に回答してください。参照した文献を回答の末尾に常に提示してください。\n"

    if "製薬の専門家" in mode:
        base_prompt += "あなたは製薬に関する専門家です。製薬に関するユーザーの質問に親切に回答してください。参照した文献は常に提示してください。\n"
    if "国際基準に準拠" in mode:
        base_prompt += "回答に際して、国際基準に準拠してください。\n"
    if "具体的な手順" in mode:
        base_prompt += "回答には具体的な作業手順を含めてください。\n"

    base_prompt += f"ユーザー: {user_input}\nアシスタント:"
    return base_prompt


async def astream_generate(engine, prompt: str, request_id: str):
    """Async generator that streams tokens from vLLM."""
    from vllm import SamplingParams

    params = SamplingParams(
        temperature=0.0,
        max_tokens=4096,
        repetition_penalty=1.2,
        stop=STOP_STRINGS,
    )

    previous_text = ""
    async for out in engine.generate(prompt, params, request_id=request_id):
        full_text = out.outputs[0].text

        # Check for stop patterns that might have leaked through
        m = STOP_RE.search(full_text)
        if m:
            cut = m.start()
            chunk = full_text[len(previous_text):cut]
            if chunk:
                yield chunk
            break

        chunk = full_text[len(previous_text):]
        previous_text = full_text
        if chunk:
            yield chunk


async def run_parallel_async(prompts: list[str], mode: list[str]):
    """
    Run multiple prompts in parallel using vLLM's continuous batching.
    Yields (slot_id, accumulated_text) tuples as tokens arrive.
    """
    engine, _ = get_engine()

    # Build full prompts and track active slots
    active_slots = {}
    results = [""] * MAX_PARALLEL_REQUESTS

    for i, prompt in enumerate(prompts):
        if prompt and prompt.strip():
            full_prompt = build_prompt(prompt.strip(), mode)
            request_id = f"req_{i}_{uuid.uuid4().hex[:8]}"
            active_slots[i] = {
                "request_id": request_id,
                "prompt": full_prompt,
                "generator": None,
                "done": False,
            }

    if not active_slots:
        yield results
        return

    # Start all generators
    for slot_id, slot_info in active_slots.items():
        slot_info["generator"] = astream_generate(
            engine, slot_info["prompt"], slot_info["request_id"]
        )

    # Poll all generators and yield updates
    while any(not slot["done"] for slot in active_slots.values()):
        for slot_id, slot_info in active_slots.items():
            if slot_info["done"]:
                continue

            try:
                # Try to get next chunk with a small timeout
                chunk = await asyncio.wait_for(
                    slot_info["generator"].__anext__(),
                    timeout=0.05
                )
                results[slot_id] += chunk
            except StopAsyncIteration:
                slot_info["done"] = True
            except asyncio.TimeoutError:
                pass  # No data ready, continue to next slot

        yield results


def respond_parallel(
    prompt0: str, prompt1: str, prompt2: str, prompt3: str,
    prompt4: str, prompt5: str, prompt6: str, prompt7: str,
    mode: list[str]
) -> Generator:
    """
    Process up to 8 prompts in parallel using vLLM's continuous batching.
    """
    prompts = [prompt0, prompt1, prompt2, prompt3, prompt4, prompt5, prompt6, prompt7]
    _, loop = get_engine()

    async def run():
        async for results in run_parallel_async(prompts, mode):
            yield tuple(results)

    # Run the async generator in the event loop
    agen = run()
    try:
        while True:
            results = loop.run_until_complete(agen.__anext__())
            yield results
    except StopAsyncIteration:
        return


def respond_single(slot_id: int, prompt: str, mode: list[str]) -> Generator:
    """Process a single prompt."""
    if not prompt or not prompt.strip():
        yield ""
        return

    engine, loop = get_engine()
    full_prompt = build_prompt(prompt.strip(), mode)
    request_id = f"single_{slot_id}_{uuid.uuid4().hex[:8]}"

    async def run():
        result = ""
        async for chunk in astream_generate(engine, full_prompt, request_id):
            result += chunk
            yield result

    agen = run()
    try:
        while True:
            result = loop.run_until_complete(agen.__anext__())
            yield result
    except StopAsyncIteration:
        return


# Build the Gradio interface
with gr.Blocks(title="JPharmatron Parallel Chat") as demo:
    gr.Markdown("# 💊 JPharmatron - Parallel Request Processing")
    gr.Markdown(
        f"Enter up to {MAX_PARALLEL_REQUESTS} prompts and process them simultaneously. "
        f"Using {NUM_GPUS} GPU(s) with vLLM continuous batching."
    )

    # Mode selection
    mode = gr.CheckboxGroup(
        label="モード (Mode)",
        choices=["製薬の専門家", "国際基準に準拠", "具体的な手順"],
        value=[],
    )

    # Preset examples
    gr.Markdown("### 🔧 Presets (click to copy)")
    preset_list = [
        "グレープフルーツと薬を一緒に飲んじゃだめなんですか?",
        "新薬の臨床試験(Phase I〜III)の概要を、具体例つきで簡単に教えて。",
        "ジェネリック医薬品が承認されるまでの流れを、タイムラインで解説して。",
        "抗生物質の作用機序と耐性菌について説明してください。",
        "COVID-19ワクチンの開発プロセスを教えてください。",
        "薬物相互作用の主なメカニズムを教えてください。",
        "バイオシミラーと先発医薬品の違いは何ですか?",
        "製薬企業のGMP(Good Manufacturing Practice)について説明してください。",
    ]

    # Input section
    gr.Markdown("### 📝 Input Prompts")
    with gr.Row():
        with gr.Column():
            input_boxes = []
            for i in range(4):
                tb = gr.Textbox(
                    label=f"Prompt {i+1}",
                    placeholder=f"Enter prompt {i+1}...",
                    lines=3
                )
                input_boxes.append(tb)
        with gr.Column():
            for i in range(4, 8):
                tb = gr.Textbox(
                    label=f"Prompt {i+1}",
                    placeholder=f"Enter prompt {i+1}...",
                    lines=3
                )
                input_boxes.append(tb)

    # Examples that fill multiple boxes
    gr.Examples(
        examples=[preset_list[:4], preset_list[4:]],
        inputs=input_boxes[:4],
        label="Fill first 4 prompts with presets"
    )

    # Control buttons
    with gr.Row():
        run_all_btn = gr.Button("🚀 Run All in Parallel", variant="primary", scale=2)
        clear_inputs_btn = gr.Button("🗑️ Clear Inputs", scale=1)
        clear_outputs_btn = gr.Button("🗑️ Clear Outputs", scale=1)

    # Output section
    gr.Markdown("### 📤 Streaming Outputs")
    with gr.Row():
        with gr.Column():
            output_boxes = []
            for i in range(4):
                tb = gr.Textbox(
                    label=f"Response {i+1}",
                    lines=10,
                    interactive=False,
                    show_copy_button=True
                )
                output_boxes.append(tb)
        with gr.Column():
            for i in range(4, 8):
                tb = gr.Textbox(
                    label=f"Response {i+1}",
                    lines=10,
                    interactive=False,
                    show_copy_button=True
                )
                output_boxes.append(tb)

    # Wire up the "Run All" button
    run_all_btn.click(
        fn=respond_parallel,
        inputs=input_boxes + [mode],
        outputs=output_boxes
    )

    # Clear buttons
    clear_inputs_btn.click(
        fn=lambda: tuple([""] * 8),
        inputs=None,
        outputs=input_boxes
    )
    clear_outputs_btn.click(
        fn=lambda: tuple([""] * 8),
        inputs=None,
        outputs=output_boxes
    )

    # Individual run buttons for each slot
    gr.Markdown("### 🎯 Run Individual Prompts")
    with gr.Row():
        for i in range(8):
            btn = gr.Button(f"Run #{i+1}", size="sm")
            # Create closure to capture slot_id
            def make_single_handler(slot_id):
                def handler(prompt, mode):
                    yield from respond_single(slot_id, prompt, mode)
                return handler
            btn.click(
                fn=make_single_handler(i),
                inputs=[input_boxes[i], mode],
                outputs=[output_boxes[i]]
            )


def main():
    """Entry point for the application."""
    demo.queue()
    demo.launch()


if __name__ == "__main__":
    main()