Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ import base64
|
|
5 |
from PIL import Image, ImageDraw, ImageFont
|
6 |
import io
|
7 |
|
8 |
-
def process_with_openrouter(image, prompt, api_key, model="
|
9 |
"""Process image with OpenRouter API for object detection"""
|
10 |
if not api_key:
|
11 |
return "Please enter your OpenRouter API key", "error"
|
@@ -220,8 +220,20 @@ def create_interface():
|
|
220 |
type="password"
|
221 |
)
|
222 |
|
223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
choices=[
|
|
|
|
|
|
|
|
|
225 |
"google/gemini-2.5-pro",
|
226 |
"google/gemini-1.5-pro",
|
227 |
"google/gemini-1.5-flash",
|
@@ -229,8 +241,17 @@ def create_interface():
|
|
229 |
"openai/gpt-4o",
|
230 |
"openai/gpt-4o-mini"
|
231 |
],
|
232 |
-
value="
|
233 |
-
label="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
234 |
)
|
235 |
|
236 |
detection_mode = gr.Radio(
|
@@ -303,7 +324,20 @@ concrete spalling deeper than 1cm""",
|
|
303 |
lines=3
|
304 |
)
|
305 |
|
306 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
307 |
if not api_key_val:
|
308 |
return None, "β Please enter your OpenRouter API key", "No API key provided"
|
309 |
|
@@ -313,10 +347,18 @@ concrete spalling deeper than 1cm""",
|
|
313 |
if not detailed_specs or not detailed_specs.strip():
|
314 |
return None, "β Please enter at least one detailed specification", "No specifications provided"
|
315 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
316 |
try:
|
317 |
prompt = create_detection_prompt(detailed_specs, conf_threshold, mode_val)
|
318 |
|
319 |
-
result, error = process_with_openrouter(image, prompt, api_key_val,
|
320 |
|
321 |
if error:
|
322 |
return None, f"β Error: {result}", "Detection failed"
|
@@ -334,7 +376,7 @@ concrete spalling deeper than 1cm""",
|
|
334 |
"exclude": "Excluding objects matching detailed specifications"
|
335 |
}
|
336 |
|
337 |
-
summary_text = f"β
{mode_descriptions.get(mode_val, 'Detection')} - Found {len(filtered_detections)} objects"
|
338 |
|
339 |
if filtered_detections:
|
340 |
# Group by class and show details
|
@@ -374,7 +416,7 @@ concrete spalling deeper than 1cm""",
|
|
374 |
|
375 |
detect_btn.click(
|
376 |
process_detection,
|
377 |
-
inputs=[image_input, detailed_specifications, confidence_threshold, api_key,
|
378 |
outputs=[annotated_image, detection_results, detection_summary]
|
379 |
)
|
380 |
|
@@ -384,6 +426,15 @@ concrete spalling deeper than 1cm""",
|
|
384 |
- **Include Mode**: Detect your specified criteria plus any other objects found
|
385 |
- **Exclude Mode**: Detect everything except objects matching your specifications
|
386 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
387 |
### Example Detailed Specifications:
|
388 |
```
|
389 |
crack width more than 2mm
|
|
|
5 |
from PIL import Image, ImageDraw, ImageFont
|
6 |
import io
|
7 |
|
8 |
+
def process_with_openrouter(image, prompt, api_key, model="openai/gpt-5-chat", temperature=0.5):
|
9 |
"""Process image with OpenRouter API for object detection"""
|
10 |
if not api_key:
|
11 |
return "Please enter your OpenRouter API key", "error"
|
|
|
220 |
type="password"
|
221 |
)
|
222 |
|
223 |
+
with gr.Row():
|
224 |
+
use_preset = gr.Radio(
|
225 |
+
choices=["Preset Model", "Custom Model"],
|
226 |
+
value="Preset Model",
|
227 |
+
label="Model Selection",
|
228 |
+
info="Choose preset or enter custom OpenRouter model"
|
229 |
+
)
|
230 |
+
|
231 |
+
model_preset = gr.Dropdown(
|
232 |
choices=[
|
233 |
+
"openai/gpt-5-chat",
|
234 |
+
"openai/gpt-5-mini",
|
235 |
+
"anthropic/claude-opus-4.1",
|
236 |
+
"x-ai/grok-4",
|
237 |
"google/gemini-2.5-pro",
|
238 |
"google/gemini-1.5-pro",
|
239 |
"google/gemini-1.5-flash",
|
|
|
241 |
"openai/gpt-4o",
|
242 |
"openai/gpt-4o-mini"
|
243 |
],
|
244 |
+
value="openai/gpt-5-chat",
|
245 |
+
label="Preset Models",
|
246 |
+
info="Select from popular OpenRouter models",
|
247 |
+
visible=True
|
248 |
+
)
|
249 |
+
|
250 |
+
custom_model_input = gr.Textbox(
|
251 |
+
label="Custom Model ID",
|
252 |
+
placeholder="Enter any OpenRouter model ID (e.g., google/gemini-1.5-flash, anthropic/claude-3-haiku)",
|
253 |
+
visible=False,
|
254 |
+
info="Copy model IDs from openrouter.ai/models"
|
255 |
)
|
256 |
|
257 |
detection_mode = gr.Radio(
|
|
|
324 |
lines=3
|
325 |
)
|
326 |
|
327 |
+
# Show/hide model input based on selection
|
328 |
+
def update_model_visibility(use_preset_val):
|
329 |
+
if use_preset_val == "Custom Model":
|
330 |
+
return gr.update(visible=False), gr.update(visible=True)
|
331 |
+
else:
|
332 |
+
return gr.update(visible=True), gr.update(visible=False)
|
333 |
+
|
334 |
+
use_preset.change(
|
335 |
+
update_model_visibility,
|
336 |
+
inputs=[use_preset],
|
337 |
+
outputs=[model_preset, custom_model_input]
|
338 |
+
)
|
339 |
+
|
340 |
+
def process_detection(image, detailed_specs, conf_threshold, api_key_val, use_preset_val, model_preset_val, custom_model_val, temp_val, mode_val):
|
341 |
if not api_key_val:
|
342 |
return None, "β Please enter your OpenRouter API key", "No API key provided"
|
343 |
|
|
|
347 |
if not detailed_specs or not detailed_specs.strip():
|
348 |
return None, "β Please enter at least one detailed specification", "No specifications provided"
|
349 |
|
350 |
+
# Determine which model to use
|
351 |
+
if use_preset_val == "Custom Model":
|
352 |
+
if not custom_model_val or custom_model_val.strip() == "":
|
353 |
+
return None, "β Please enter a custom model ID", "Custom model required"
|
354 |
+
final_model = custom_model_val.strip()
|
355 |
+
else:
|
356 |
+
final_model = model_preset_val
|
357 |
+
|
358 |
try:
|
359 |
prompt = create_detection_prompt(detailed_specs, conf_threshold, mode_val)
|
360 |
|
361 |
+
result, error = process_with_openrouter(image, prompt, api_key_val, final_model, temp_val)
|
362 |
|
363 |
if error:
|
364 |
return None, f"β Error: {result}", "Detection failed"
|
|
|
376 |
"exclude": "Excluding objects matching detailed specifications"
|
377 |
}
|
378 |
|
379 |
+
summary_text = f"β
{mode_descriptions.get(mode_val, 'Detection')} - Found {len(filtered_detections)} objects\nπ€ Model: {final_model}"
|
380 |
|
381 |
if filtered_detections:
|
382 |
# Group by class and show details
|
|
|
416 |
|
417 |
detect_btn.click(
|
418 |
process_detection,
|
419 |
+
inputs=[image_input, detailed_specifications, confidence_threshold, api_key, use_preset, model_preset, custom_model_input, temperature, detection_mode],
|
420 |
outputs=[annotated_image, detection_results, detection_summary]
|
421 |
)
|
422 |
|
|
|
426 |
- **Include Mode**: Detect your specified criteria plus any other objects found
|
427 |
- **Exclude Mode**: Detect everything except objects matching your specifications
|
428 |
|
429 |
+
### π€ Model Selection
|
430 |
+
**Default Models (Recommended):**
|
431 |
+
- `openai/gpt-5-chat` - Latest GPT-5 with advanced vision capabilities (Default)
|
432 |
+
- `openai/gpt-5-mini` - Faster, efficient GPT-5 variant
|
433 |
+
- `anthropic/claude-opus-4.1` - Next-gen Claude with superior reasoning
|
434 |
+
- `x-ai/grok-4` - Advanced Grok model with detailed analysis
|
435 |
+
|
436 |
+
**Custom Models**: Enter any OpenRouter model ID from [openrouter.ai/models](https://openrouter.ai/models)
|
437 |
+
|
438 |
### Example Detailed Specifications:
|
439 |
```
|
440 |
crack width more than 2mm
|