YUGOROU commited on
Commit ·
a1498a3
1
Parent(s): ceef7e0
revert: restore working AMD gateway config with new Droplet IP 165.245.137.45
Browse files- app.py +12 -66
- requirements.txt +1 -3
app.py
CHANGED
|
@@ -29,7 +29,6 @@ load_dotenv()
|
|
| 29 |
|
| 30 |
import gradio as gr
|
| 31 |
from openai import OpenAI
|
| 32 |
-
import spaces # ZeroGPU — provided by HF runtime
|
| 33 |
|
| 34 |
from pipeline.memory import build_system_prompt, get_context, save_session
|
| 35 |
from pipeline.parser import extract_facts_from_response, parse_structured_output
|
|
@@ -49,59 +48,10 @@ PATIENT_ID = os.getenv("PATIENT_ID", "demo_user_001")
|
|
| 49 |
PATIENT_NAME = os.getenv("PATIENT_NAME", "Margaret")
|
| 50 |
STT_URL = os.getenv("STT_URL", "")
|
| 51 |
TTS_URL = os.getenv("TTS_URL", "")
|
| 52 |
-
|
| 53 |
-
LUMI_GATEWAY_URL = os.getenv("LUMI_GATEWAY_URL", "")
|
| 54 |
-
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
| 55 |
|
| 56 |
llm = OpenAI(base_url=VLLM_BASE_URL, api_key=os.getenv("OPENAI_API_KEY", "not-required"))
|
| 57 |
|
| 58 |
-
# ---------------------------------------------------------------------------
|
| 59 |
-
# ZeroGPU — lazy model loading
|
| 60 |
-
# ---------------------------------------------------------------------------
|
| 61 |
-
import torch
|
| 62 |
-
|
| 63 |
-
_llm_model = None
|
| 64 |
-
_llm_tokenizer = None
|
| 65 |
-
|
| 66 |
-
def _ensure_llm_loaded():
|
| 67 |
-
global _llm_model, _llm_tokenizer
|
| 68 |
-
if _llm_model is not None:
|
| 69 |
-
return
|
| 70 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 71 |
-
print(f"[LLM] Loading {MODEL_NAME} ...")
|
| 72 |
-
_llm_tokenizer = AutoTokenizer.from_pretrained(
|
| 73 |
-
MODEL_NAME, token=HF_TOKEN or None
|
| 74 |
-
)
|
| 75 |
-
_llm_model = AutoModelForCausalLM.from_pretrained(
|
| 76 |
-
MODEL_NAME,
|
| 77 |
-
torch_dtype=torch.bfloat16,
|
| 78 |
-
device_map="cuda",
|
| 79 |
-
token=HF_TOKEN or None,
|
| 80 |
-
)
|
| 81 |
-
_llm_model.eval()
|
| 82 |
-
print("[LLM] Ready.")
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
@spaces.GPU
|
| 86 |
-
def _call_llm_gpu(messages: list[dict]) -> str:
|
| 87 |
-
"""Run LLM inference on ZeroGPU. Returns the full decoded response."""
|
| 88 |
-
_ensure_llm_loaded()
|
| 89 |
-
text = _llm_tokenizer.apply_chat_template(
|
| 90 |
-
messages, tokenize=False, add_generation_prompt=True
|
| 91 |
-
)
|
| 92 |
-
inputs = _llm_tokenizer(text, return_tensors="pt").to(_llm_model.device)
|
| 93 |
-
with torch.no_grad():
|
| 94 |
-
output = _llm_model.generate(
|
| 95 |
-
**inputs,
|
| 96 |
-
max_new_tokens=512,
|
| 97 |
-
temperature=0.7,
|
| 98 |
-
do_sample=True,
|
| 99 |
-
repetition_penalty=1.2,
|
| 100 |
-
pad_token_id=_llm_tokenizer.eos_token_id,
|
| 101 |
-
)
|
| 102 |
-
new_tokens = output[0][inputs["input_ids"].shape[-1]:]
|
| 103 |
-
return _llm_tokenizer.decode(new_tokens, skip_special_tokens=True)
|
| 104 |
-
|
| 105 |
PORTRAIT_DIR = Path(__file__).parent / "portraits"
|
| 106 |
|
| 107 |
def _load_portrait(profile_id: str) -> Image.Image | None:
|
|
@@ -133,17 +83,12 @@ def _get_system_prompt() -> str:
|
|
| 133 |
|
| 134 |
|
| 135 |
def _call_llm(messages: list[dict]) -> str:
|
| 136 |
-
# Priority 1: external AMD / custom gateway
|
| 137 |
if LUMI_GATEWAY_URL:
|
| 138 |
import httpx as _httpx
|
| 139 |
r = _httpx.post(f"{LUMI_GATEWAY_URL}/chat",
|
| 140 |
json={"messages": messages}, timeout=60.0)
|
| 141 |
r.raise_for_status()
|
| 142 |
return r.json()["response"]
|
| 143 |
-
# Priority 2: ZeroGPU local inference
|
| 144 |
-
return _call_llm_gpu(messages)
|
| 145 |
-
# Priority 3 (unreachable on ZeroGPU, kept for local dev reference):
|
| 146 |
-
# vLLM OpenAI-compatible fallback
|
| 147 |
resp = llm.chat.completions.create(
|
| 148 |
model=MODEL_NAME,
|
| 149 |
messages=messages,
|
|
@@ -155,16 +100,17 @@ def _call_llm(messages: list[dict]) -> str:
|
|
| 155 |
|
| 156 |
|
| 157 |
def _call_llm_stream(messages: list[dict]):
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
| 168 |
|
| 169 |
|
| 170 |
def _build_messages(
|
|
|
|
| 29 |
|
| 30 |
import gradio as gr
|
| 31 |
from openai import OpenAI
|
|
|
|
| 32 |
|
| 33 |
from pipeline.memory import build_system_prompt, get_context, save_session
|
| 34 |
from pipeline.parser import extract_facts_from_response, parse_structured_output
|
|
|
|
| 48 |
PATIENT_NAME = os.getenv("PATIENT_NAME", "Margaret")
|
| 49 |
STT_URL = os.getenv("STT_URL", "")
|
| 50 |
TTS_URL = os.getenv("TTS_URL", "")
|
| 51 |
+
LUMI_GATEWAY_URL = os.getenv("LUMI_GATEWAY_URL", "http://165.245.137.45:8080")
|
|
|
|
|
|
|
| 52 |
|
| 53 |
llm = OpenAI(base_url=VLLM_BASE_URL, api_key=os.getenv("OPENAI_API_KEY", "not-required"))
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
PORTRAIT_DIR = Path(__file__).parent / "portraits"
|
| 56 |
|
| 57 |
def _load_portrait(profile_id: str) -> Image.Image | None:
|
|
|
|
| 83 |
|
| 84 |
|
| 85 |
def _call_llm(messages: list[dict]) -> str:
|
|
|
|
| 86 |
if LUMI_GATEWAY_URL:
|
| 87 |
import httpx as _httpx
|
| 88 |
r = _httpx.post(f"{LUMI_GATEWAY_URL}/chat",
|
| 89 |
json={"messages": messages}, timeout=60.0)
|
| 90 |
r.raise_for_status()
|
| 91 |
return r.json()["response"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
resp = llm.chat.completions.create(
|
| 93 |
model=MODEL_NAME,
|
| 94 |
messages=messages,
|
|
|
|
| 100 |
|
| 101 |
|
| 102 |
def _call_llm_stream(messages: list[dict]):
|
| 103 |
+
for chunk in llm.chat.completions.create(
|
| 104 |
+
model=MODEL_NAME,
|
| 105 |
+
messages=messages,
|
| 106 |
+
temperature=0.7,
|
| 107 |
+
max_tokens=512,
|
| 108 |
+
stream=True,
|
| 109 |
+
extra_body={"repetition_penalty": 1.2},
|
| 110 |
+
):
|
| 111 |
+
delta = chunk.choices[0].delta.content
|
| 112 |
+
if delta:
|
| 113 |
+
yield delta
|
| 114 |
|
| 115 |
|
| 116 |
def _build_messages(
|
requirements.txt
CHANGED
|
@@ -4,6 +4,7 @@ chromadb>=0.5.0
|
|
| 4 |
sentence-transformers>=3.0.0
|
| 5 |
numpy>=1.26.0
|
| 6 |
python-dotenv
|
|
|
|
| 7 |
Pillow>=10.0.0
|
| 8 |
soundfile>=0.12.0
|
| 9 |
uvicorn>=0.29.0
|
|
@@ -11,6 +12,3 @@ fastapi>=0.110.0
|
|
| 11 |
gradio>=5.0.0
|
| 12 |
edge-tts
|
| 13 |
kokoro-onnx>=0.4.0
|
| 14 |
-
# ZeroGPU (spaces injected by HF build)
|
| 15 |
-
transformers>=4.45.0
|
| 16 |
-
accelerate>=0.33.0
|
|
|
|
| 4 |
sentence-transformers>=3.0.0
|
| 5 |
numpy>=1.26.0
|
| 6 |
python-dotenv
|
| 7 |
+
audioop-lts
|
| 8 |
Pillow>=10.0.0
|
| 9 |
soundfile>=0.12.0
|
| 10 |
uvicorn>=0.29.0
|
|
|
|
| 12 |
gradio>=5.0.0
|
| 13 |
edge-tts
|
| 14 |
kokoro-onnx>=0.4.0
|
|
|
|
|
|
|
|
|