Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -99,56 +99,56 @@ def respond(age, gender, height, weight,
|
|
| 99 |
f"- Plasma (PV): {pv} mL"
|
| 100 |
)
|
| 101 |
|
| 102 |
-
|
| 103 |
-
try:
|
| 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 |
-
except Exception as e:
|
| 151 |
-
|
| 152 |
|
| 153 |
|
| 154 |
|
|
|
|
| 99 |
f"- Plasma (PV): {pv} mL"
|
| 100 |
)
|
| 101 |
|
| 102 |
+
# --- Model Inference + Streaming ---
|
| 103 |
+
try:
|
| 104 |
+
messages = [
|
| 105 |
+
{"role": "system", "content": system_message},
|
| 106 |
+
{"role": "user", "content": user_message}
|
| 107 |
+
]
|
| 108 |
+
|
| 109 |
+
# Tokenize messages
|
| 110 |
+
encodings = tokenizer.apply_chat_template(
|
| 111 |
+
messages,
|
| 112 |
+
return_tensors="pt",
|
| 113 |
+
add_special_tokens=True,
|
| 114 |
+
padding=True,
|
| 115 |
+
truncation=True
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
input_ids = encodings["input_ids"].to(model.device)
|
| 119 |
+
attention_mask = encodings["attention_mask"].to(model.device) # β
Correct attention mask
|
| 120 |
+
|
| 121 |
+
# Streamer for capturing tokens
|
| 122 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 123 |
+
|
| 124 |
+
generation_kwargs = dict(
|
| 125 |
+
input_ids=input_ids,
|
| 126 |
+
attention_mask=attention_mask,
|
| 127 |
+
max_new_tokens=2000,
|
| 128 |
+
temperature=0.3,
|
| 129 |
+
top_p=0.9,
|
| 130 |
+
do_sample=True,
|
| 131 |
+
streamer=streamer,
|
| 132 |
+
pad_token_id=tokenizer.pad_token_id
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# Run model in separate thread
|
| 136 |
+
thread = threading.Thread(target=model.generate, kwargs=generation_kwargs)
|
| 137 |
+
thread.start()
|
| 138 |
+
|
| 139 |
+
# Collect tokens into full response
|
| 140 |
+
reply = ""
|
| 141 |
+
for token in streamer:
|
| 142 |
+
reply += token
|
| 143 |
+
|
| 144 |
+
# Convert Markdown β HTML
|
| 145 |
+
html_output = markdown.markdown(
|
| 146 |
+
reply,
|
| 147 |
+
extensions=["tables", "fenced_code", "nl2br"]
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
except Exception as e:
|
| 151 |
+
html_output = f"<p style='color:red;'>β οΈ Error: {str(e)}</p>"
|
| 152 |
|
| 153 |
|
| 154 |
|