| |
| import os |
|
|
| import gradio as gr |
| from openai import OpenAI |
|
|
|
|
| VLLM_BASE_URL = os.getenv("VLLM_BASE_URL", "http://127.0.0.1:8000/v1") |
| VLLM_API_KEY = os.getenv("VLLM_API_KEY", "EMPTY") |
| DEFAULT_MODEL = os.getenv("VLLM_MODEL", "medgemma-4b-it") |
| CHATBOT_PORT = int(os.getenv("CHATBOT_PORT", "7860")) |
| ENABLE_SHARE = os.getenv("CHATBOT_SHARE", "false").strip().lower() in { |
| "1", |
| "true", |
| "yes", |
| } |
| SYSTEM_PROMPT = os.getenv( |
| "CHATBOT_SYSTEM_PROMPT", |
| "You are a helpful medical assistant. Be clear, safe, and concise.", |
| ) |
| GPT_STYLE_CSS = """ |
| body, .gradio-container { |
| background: #0b1020 !important; |
| color: #e5e7eb !important; |
| } |
| #app-shell { |
| max-width: 1400px !important; |
| margin: 0 auto !important; |
| } |
| #left-sidebar { |
| background: #111827 !important; |
| border: 1px solid #1f2937 !important; |
| border-radius: 16px !important; |
| padding: 16px !important; |
| min-height: 82vh !important; |
| } |
| #chat-panel { |
| background: #0f172a !important; |
| border: 1px solid #1f2937 !important; |
| border-radius: 16px !important; |
| padding: 8px 8px 0 8px !important; |
| min-height: 82vh !important; |
| } |
| .gpt-title { |
| font-size: 1.35rem; |
| font-weight: 700; |
| margin-bottom: 4px; |
| } |
| .gpt-subtitle { |
| color: #94a3b8; |
| margin-bottom: 12px; |
| } |
| .meta-chip { |
| display: inline-block; |
| font-size: 0.82rem; |
| color: #cbd5e1; |
| background: #1f2937; |
| border: 1px solid #334155; |
| border-radius: 999px; |
| padding: 5px 10px; |
| margin: 4px 8px 8px 0; |
| } |
| button.primary { |
| background: #2563eb !important; |
| } |
| footer { display: none !important; } |
| """ |
|
|
|
|
| def get_client() -> OpenAI: |
| return OpenAI(base_url=VLLM_BASE_URL, api_key=VLLM_API_KEY) |
|
|
|
|
| def normalize_content(content) -> str: |
| if content is None: |
| return "" |
| if isinstance(content, str): |
| return content |
| if isinstance(content, list): |
| parts = [] |
| for item in content: |
| if isinstance(item, dict): |
| if item.get("type") == "text" and item.get("text"): |
| parts.append(str(item["text"])) |
| elif item.get("content"): |
| parts.append(str(item["content"])) |
| else: |
| parts.append(str(item)) |
| else: |
| parts.append(str(item)) |
| return "\n".join([p for p in parts if p]).strip() |
| if isinstance(content, dict): |
| if content.get("text"): |
| return str(content["text"]) |
| if content.get("content"): |
| return str(content["content"]) |
| return str(content) |
|
|
|
|
| def list_backend_models() -> list[str]: |
| try: |
| models = get_client().models.list().data |
| return [m.id for m in models] or [DEFAULT_MODEL] |
| except Exception: |
| return [DEFAULT_MODEL] |
|
|
|
|
| AVAILABLE_MODELS = list_backend_models() |
| ACTIVE_MODEL = AVAILABLE_MODELS[0] |
|
|
|
|
| def chat_fn(message: str, history, model_id: str, temperature: float, max_tokens: int, system_prompt: str) -> str: |
| messages = [{"role": "system", "content": system_prompt}] |
| for item in history: |
| if isinstance(item, (list, tuple)) and len(item) == 2: |
| user_msg, assistant_msg = item |
| if user_msg: |
| messages.append({"role": "user", "content": normalize_content(user_msg)}) |
| if assistant_msg: |
| messages.append({"role": "assistant", "content": normalize_content(assistant_msg)}) |
| elif isinstance(item, dict): |
| role = item.get("role") |
| content = item.get("content") |
| if role in {"user", "assistant"} and content: |
| messages.append({"role": role, "content": normalize_content(content)}) |
| messages.append({"role": "user", "content": normalize_content(message)}) |
|
|
| response = get_client().chat.completions.create( |
| model=model_id, |
| messages=messages, |
| temperature=temperature, |
| max_tokens=max_tokens, |
| ) |
| return normalize_content(response.choices[0].message.content) |
|
|
|
|
| def main() -> None: |
| with gr.Blocks(title="MedGemma Chatbot") as demo: |
| with gr.Row(elem_id="app-shell"): |
| with gr.Column(scale=3, elem_id="left-sidebar"): |
| gr.HTML( |
| "<div class='gpt-title'>MedGemma Chat</div>" |
| "<div class='gpt-subtitle'>GPT-style dashboard for your vLLM backend.</div>" |
| ) |
| gr.HTML( |
| f"<span class='meta-chip'>Active model: {ACTIVE_MODEL}</span>" |
| f"<span class='meta-chip'>Endpoint: {VLLM_BASE_URL}</span>" |
| ) |
| gr.Markdown("### Settings") |
| model_dd = gr.Dropdown( |
| choices=AVAILABLE_MODELS, |
| value=ACTIVE_MODEL if ACTIVE_MODEL in AVAILABLE_MODELS else AVAILABLE_MODELS[0], |
| label="Model", |
| ) |
| temp_slider = gr.Slider(0.0, 1.2, value=0.2, step=0.05, label="Temperature") |
| token_slider = gr.Slider(64, 2048, value=512, step=32, label="Max response tokens") |
| prompt_box = gr.Textbox( |
| value=SYSTEM_PROMPT, |
| label="System prompt", |
| lines=4, |
| ) |
| gr.Markdown( |
| "### Prompt ideas\n" |
| "- Summarize this clinical note in simple language.\n" |
| "- List red flags that need emergency care.\n" |
| "- Explain this diagnosis for a patient handout." |
| ) |
| with gr.Column(scale=9, elem_id="chat-panel"): |
| gr.ChatInterface( |
| fn=chat_fn, |
| chatbot=gr.Chatbot(height=690), |
| additional_inputs=[model_dd, temp_slider, token_slider, prompt_box], |
| ) |
|
|
| demo.queue(default_concurrency_limit=32).launch( |
| server_name="0.0.0.0", |
| server_port=CHATBOT_PORT, |
| share=ENABLE_SHARE, |
| css=GPT_STYLE_CSS, |
| show_error=True, |
| ) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|