beeguy commited on
Commit
3f7b6a8
·
1 Parent(s): 7a26dd9

embeddings upload

Browse files
Files changed (3) hide show
  1. app.py +8 -1
  2. static/index.html +5 -2
  3. static/script.js +32 -0
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from fastapi import FastAPI
2
  from os import getenv
3
  from langchain_huggingface import HuggingFaceEmbeddings
4
  from fastapi.responses import HTMLResponse
@@ -11,6 +11,13 @@ embeddings = HuggingFaceEmbeddings(model_name="jinaai/jina-embeddings-v2-small-e
11
 
12
  app.mount("/static", StaticFiles(directory="static"), name="static")
13
 
 
 
 
 
 
 
 
14
  @app.get("/embeddings")
15
  def get_embeddings(input: str):
16
  result = embeddings.embed_query(input)
 
1
+ from fastapi import FastAPI, Request
2
  from os import getenv
3
  from langchain_huggingface import HuggingFaceEmbeddings
4
  from fastapi.responses import HTMLResponse
 
11
 
12
  app.mount("/static", StaticFiles(directory="static"), name="static")
13
 
14
+ @app.post("/receive-embeddings")
15
+ async def receive_embeddings(request: Request):
16
+ data = await request.json()
17
+ embeddings = data.get("embeddings")
18
+ # Process the embeddings as needed
19
+ return {"status": "OK"}
20
+
21
  @app.get("/embeddings")
22
  def get_embeddings(input: str):
23
  result = embeddings.embed_query(input)
static/index.html CHANGED
@@ -5,8 +5,8 @@
5
  <meta charset="UTF-8" />
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
  <title>Fast API 🤗 Space served with Uvicorn</title>
8
- <link rel="stylesheet" href="style.css" />
9
- <script type="module" src="script.js"></script>
10
  </head>
11
 
12
  <body>
@@ -20,11 +20,14 @@
20
  </p>
21
  <p>Here you can create embeddings from the text you provide. This text should be submitted to the
22
  /embeddings endpoint.</p>
 
23
  <form class="text-gen-form">
24
  <label for="text-gen-input">Text prompt</label>
25
  <input id="text-gen-input" type="text" value="English: Translate There are many ducks. German:" />
26
  <button id="text-gen-submit">Submit</button>
27
  <button id="download-embeddings" type="button">Download Embeddings</button>
 
 
28
  <p class="text-gen-output"></p>
29
  </form>
30
  </section>
 
5
  <meta charset="UTF-8" />
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
  <title>Fast API 🤗 Space served with Uvicorn</title>
8
+ <link rel="stylesheet" href="/static/style.css" />
9
+ <script type="module" src="/static/script.js"></script>
10
  </head>
11
 
12
  <body>
 
20
  </p>
21
  <p>Here you can create embeddings from the text you provide. This text should be submitted to the
22
  /embeddings endpoint.</p>
23
+ <p>If you have embeddings, you can upload them instead.</p>
24
  <form class="text-gen-form">
25
  <label for="text-gen-input">Text prompt</label>
26
  <input id="text-gen-input" type="text" value="English: Translate There are many ducks. German:" />
27
  <button id="text-gen-submit">Submit</button>
28
  <button id="download-embeddings" type="button">Download Embeddings</button>
29
+ <button id="upload-embeddings" type="button">Upload Embeddings</button>
30
+ <input type="file" id="file-input" style="display: none;" />
31
  <p class="text-gen-output"></p>
32
  </form>
33
  </section>
static/script.js CHANGED
@@ -25,6 +25,8 @@ textGenForm.addEventListener('submit', async (event) => {
25
  });
26
 
27
  const downloadButton = document.getElementById('download-embeddings');
 
 
28
 
29
  const updateDownloadButtonState = () => {
30
  downloadButton.disabled = embeddingsList.length === 0;
@@ -40,7 +42,37 @@ const downloadEmbeddings = () => {
40
  downloadAnchorNode.remove();
41
  };
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  downloadButton.addEventListener('click', downloadEmbeddings);
 
44
 
45
  // Initialize button state
46
  updateDownloadButtonState();
 
25
  });
26
 
27
  const downloadButton = document.getElementById('download-embeddings');
28
+ const uploadButton = document.getElementById('upload-embeddings');
29
+ const fileInput = document.getElementById('file-input');
30
 
31
  const updateDownloadButtonState = () => {
32
  downloadButton.disabled = embeddingsList.length === 0;
 
42
  downloadAnchorNode.remove();
43
  };
44
 
45
+ const uploadEmbeddings = () => {
46
+ fileInput.click();
47
+ };
48
+
49
+ fileInput.addEventListener('change', async (event) => {
50
+ const file = event.target.files[0];
51
+ if (file) {
52
+ const reader = new FileReader();
53
+ reader.onload = async (e) => {
54
+ try {
55
+ const embeddings = JSON.parse(e.target.result);
56
+ embeddingsList = embeddings; // Store uploaded embeddings in the variable
57
+ updateDownloadButtonState(); // Update button state
58
+ // Optionally, you can send the embeddings to the server
59
+ await fetch('/receive-embeddings', {
60
+ method: 'POST',
61
+ headers: {
62
+ 'Content-Type': 'application/json'
63
+ },
64
+ body: JSON.stringify({ embeddings: embeddingsList })
65
+ });
66
+ } catch (err) {
67
+ console.error('Error reading or parsing the file', err);
68
+ }
69
+ };
70
+ reader.readAsText(file);
71
+ }
72
+ });
73
+
74
  downloadButton.addEventListener('click', downloadEmbeddings);
75
+ uploadButton.addEventListener('click', uploadEmbeddings);
76
 
77
  // Initialize button state
78
  updateDownloadButtonState();