Spaces:
Sleeping
Sleeping
parsing
Browse files- app.py +9 -7
- static/index.html +8 -7
- static/script.js +12 -10
- static/style.css +10 -0
app.py
CHANGED
@@ -55,16 +55,18 @@ async def chat(request: Request):
|
|
55 |
reply = response.content
|
56 |
return {"reply": reply}
|
57 |
|
58 |
-
@app.get("/
|
59 |
-
def
|
60 |
loader = create_loader(input)
|
61 |
docs = loader.load()
|
62 |
-
my_vector_store = InMemoryVectorStore.from_documents(docs, embeddings)
|
63 |
-
|
64 |
return {
|
65 |
-
"
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
68 |
|
69 |
@app.get("/", response_class=HTMLResponse)
|
70 |
def get_index():
|
|
|
55 |
reply = response.content
|
56 |
return {"reply": reply}
|
57 |
|
58 |
+
@app.get("/parsing")
|
59 |
+
def parse_url(input: str):
|
60 |
loader = create_loader(input)
|
61 |
docs = loader.load()
|
|
|
|
|
62 |
return {
|
63 |
+
"docs": docs
|
64 |
+
}
|
65 |
+
|
66 |
+
@app.get("/embeddings")
|
67 |
+
def get_embeddings():
|
68 |
+
my_vector_store = InMemoryVectorStore.from_documents(docs, embeddings)
|
69 |
+
return {"status": "OK"}
|
70 |
|
71 |
@app.get("/", response_class=HTMLResponse)
|
72 |
def get_index():
|
static/index.html
CHANGED
@@ -11,20 +11,21 @@
|
|
11 |
|
12 |
<body>
|
13 |
<main>
|
|
|
14 |
<section id="text-gen">
|
15 |
-
<
|
16 |
-
<p>Here you can create a vector storage from the url you provide.</p>
|
17 |
<form class="text-gen-form">
|
18 |
-
<
|
|
|
19 |
<input id="text-gen-input" type="text" />
|
20 |
-
<button id="text-gen-submit">
|
21 |
-
<button id="download-embeddings" type="button">
|
22 |
<p class="text-gen-output"></p>
|
23 |
</form>
|
24 |
-
<p>
|
25 |
<form class="text-gen-form">
|
26 |
<input type="file" id="file-input" style="display: none;" />
|
27 |
-
<button id="upload-embeddings" type="button">
|
28 |
</form>
|
29 |
</section>
|
30 |
<section id="chat">
|
|
|
11 |
|
12 |
<body>
|
13 |
<main>
|
14 |
+
<h1>Chat with your favorite website!</h1>
|
15 |
<section id="text-gen">
|
16 |
+
<p>here you can create a vector storage from the url you provide.</p>
|
|
|
17 |
<form class="text-gen-form">
|
18 |
+
<p>Here you will parse url and get list of docs</p>
|
19 |
+
<label for="text-gen-input">the url of the website</label>
|
20 |
<input id="text-gen-input" type="text" />
|
21 |
+
<button id="text-gen-submit">submit</button>
|
22 |
+
<button id="download-embeddings" type="button">download docs</button>
|
23 |
<p class="text-gen-output"></p>
|
24 |
</form>
|
25 |
+
<p>if you have docs, you can upload them instead.</p>
|
26 |
<form class="text-gen-form">
|
27 |
<input type="file" id="file-input" style="display: none;" />
|
28 |
+
<button id="upload-embeddings" type="button">upload docs</button>
|
29 |
</form>
|
30 |
</section>
|
31 |
<section id="chat">
|
static/script.js
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
const textGenForm = document.querySelector('.text-gen-form');
|
2 |
-
let
|
3 |
|
4 |
const embedText = async (text) => {
|
5 |
-
const inferResponse = await fetch(`
|
6 |
const inferJson = await inferResponse.json();
|
7 |
|
8 |
-
return inferJson
|
9 |
};
|
10 |
|
11 |
const spinnerOverlay = document.createElement('div');
|
@@ -30,8 +30,8 @@ textGenForm.addEventListener('submit', async (event) => {
|
|
30 |
|
31 |
try {
|
32 |
const embeddings = await embedText(textGenInput.value);
|
33 |
-
|
34 |
-
textGenParagraph.textContent = JSON.stringify(
|
35 |
updateDownloadButtonState(); // Update button state
|
36 |
} catch (err) {
|
37 |
console.error(err);
|
@@ -47,11 +47,11 @@ const uploadButton = document.getElementById('upload-embeddings');
|
|
47 |
const fileInput = document.getElementById('file-input');
|
48 |
|
49 |
const updateDownloadButtonState = () => {
|
50 |
-
downloadButton.disabled =
|
51 |
};
|
52 |
|
53 |
const downloadEmbeddings = () => {
|
54 |
-
const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(
|
55 |
const downloadAnchorNode = document.createElement('a');
|
56 |
downloadAnchorNode.setAttribute("href", dataStr);
|
57 |
downloadAnchorNode.setAttribute("download", "embeddings.json");
|
@@ -72,10 +72,10 @@ fileInput.addEventListener('change', async (event) => {
|
|
72 |
reader.onload = async (e) => {
|
73 |
try {
|
74 |
const embeddings = JSON.parse(e.target.result);
|
75 |
-
|
76 |
|
77 |
const textGenParagraph = document.querySelector('.text-gen-output');
|
78 |
-
textGenParagraph.textContent = JSON.stringify(
|
79 |
|
80 |
updateDownloadButtonState(); // Update button state
|
81 |
// Optionally, you can send the embeddings to the server
|
@@ -84,7 +84,7 @@ fileInput.addEventListener('change', async (event) => {
|
|
84 |
headers: {
|
85 |
'Content-Type': 'application/json'
|
86 |
},
|
87 |
-
body: JSON.stringify({ embeddings:
|
88 |
});
|
89 |
} catch (err) {
|
90 |
console.error('Error reading or parsing the file', err);
|
@@ -127,6 +127,7 @@ chatForm.addEventListener('submit', async (event) => {
|
|
127 |
|
128 |
const userMessageElement = document.createElement('div');
|
129 |
userMessageElement.textContent = `You: ${userMessage}`;
|
|
|
130 |
chatBox.appendChild(userMessageElement);
|
131 |
|
132 |
chatInput.value = '';
|
@@ -135,6 +136,7 @@ chatForm.addEventListener('submit', async (event) => {
|
|
135 |
const reply = await sendMessage(userMessage);
|
136 |
const replyMessageElement = document.createElement('div');
|
137 |
replyMessageElement.textContent = `Bot: ${reply}`;
|
|
|
138 |
chatBox.appendChild(replyMessageElement);
|
139 |
} catch (err) {
|
140 |
console.error('Error sending message:', err);
|
|
|
1 |
const textGenForm = document.querySelector('.text-gen-form');
|
2 |
+
let docsList = [];
|
3 |
|
4 |
const embedText = async (text) => {
|
5 |
+
const inferResponse = await fetch(`parsing?input=${text}`);
|
6 |
const inferJson = await inferResponse.json();
|
7 |
|
8 |
+
return inferJson;
|
9 |
};
|
10 |
|
11 |
const spinnerOverlay = document.createElement('div');
|
|
|
30 |
|
31 |
try {
|
32 |
const embeddings = await embedText(textGenInput.value);
|
33 |
+
docsList = embeddings; // Store embeddings in the variable
|
34 |
+
textGenParagraph.textContent = JSON.stringify(docsList);
|
35 |
updateDownloadButtonState(); // Update button state
|
36 |
} catch (err) {
|
37 |
console.error(err);
|
|
|
47 |
const fileInput = document.getElementById('file-input');
|
48 |
|
49 |
const updateDownloadButtonState = () => {
|
50 |
+
downloadButton.disabled = docsList.length === 0;
|
51 |
};
|
52 |
|
53 |
const downloadEmbeddings = () => {
|
54 |
+
const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(docsList));
|
55 |
const downloadAnchorNode = document.createElement('a');
|
56 |
downloadAnchorNode.setAttribute("href", dataStr);
|
57 |
downloadAnchorNode.setAttribute("download", "embeddings.json");
|
|
|
72 |
reader.onload = async (e) => {
|
73 |
try {
|
74 |
const embeddings = JSON.parse(e.target.result);
|
75 |
+
docsList = embeddings; // Store uploaded embeddings in the variable
|
76 |
|
77 |
const textGenParagraph = document.querySelector('.text-gen-output');
|
78 |
+
textGenParagraph.textContent = JSON.stringify(docsList);
|
79 |
|
80 |
updateDownloadButtonState(); // Update button state
|
81 |
// Optionally, you can send the embeddings to the server
|
|
|
84 |
headers: {
|
85 |
'Content-Type': 'application/json'
|
86 |
},
|
87 |
+
body: JSON.stringify({ embeddings: docsList })
|
88 |
});
|
89 |
} catch (err) {
|
90 |
console.error('Error reading or parsing the file', err);
|
|
|
127 |
|
128 |
const userMessageElement = document.createElement('div');
|
129 |
userMessageElement.textContent = `You: ${userMessage}`;
|
130 |
+
userMessageElement.classList.add('user-message');
|
131 |
chatBox.appendChild(userMessageElement);
|
132 |
|
133 |
chatInput.value = '';
|
|
|
136 |
const reply = await sendMessage(userMessage);
|
137 |
const replyMessageElement = document.createElement('div');
|
138 |
replyMessageElement.textContent = `Bot: ${reply}`;
|
139 |
+
replyMessageElement.classList.add('bot-reply');
|
140 |
chatBox.appendChild(replyMessageElement);
|
141 |
} catch (err) {
|
142 |
console.error('Error sending message:', err);
|
static/style.css
CHANGED
@@ -92,4 +92,14 @@ button {
|
|
92 |
@keyframes spin {
|
93 |
0% { transform: rotate(0deg); }
|
94 |
100% { transform: rotate(360deg); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
}
|
|
|
92 |
@keyframes spin {
|
93 |
0% { transform: rotate(0deg); }
|
94 |
100% { transform: rotate(360deg); }
|
95 |
+
}
|
96 |
+
|
97 |
+
.user-message {
|
98 |
+
text-align: right;
|
99 |
+
margin: 0.5rem 0;
|
100 |
+
}
|
101 |
+
|
102 |
+
.bot-reply {
|
103 |
+
text-align: left;
|
104 |
+
margin: 0.5rem 0;
|
105 |
}
|