JairoDanielMT commited on
Commit
9753f1e
verified
1 Parent(s): b26c92a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +186 -178
app.py CHANGED
@@ -1,178 +1,186 @@
1
- from fastapi import FastAPI, HTTPException, Depends, File, UploadFile
2
- from typing import List
3
- from fastapi.responses import FileResponse
4
- import urllib
5
- from embeddings import EmbeddingManager
6
- from model import (
7
- AddFilesRequest,
8
- CreateVectorStoreRequest,
9
- DeleteVectorStoreRequest,
10
- DownloadVectorStoreRequest,
11
- ListSourcesRequest,
12
- SaveTempRequest,
13
- SearchSimilarityRequest,
14
- )
15
- from vector_db import VectorStoreManager
16
- import os
17
- import shutil
18
- from starlette.responses import RedirectResponse
19
-
20
- app = FastAPI()
21
-
22
-
23
- @app.get("/", include_in_schema=False)
24
- async def redirect_to_docs():
25
- return RedirectResponse(url="/docs")
26
-
27
-
28
- # Crear una sola instancia de EmbeddingManager
29
- embedding_manager = EmbeddingManager()
30
- embeddings = embedding_manager.get_embeddings
31
- path_docs = "docs" # Directorio temporal para almacenar los archivos subidos
32
- path_db = "database" # Directorio para almacenar el vectorstore
33
-
34
-
35
- @app.post("/vectorstore/create", tags=["VectorStore"])
36
- async def create_vectorstore(
37
- create_request: CreateVectorStoreRequest = Depends(), # Usar el modelo como dependencia
38
- files: List[UploadFile] = File(...),
39
- ):
40
- """Create a vectorstore from the uploaded documents."""
41
- try:
42
- if os.path.exists(path_docs):
43
- shutil.rmtree(path_docs)
44
- os.makedirs(path_docs)
45
- for file in files:
46
- file_path = os.path.join(path_docs, file.filename)
47
- with open(file_path, "wb") as f:
48
- f.write(await file.read())
49
- manager = VectorStoreManager(
50
- path=path_docs, name=create_request.name, embeddings=embeddings
51
- )
52
- if manager.create_vectorstore():
53
- shutil.rmtree(path_docs)
54
- return {"message": "Vectorstore created successfully."}
55
- shutil.rmtree(path_docs)
56
- return {"message": "Failed to create vectorstore."}
57
- except Exception as e:
58
- raise HTTPException(status_code=500, detail=str(e))
59
-
60
-
61
- @app.get("/vectorstore/search", tags=["Similarity Search"])
62
- async def search_similarity(search_request: SearchSimilarityRequest = Depends()):
63
- """Search for similar documents in the vectorstore."""
64
- try:
65
- manager = VectorStoreManager(
66
- path=path_db,
67
- name=search_request.name_database,
68
- embeddings=embeddings,
69
- )
70
- search_request.query = str(urllib.parse.unquote(search_request.query))
71
- result = manager.search_similarity(
72
- query=search_request.query, fuente=search_request.fuente
73
- )
74
- return {"results": result}
75
- except Exception as e:
76
- raise HTTPException(status_code=500, detail=str(e))
77
-
78
-
79
- @app.get("/vectorstore/sources", tags=["Sources"])
80
- async def list_sources(list_request: ListSourcesRequest = Depends()):
81
- try:
82
- manager = VectorStoreManager(
83
- path=path_db, name=list_request.nombre_db_vectorial, embeddings=embeddings
84
- )
85
- sources = manager.list_sources()
86
- return {"sources": sources}
87
- except Exception as e:
88
- raise HTTPException(status_code=500, detail=str(e))
89
-
90
-
91
- @app.post("/vectorstore/save_temp", tags=["Save Temp"])
92
- async def save_text_to_file_temp(save_temp: SaveTempRequest = Depends()):
93
- """Descripci贸n: Guarda en un archivo temporal el texto de una fuente espec铆fica."""
94
- try:
95
- manager = VectorStoreManager(
96
- path=path_db, name=save_temp.nombre_db_vectorial, embeddings=embeddings
97
- )
98
- saved = manager.save_text_to_file_temp(source=save_temp.fuente)
99
- if saved:
100
- return {"message": "Text saved to file successfully."}
101
- else:
102
- return {"message": "No text found to save."}
103
- except Exception as e:
104
- raise HTTPException(status_code=500, detail=str(e))
105
-
106
-
107
- @app.post("/vectorstore/add_files", tags=["Add Files"])
108
- async def add_files_vectorstore(
109
- add_files_request: AddFilesRequest = Depends(), files: List[UploadFile] = File(...)
110
- ):
111
- try:
112
- if os.path.exists(path_docs):
113
- shutil.rmtree(path_docs)
114
- os.makedirs(path_docs)
115
-
116
- for file in files:
117
- file_path = os.path.join(path_docs, file.filename)
118
- with open(file_path, "wb") as f:
119
- f.write(await file.read())
120
- manager = VectorStoreManager(
121
- path=path_docs,
122
- name=add_files_request.nombre_db_vectorial,
123
- embeddings=embeddings,
124
- )
125
- if manager.add_files_vectorstore():
126
- shutil.rmtree(path_docs)
127
- return {"message": "Files added to vectorstore successfully."}
128
- shutil.rmtree(path_docs)
129
- return {"message": "Failed to add files to vectorstore."}
130
- except Exception as e:
131
- raise HTTPException(status_code=500, detail=str(e))
132
-
133
-
134
- @app.delete("/vectorstore/delete", tags=["Delete VectorStore"])
135
- async def delete_vectorstore(delete_request: DeleteVectorStoreRequest = Depends()):
136
- """Delete the vectorstore and its data."""
137
- try:
138
- manager = VectorStoreManager(
139
- path=path_db, name=delete_request.nombre_db_vectorial, embeddings=embeddings
140
- )
141
- if manager.delete_vectorstore():
142
- return {"message": "Vectorstore deleted successfully."}
143
- return {"message": "Failed to delete vectorstore."}
144
- except Exception as e:
145
- raise HTTPException(status_code=500, detail=str(e))
146
-
147
-
148
- @app.post("/vectorstore/download", tags=["Download VectorStore"])
149
- async def download_vectorstore(
150
- download_request: DownloadVectorStoreRequest = Depends(),
151
- ):
152
- try:
153
- manager = VectorStoreManager(
154
- path=path_db,
155
- name=download_request.nombre_db_vectorial,
156
- embeddings=embeddings,
157
- )
158
- zip_path = manager.download_vectorstore()
159
- return FileResponse(zip_path, filename="vectorstore.zip")
160
- except Exception as e:
161
- raise HTTPException(status_code=500, detail=str(e))
162
-
163
-
164
- if __name__ == "__main__":
165
- import os
166
-
167
- try:
168
- # crear todas las carpetas necesarias si no existen
169
- carpetas = [path_docs, path_db, "temp"]
170
- for carpeta in carpetas:
171
- if not os.path.exists(carpeta):
172
- os.makedirs(carpeta)
173
- os.system("uvicorn app:app --port 7860 --host 0.0.0.0")
174
- except KeyboardInterrupt:
175
- print("Server stopped.")
176
- except Exception as e:
177
- print(e)
178
- print("Failed to start server.")
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException, Depends, File, UploadFile
2
+ from typing import List
3
+ from fastapi.responses import FileResponse
4
+ import urllib
5
+ from embeddings import EmbeddingManager
6
+ from model import (
7
+ AddFilesRequest,
8
+ CreateVectorStoreRequest,
9
+ DeleteVectorStoreRequest,
10
+ DownloadVectorStoreRequest,
11
+ ListSourcesRequest,
12
+ SaveTempRequest,
13
+ SearchSimilarityRequest,
14
+ )
15
+ from vector_db import VectorStoreManager
16
+ import os
17
+ import shutil
18
+ from starlette.responses import RedirectResponse
19
+ from fastapi.middleware.cors import CORSMiddleware
20
+
21
+ app = FastAPI()
22
+ app.add_middleware(
23
+ CORSMiddleware,
24
+ allow_origins=["*"], # Puedes poner una lista espec铆fica de dominios si lo prefieres
25
+ allow_credentials=True,
26
+ allow_methods=["*"], # Permite todos los m茅todos (GET, POST, PUT, DELETE, etc.)
27
+ allow_headers=["*"], # Permite todos los encabezados
28
+ )
29
+
30
+
31
+ @app.get("/", include_in_schema=False)
32
+ async def redirect_to_docs():
33
+ return RedirectResponse(url="/docs")
34
+
35
+
36
+ # Crear una sola instancia de EmbeddingManager
37
+ embedding_manager = EmbeddingManager()
38
+ embeddings = embedding_manager.get_embeddings
39
+ path_docs = "docs" # Directorio temporal para almacenar los archivos subidos
40
+ path_db = "database" # Directorio para almacenar el vectorstore
41
+
42
+
43
+ @app.post("/vectorstore/create", tags=["VectorStore"])
44
+ async def create_vectorstore(
45
+ create_request: CreateVectorStoreRequest = Depends(), # Usar el modelo como dependencia
46
+ files: List[UploadFile] = File(...),
47
+ ):
48
+ """Create a vectorstore from the uploaded documents."""
49
+ try:
50
+ if os.path.exists(path_docs):
51
+ shutil.rmtree(path_docs)
52
+ os.makedirs(path_docs)
53
+ for file in files:
54
+ file_path = os.path.join(path_docs, file.filename)
55
+ with open(file_path, "wb") as f:
56
+ f.write(await file.read())
57
+ manager = VectorStoreManager(
58
+ path=path_docs, name=create_request.name, embeddings=embeddings
59
+ )
60
+ if manager.create_vectorstore():
61
+ shutil.rmtree(path_docs)
62
+ return {"message": "Vectorstore created successfully."}
63
+ shutil.rmtree(path_docs)
64
+ return {"message": "Failed to create vectorstore."}
65
+ except Exception as e:
66
+ raise HTTPException(status_code=500, detail=str(e))
67
+
68
+
69
+ @app.get("/vectorstore/search", tags=["Similarity Search"])
70
+ async def search_similarity(search_request: SearchSimilarityRequest = Depends()):
71
+ """Search for similar documents in the vectorstore."""
72
+ try:
73
+ manager = VectorStoreManager(
74
+ path=path_db,
75
+ name=search_request.name_database,
76
+ embeddings=embeddings,
77
+ )
78
+ search_request.query = str(urllib.parse.unquote(search_request.query))
79
+ result = manager.search_similarity(
80
+ query=search_request.query, fuente=search_request.fuente
81
+ )
82
+ return {"results": result}
83
+ except Exception as e:
84
+ raise HTTPException(status_code=500, detail=str(e))
85
+
86
+
87
+ @app.get("/vectorstore/sources", tags=["Sources"])
88
+ async def list_sources(list_request: ListSourcesRequest = Depends()):
89
+ try:
90
+ manager = VectorStoreManager(
91
+ path=path_db, name=list_request.nombre_db_vectorial, embeddings=embeddings
92
+ )
93
+ sources = manager.list_sources()
94
+ return {"sources": sources}
95
+ except Exception as e:
96
+ raise HTTPException(status_code=500, detail=str(e))
97
+
98
+
99
+ @app.post("/vectorstore/save_temp", tags=["Save Temp"])
100
+ async def save_text_to_file_temp(save_temp: SaveTempRequest = Depends()):
101
+ """Descripci贸n: Guarda en un archivo temporal el texto de una fuente espec铆fica."""
102
+ try:
103
+ manager = VectorStoreManager(
104
+ path=path_db, name=save_temp.nombre_db_vectorial, embeddings=embeddings
105
+ )
106
+ saved = manager.save_text_to_file_temp(source=save_temp.fuente)
107
+ if saved:
108
+ return {"message": "Text saved to file successfully."}
109
+ else:
110
+ return {"message": "No text found to save."}
111
+ except Exception as e:
112
+ raise HTTPException(status_code=500, detail=str(e))
113
+
114
+
115
+ @app.post("/vectorstore/add_files", tags=["Add Files"])
116
+ async def add_files_vectorstore(
117
+ add_files_request: AddFilesRequest = Depends(), files: List[UploadFile] = File(...)
118
+ ):
119
+ try:
120
+ if os.path.exists(path_docs):
121
+ shutil.rmtree(path_docs)
122
+ os.makedirs(path_docs)
123
+
124
+ for file in files:
125
+ file_path = os.path.join(path_docs, file.filename)
126
+ with open(file_path, "wb") as f:
127
+ f.write(await file.read())
128
+ manager = VectorStoreManager(
129
+ path=path_docs,
130
+ name=add_files_request.nombre_db_vectorial,
131
+ embeddings=embeddings,
132
+ )
133
+ if manager.add_files_vectorstore():
134
+ shutil.rmtree(path_docs)
135
+ return {"message": "Files added to vectorstore successfully."}
136
+ shutil.rmtree(path_docs)
137
+ return {"message": "Failed to add files to vectorstore."}
138
+ except Exception as e:
139
+ raise HTTPException(status_code=500, detail=str(e))
140
+
141
+
142
+ @app.delete("/vectorstore/delete", tags=["Delete VectorStore"])
143
+ async def delete_vectorstore(delete_request: DeleteVectorStoreRequest = Depends()):
144
+ """Delete the vectorstore and its data."""
145
+ try:
146
+ manager = VectorStoreManager(
147
+ path=path_db, name=delete_request.nombre_db_vectorial, embeddings=embeddings
148
+ )
149
+ if manager.delete_vectorstore():
150
+ return {"message": "Vectorstore deleted successfully."}
151
+ return {"message": "Failed to delete vectorstore."}
152
+ except Exception as e:
153
+ raise HTTPException(status_code=500, detail=str(e))
154
+
155
+
156
+ @app.post("/vectorstore/download", tags=["Download VectorStore"])
157
+ async def download_vectorstore(
158
+ download_request: DownloadVectorStoreRequest = Depends(),
159
+ ):
160
+ try:
161
+ manager = VectorStoreManager(
162
+ path=path_db,
163
+ name=download_request.nombre_db_vectorial,
164
+ embeddings=embeddings,
165
+ )
166
+ zip_path = manager.download_vectorstore()
167
+ return FileResponse(zip_path, filename="vectorstore.zip")
168
+ except Exception as e:
169
+ raise HTTPException(status_code=500, detail=str(e))
170
+
171
+
172
+ if __name__ == "__main__":
173
+ import os
174
+
175
+ try:
176
+ # crear todas las carpetas necesarias si no existen
177
+ carpetas = [path_docs, path_db, "temp"]
178
+ for carpeta in carpetas:
179
+ if not os.path.exists(carpeta):
180
+ os.makedirs(carpeta)
181
+ os.system("uvicorn app:app --port 7860 --host 0.0.0.0")
182
+ except KeyboardInterrupt:
183
+ print("Server stopped.")
184
+ except Exception as e:
185
+ print(e)
186
+ print("Failed to start server.")