Spaces:
Sleeping
Sleeping
Commit ·
697fa2b
1
Parent(s): d792ddc
Auto-update via watch script
Browse files
app.py
CHANGED
|
@@ -162,40 +162,44 @@ def respond(message, history, system_prompt, max_tokens, temperature, disable_th
|
|
| 162 |
prompt_parts.append(f"<|user|>\n{img_tags}{user_text}<|end|>\n<|assistant|>\n")
|
| 163 |
prompt = "".join(prompt_parts)
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
else:
|
| 182 |
-
params.input_ids = inputs
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
partial += decoded
|
| 198 |
-
yield partial
|
| 199 |
else:
|
| 200 |
# Qwen modes
|
| 201 |
llm = _load_llm(mode)
|
|
|
|
| 162 |
prompt_parts.append(f"<|user|>\n{img_tags}{user_text}<|end|>\n<|assistant|>\n")
|
| 163 |
prompt = "".join(prompt_parts)
|
| 164 |
|
| 165 |
+
try:
|
| 166 |
+
# Prepare inputs
|
| 167 |
+
if image_paths:
|
| 168 |
+
print(f"[inference] Processing prompt with {len(image_paths)} images...", flush=True)
|
| 169 |
+
images = og.Images.open(*image_paths)
|
| 170 |
+
inputs = processor(prompt, images=images)
|
| 171 |
+
else:
|
| 172 |
+
print("[inference] Processing text-only prompt for Phi-3...", flush=True)
|
| 173 |
+
try:
|
| 174 |
+
inputs = processor(prompt)
|
| 175 |
+
except Exception:
|
| 176 |
+
inputs = tokenizer.encode(prompt)
|
| 177 |
+
|
| 178 |
+
# Set up generator parameters
|
| 179 |
+
params = og.GeneratorParams(model)
|
| 180 |
+
if hasattr(inputs, 'set_inputs') or not isinstance(inputs, (list, tuple)):
|
| 181 |
+
params.set_inputs(inputs)
|
| 182 |
+
else:
|
| 183 |
+
params.input_ids = inputs
|
| 184 |
|
| 185 |
+
params.set_search_options(
|
| 186 |
+
max_length=int(max_tokens),
|
| 187 |
+
temperature=temperature
|
| 188 |
+
)
|
|
|
|
|
|
|
| 189 |
|
| 190 |
+
# Stream output
|
| 191 |
+
generator = og.Generator(model, params)
|
| 192 |
+
partial = ""
|
| 193 |
+
while not generator.is_done():
|
| 194 |
+
generator.compute_logits()
|
| 195 |
+
generator.generate_next_token()
|
| 196 |
+
new_token = generator.get_next_tokens()[0]
|
| 197 |
+
decoded = tokenizer.decode([new_token])
|
| 198 |
+
partial += decoded
|
| 199 |
+
yield partial
|
| 200 |
+
except Exception as e:
|
| 201 |
+
print(f"[inference] Error during Phi-3 generation: {e}", flush=True)
|
| 202 |
+
yield f"⚠️ **Error during model inference**: {str(e)}\n\n*This was caught gracefully to prevent the Space from crashing. Please try again or rephrase.*"
|
|
|
|
|
|
|
| 203 |
else:
|
| 204 |
# Qwen modes
|
| 205 |
llm = _load_llm(mode)
|