beeguy commited on
Commit
0d3c10a
·
1 Parent(s): 3426e6e

test of embeddings

Browse files
Files changed (4) hide show
  1. app.py +11 -7
  2. static/index.html +33 -0
  3. static/script.js +21 -0
  4. static/style.css +45 -0
app.py CHANGED
@@ -1,22 +1,26 @@
1
  from fastapi import FastAPI
2
  from os import getenv
3
  from langchain_huggingface import HuggingFaceEmbeddings
 
 
4
 
5
  app = FastAPI()
6
  MY_KEY = getenv("MY_KEY")
7
 
8
  embeddings = HuggingFaceEmbeddings(model_name="jinaai/jina-embeddings-v2-small-en")
9
 
 
10
 
11
  @app.get("/embeddings")
12
- def get_embeddings():
13
- test = embeddings.embed_query("Hello, world!")
14
  return {
15
- "embeddings": "This is the embeddings endpoint",
16
- "test": test
17
  }
18
 
19
- @app.get("/")
20
- def greet_json():
21
- return {"Hello": "World! My key is: " + MY_KEY}
 
22
 
 
1
  from fastapi import FastAPI
2
  from os import getenv
3
  from langchain_huggingface import HuggingFaceEmbeddings
4
+ from fastapi.responses import HTMLResponse
5
+ from fastapi.staticfiles import StaticFiles
6
 
7
  app = FastAPI()
8
  MY_KEY = getenv("MY_KEY")
9
 
10
  embeddings = HuggingFaceEmbeddings(model_name="jinaai/jina-embeddings-v2-small-en")
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)
17
  return {
18
+ "embeddings": result,
19
+ "test": "testtext"
20
  }
21
 
22
+ @app.get("/", response_class=HTMLResponse)
23
+ def get_index():
24
+ with open("static/index.html") as f:
25
+ return f.read()
26
 
static/index.html ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+
4
+ <head>
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>
13
+ <main>
14
+ <section id="text-gen">
15
+ <h1>Chat with your favorite website!</h1>
16
+ <p>
17
+ Model:
18
+ <a href="https://huggingface.co/google/flan-t5-small" rel="noreferrer"
19
+ target="_blank">google/flan-t5-small</a>
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
+ <p class="text-gen-output"></p>
28
+ </form>
29
+ </section>
30
+ </main>
31
+ </body>
32
+
33
+ </html>
static/script.js ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const textGenForm = document.querySelector('.text-gen-form');
2
+
3
+ const embedText = async (text) => {
4
+ const inferResponse = await fetch(`embeddings?input=${text}`);
5
+ const inferJson = await inferResponse.json();
6
+
7
+ return inferJson.output;
8
+ };
9
+
10
+ textGenForm.addEventListener('submit', async (event) => {
11
+ event.preventDefault();
12
+
13
+ const textGenInput = document.getElementById('text-gen-input');
14
+ const textGenParagraph = document.querySelector('.text-gen-output');
15
+
16
+ try {
17
+ textGenParagraph.textContent = await embedText(textGenInput.value);
18
+ } catch (err) {
19
+ console.error(err);
20
+ }
21
+ });
static/style.css ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ body {
2
+ --text: hsl(0 0% 15%);
3
+ padding: 2.5rem;
4
+ font-family: sans-serif;
5
+ color: var(--text);
6
+ }
7
+
8
+ body.dark-theme {
9
+ --text: hsl(0 0% 90%);
10
+ background-color: hsl(223 39% 7%);
11
+ }
12
+
13
+ main {
14
+ max-width: 80rem;
15
+ text-align: center;
16
+ }
17
+
18
+ section {
19
+ display: flex;
20
+ flex-direction: column;
21
+ align-items: center;
22
+ }
23
+
24
+ a {
25
+ color: var(--text);
26
+ }
27
+
28
+ form {
29
+ width: 30rem;
30
+ margin: 0 auto;
31
+ }
32
+
33
+ input {
34
+ width: 100%;
35
+ }
36
+
37
+ button {
38
+ cursor: pointer;
39
+ }
40
+
41
+ .text-gen-output {
42
+ min-height: 1.2rem;
43
+ margin: 1rem;
44
+ border: 0.5px solid grey;
45
+ }