Spaces:
Paused

File size: 4,339 Bytes
bc9f3e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
<script>

  import { t } from "../../lib/stores.svelte.js";

  import { fetchMmproj, uploadImage } from "../../lib/api.js";

  import "./Controls.css";

  import MiniSelect from "./MiniSelect.svelte";



  /** modelLabel/providerLabel come from the header pickers (read-only); the

   * image/rag bindables feed the pane's send payload and clear/save flows,

   * and mmprojLabel carries the selected projector back to ChatPane. The

   * mmproj choices + visibility are internal (only rendered here).

   * notify(msg, isErr) surfaces upload errors via the shared flash line.

   *

   * The component is split into two movable halves so the layout can keep

   * the image upload visible next to the composer while tucking the

   * projector select into the collapsed "Generation Settings" section:

   *   - imageOnly     — render ONLY the upload chip + preview (the vision

   *                     INPUT, not a setting). Skips the mmproj fetch.

   *   - projectorOnly — render ONLY the mmproj select (the projector

   *                     choice is a setting). This instance owns the

   *                     /api/mmproj fetch.

   * Default (neither) keeps the legacy combined layout. */

  let {

    modelLabel,

    providerLabel,

    imagePath = $bindable(),

    imagePreview = $bindable(),

    mmprojLabel = $bindable(),

    notify,

    imageOnly = false,

    projectorOnly = false,

  } = $props();



  let mmprojChoices = $state([]);

  let mmprojVisible = $state(false);

  // Guards stale /api/mmproj responses (the header pickers re-run the effect).

  let mmprojFetched = 0;



  async function onPickImage(e) {

    const file = e.currentTarget.files?.[0];

    e.currentTarget.value = ""; // allow re-picking the same file

    if (!file) return;

    if (!file.type.startsWith("image/")) {

      notify(t("web_upload_fail"), true);

      return;

    }

    if (file.size > 25 * 1024 * 1024) {

      notify(t("web_upload_fail"), true);

      return;

    }

    try {

      const r = await uploadImage(file);

      imagePath = r.path;

      if (imagePreview) URL.revokeObjectURL(imagePreview);

      imagePreview = URL.createObjectURL(file);

      notify("", false);

    } catch {

      notify(t("web_upload_fail"), true);

    }

  }



  function removeImage() {

    if (imagePreview) URL.revokeObjectURL(imagePreview);

    imagePreview = "";

    imagePath = "";

  }



  // Refresh mmproj choices whenever the header pickers change (the Gradio

  // vlm_dd.change parity — GGUF VLMs under llama.cpp providers only). Only

  // the projector-rendering instance fetches — an imageOnly duplicate would

  // fire a second request for the same data.

  $effect(() => {

    if (imageOnly) return;

    void modelLabel;

    void providerLabel;

    const req = ++mmprojFetched;

    mmprojVisible = false;

    mmprojChoices = [];

    if (!modelLabel || !providerLabel) return;

    fetchMmproj(modelLabel, providerLabel)

      .then((r) => {

        if (req !== mmprojFetched) return; // stale response

        mmprojVisible = !!r.visible;

        mmprojChoices = r.choices ?? [];

        mmprojLabel = r.saved ?? "";

      })

      .catch(() => {

        if (req !== mmprojFetched) return;

        mmprojVisible = false;

        mmprojChoices = [];

      });

  });

</script>

{#if !projectorOnly}
  <div class="image-row">
    <label class="chip upload-chip" data-tooltip={t("web_tt_upload_image")}>
      <input

        type="file"

        accept="image/*"

        onchange={onPickImage}

        hidden

      />
      {t("web_upload_image")}
    </label>
    {#if imagePreview}
      <div class="img-preview">
        <img src={imagePreview} alt="" />
        <button

          class="img-remove"

          onclick={removeImage}

          data-tooltip={t("web_tt_remove_image")}

          aria-label={t("web_tt_remove_image")}

        >×</button>
      </div>
    {/if}
  </div>
{/if}

{#if !imageOnly && mmprojVisible}
  <div class="theme-row">
    <label class="mini-field" data-tooltip={t("web_tt_mmproj")}>
      <span>{t("web_mmproj")}</span>
      <MiniSelect

        bind:value={mmprojLabel}

        options={mmprojChoices}

        emptyLabel="Auto"

        tip={t("web_tt_mmproj")}

      />
    </label>
  </div>
{/if}