File size: 3,311 Bytes
549f7f3
 
 
 
 
 
 
 
 
 
cf6bfde
549f7f3
 
 
 
 
 
 
cf6bfde
 
 
 
 
549f7f3
 
 
 
cf6bfde
549f7f3
 
 
 
 
cf6bfde
 
 
549f7f3
 
 
 
 
 
 
 
 
 
 
 
 
cf6bfde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
549f7f3
 
 
cf6bfde
 
 
 
 
 
 
549f7f3
 
cf6bfde
 
 
 
 
 
 
549f7f3
 
cf6bfde
 
549f7f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf6bfde
549f7f3
 
 
 
 
 
cf6bfde
 
549f7f3
 
cf6bfde
 
 
 
 
 
549f7f3
 
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
133
134
135
136
137
138
<svelte:options accessors={true} />

<script context="module" lang="ts">
  export { default as BaseExample } from "./Example.svelte";
</script>

<script lang="ts">
  import "./app.css";
  import type { Gradio } from "@gradio/utils";

  import { WebViewer, LogChannel } from "@rerun-io/web-viewer";
  import { onMount } from "svelte";

  import { Block } from "@gradio/atoms";
  import { StatusTracker } from "@gradio/statustracker";
  import type { FileData } from "@gradio/client";
  import type { LoadingStatus } from "@gradio/statustracker";

  interface BinaryStream {
    url: string;
    is_stream: boolean;
  }

  export let elem_id = "";
  export let elem_classes: string[] = [];
  export let visible = true;
  export let height: number | string = 640;
  export let value: null | BinaryStream | (FileData | string)[] = null;
  export let container = true;
  export let scale: number | null = null;
  export let min_width: number | undefined = undefined;
  export let loading_status: LoadingStatus;
  export let interactive: boolean;
  export let streaming: boolean;

  let old_value: null | BinaryStream | (FileData | string)[] = null;

  export let gradio: Gradio<{
    change: never;
    upload: never;
    clear: never;
    clear_status: LoadingStatus;
  }>;

  $: height = typeof height === "number" ? `${height}px` : height;

  let dragging: boolean;
  let rr: WebViewer;
  let ref: HTMLDivElement;
  let patched_loading_status: LoadingStatus;

  function try_load_value() {
    if (
      JSON.stringify(value) !== JSON.stringify(old_value) &&
      rr != undefined &&
      rr.ready
    ) {
      old_value = value;
      if (!Array.isArray(value)) {
        if (value.is_stream) {
          rr.open(value.url, { follow_if_http: true });
        } else {
          rr.open(value.url);
        }
      } else {
        for (const file of value) {
          if (typeof file !== "string") {
            if (file.url) {
              rr.open(file.url);
            }
          } else {
            rr.open(file);
          }
        }
      }
    }
  }

  onMount(() => {
    rr = new WebViewer();
    rr.start(undefined, ref, true).then(() => {
      try_load_value();
    });

    return () => {
      rr.stop();
    };
  });

  $: {
    patched_loading_status = loading_status;

    // In streaming mode, we want the UI to be interactive even while the model is generating
    // so set the status to complete.
    if (streaming && patched_loading_status?.status === "generating") {
      patched_loading_status.status = "complete";
    }
  }

  $: value, try_load_value();
</script>

{#if !interactive}
  <Block
    {visible}
    variant={"solid"}
    border_mode={dragging ? "focus" : "base"}
    padding={false}
    {elem_id}
    {elem_classes}
    allow_overflow={false}
    {container}
    {scale}
    {min_width}
  >
    <StatusTracker
      autoscroll={gradio.autoscroll}
      i18n={gradio.i18n}
      {...patched_loading_status}
      on:clear_status={() => gradio.dispatch("clear_status", loading_status)}
    />
    <div class="viewer" bind:this={ref} style:height />
  </Block>
{/if}

<style lang="scss">
  div.viewer {
    width: 100%;

    :global(> canvas) {
      position: initial !important;
      top: unset !important;
      width: 100%;
      height: 100%;
    }
  }
</style>