roberto ceraolo
commited on
Commit
•
e1aeb67
1
Parent(s):
be3c80f
app
Browse files- app.py +44 -4
- faiss_index_upload.py +0 -47
app.py
CHANGED
@@ -1,7 +1,47 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
import faiss
|
4 |
+
import numpy as np
|
5 |
+
from pathlib import Path
|
6 |
|
7 |
+
# Constants
|
8 |
+
DATA_DIR = "data"
|
9 |
+
INDEX_PATH = os.path.join(DATA_DIR, "faiss_index.index")
|
10 |
|
11 |
+
def save_index(file_obj):
|
12 |
+
"""
|
13 |
+
Save uploaded FAISS index to the data directory
|
14 |
+
"""
|
15 |
+
# Create data directory if it doesn't exist
|
16 |
+
Path(DATA_DIR).mkdir(exist_ok=True)
|
17 |
+
|
18 |
+
# Check if index already exists
|
19 |
+
if os.path.exists(INDEX_PATH):
|
20 |
+
return "⚠️ A FAISS index already exists in the data directory. Please remove it first."
|
21 |
+
|
22 |
+
try:
|
23 |
+
# Save the uploaded file
|
24 |
+
with open(INDEX_PATH, 'wb') as f:
|
25 |
+
f.write(file_obj)
|
26 |
+
|
27 |
+
# Verify the saved file is a valid FAISS index
|
28 |
+
faiss.read_index(INDEX_PATH)
|
29 |
+
return "✅ FAISS index successfully uploaded and saved!"
|
30 |
+
|
31 |
+
except Exception as e:
|
32 |
+
# If there was an error, remove the file if it was created
|
33 |
+
if os.path.exists(INDEX_PATH):
|
34 |
+
os.remove(INDEX_PATH)
|
35 |
+
return f"❌ Error: Invalid FAISS index file - {str(e)}"
|
36 |
+
|
37 |
+
# Create Gradio interface
|
38 |
+
demo = gr.Interface(
|
39 |
+
fn=save_index,
|
40 |
+
inputs=gr.File(label="Upload FAISS Index", file_types=[".index"]),
|
41 |
+
outputs=gr.Textbox(label="Status"),
|
42 |
+
title="FAISS Index Uploader",
|
43 |
+
description="Upload a FAISS index file to store in the HuggingFace Space data directory.",
|
44 |
+
)
|
45 |
+
|
46 |
+
if __name__ == "__main__":
|
47 |
+
demo.launch()
|
faiss_index_upload.py
DELETED
@@ -1,47 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
import faiss
|
4 |
-
import numpy as np
|
5 |
-
from pathlib import Path
|
6 |
-
|
7 |
-
# Constants
|
8 |
-
DATA_DIR = "data"
|
9 |
-
INDEX_PATH = os.path.join(DATA_DIR, "faiss_index.index")
|
10 |
-
|
11 |
-
def save_index(file_obj):
|
12 |
-
"""
|
13 |
-
Save uploaded FAISS index to the data directory
|
14 |
-
"""
|
15 |
-
# Create data directory if it doesn't exist
|
16 |
-
Path(DATA_DIR).mkdir(exist_ok=True)
|
17 |
-
|
18 |
-
# Check if index already exists
|
19 |
-
if os.path.exists(INDEX_PATH):
|
20 |
-
return "⚠️ A FAISS index already exists in the data directory. Please remove it first."
|
21 |
-
|
22 |
-
try:
|
23 |
-
# Save the uploaded file
|
24 |
-
with open(INDEX_PATH, 'wb') as f:
|
25 |
-
f.write(file_obj)
|
26 |
-
|
27 |
-
# Verify the saved file is a valid FAISS index
|
28 |
-
faiss.read_index(INDEX_PATH)
|
29 |
-
return "✅ FAISS index successfully uploaded and saved!"
|
30 |
-
|
31 |
-
except Exception as e:
|
32 |
-
# If there was an error, remove the file if it was created
|
33 |
-
if os.path.exists(INDEX_PATH):
|
34 |
-
os.remove(INDEX_PATH)
|
35 |
-
return f"❌ Error: Invalid FAISS index file - {str(e)}"
|
36 |
-
|
37 |
-
# Create Gradio interface
|
38 |
-
demo = gr.Interface(
|
39 |
-
fn=save_index,
|
40 |
-
inputs=gr.File(label="Upload FAISS Index", file_types=[".index"]),
|
41 |
-
outputs=gr.Textbox(label="Status"),
|
42 |
-
title="FAISS Index Uploader",
|
43 |
-
description="Upload a FAISS index file to store in the HuggingFace Space data directory.",
|
44 |
-
)
|
45 |
-
|
46 |
-
if __name__ == "__main__":
|
47 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|