KasKniesmeijer commited on
Commit
12f098c
·
1 Parent(s): 1a07a93
Files changed (2) hide show
  1. app.py +1 -1
  2. src/main.js +83 -0
app.py CHANGED
@@ -112,7 +112,7 @@ iface = gr.Interface(
112
  ],
113
  outputs="text",
114
  title="FAAM-demo | Vision Language Model | SmolVLM",
115
- description="Welcome to the FAAM-demo!",
116
  theme=custom_citrus,
117
  )
118
 
 
112
  ],
113
  outputs="text",
114
  title="FAAM-demo | Vision Language Model | SmolVLM",
115
+ description="Upload an image and ask questions about it",
116
  theme=custom_citrus,
117
  )
118
 
src/main.js CHANGED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ async function initializeWebGPU() {
2
+ const canvas = document.getElementById("webgpu-canvas");
3
+
4
+ if (!navigator.gpu) {
5
+ document.body.innerHTML = "<p>Your browser does not support WebGPU.</p>";
6
+ return;
7
+ }
8
+
9
+ console.log("WebGPU is supported.");
10
+
11
+ const adapter = await navigator.gpu.requestAdapter();
12
+ if (!adapter) {
13
+ console.error("Failed to get GPU adapter.");
14
+ return;
15
+ }
16
+ console.log("GPU adapter obtained.");
17
+
18
+ const device = await adapter.requestDevice();
19
+ if (!device) {
20
+ console.error("Failed to get GPU device.");
21
+ return;
22
+ }
23
+ console.log("GPU device obtained.");
24
+
25
+ const context = canvas.getContext("webgpu");
26
+ if (!context) {
27
+ console.error("Failed to get WebGPU context.");
28
+ return;
29
+ }
30
+ console.log("WebGPU context obtained.");
31
+
32
+ context.configure({
33
+ device: device,
34
+ format: navigator.gpu.getPreferredCanvasFormat(),
35
+ alphaMode: "opaque",
36
+ });
37
+
38
+ console.log("WebGPU initialized and canvas configured.");
39
+ }
40
+
41
+ // Call the initializeWebGPU function to ensure it runs
42
+ initializeWebGPU();
43
+
44
+ async function submitQuestion(imageFile, question) {
45
+ const formData = new FormData();
46
+ formData.append("image", imageFile);
47
+ formData.append("text", question);
48
+
49
+ try {
50
+ const response = await fetch("/predict", {
51
+ method: "POST",
52
+ body: formData,
53
+ });
54
+
55
+ if (!response.ok) {
56
+ const errorText = await response.text();
57
+ console.error("Failed to get a response:", response.status, response.statusText, errorText);
58
+ return `Error: Unable to fetch the answer. Status: ${response.status}, ${response.statusText}`;
59
+ }
60
+
61
+ const result = await response.json();
62
+ return result.data[0];
63
+ } catch (error) {
64
+ console.error("Fetch error:", error);
65
+ return `Error: Unable to fetch the answer. ${error.message}`;
66
+ }
67
+ }
68
+
69
+ // Handle user interactions
70
+ document.getElementById("submit-btn").addEventListener("click", async () => {
71
+ const imageFile = document.getElementById("image-upload").files[0];
72
+ if (!imageFile) {
73
+ alert("Please upload an image.");
74
+ return;
75
+ }
76
+ const question = document.getElementById("question").value;
77
+
78
+ const answer = await submitQuestion(imageFile, question);
79
+ document.getElementById("answer").innerText = `Answer: ${answer}`;
80
+ });
81
+
82
+ // Initialize WebGPU when the page loads
83
+ initializeWebGPU();