Spaces:
Running
Running
deveix
commited on
Commit
•
e13455c
1
Parent(s):
8eb152d
files for docker
Browse files- .gitignore +128 -0
- Dockerfile +30 -0
- LICENSE +21 -0
- app/.env +3 -0
- app/__init__.py +0 -0
- app/main.py +76 -0
- requirements.txt +13 -0
.gitignore
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
pip-wheel-metadata/
|
24 |
+
share/python-wheels/
|
25 |
+
*.egg-info/
|
26 |
+
.installed.cfg
|
27 |
+
*.egg
|
28 |
+
MANIFEST
|
29 |
+
|
30 |
+
# PyInstaller
|
31 |
+
# Usually these files are written by a python script from a template
|
32 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
33 |
+
*.manifest
|
34 |
+
*.spec
|
35 |
+
|
36 |
+
# Installer logs
|
37 |
+
pip-log.txt
|
38 |
+
pip-delete-this-directory.txt
|
39 |
+
|
40 |
+
# Unit test / coverage reports
|
41 |
+
htmlcov/
|
42 |
+
.tox/
|
43 |
+
.nox/
|
44 |
+
.coverage
|
45 |
+
.coverage.*
|
46 |
+
.cache
|
47 |
+
nosetests.xml
|
48 |
+
coverage.xml
|
49 |
+
*.cover
|
50 |
+
*.py,cover
|
51 |
+
.hypothesis/
|
52 |
+
.pytest_cache/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
target/
|
76 |
+
|
77 |
+
# Jupyter Notebook
|
78 |
+
.ipynb_checkpoints
|
79 |
+
|
80 |
+
# IPython
|
81 |
+
profile_default/
|
82 |
+
ipython_config.py
|
83 |
+
|
84 |
+
# pyenv
|
85 |
+
.python-version
|
86 |
+
|
87 |
+
# pipenv
|
88 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
89 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
90 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
91 |
+
# install all needed dependencies.
|
92 |
+
#Pipfile.lock
|
93 |
+
|
94 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
95 |
+
__pypackages__/
|
96 |
+
|
97 |
+
# Celery stuff
|
98 |
+
celerybeat-schedule
|
99 |
+
celerybeat.pid
|
100 |
+
|
101 |
+
# SageMath parsed files
|
102 |
+
*.sage.py
|
103 |
+
|
104 |
+
# Environments
|
105 |
+
.venv
|
106 |
+
env/
|
107 |
+
venv/
|
108 |
+
ENV/
|
109 |
+
env.bak/
|
110 |
+
venv.bak/
|
111 |
+
|
112 |
+
# Spyder project settings
|
113 |
+
.spyderproject
|
114 |
+
.spyproject
|
115 |
+
|
116 |
+
# Rope project settings
|
117 |
+
.ropeproject
|
118 |
+
|
119 |
+
# mkdocs documentation
|
120 |
+
/site
|
121 |
+
|
122 |
+
# mypy
|
123 |
+
.mypy_cache/
|
124 |
+
.dmypy.json
|
125 |
+
dmypy.json
|
126 |
+
|
127 |
+
# Pyre type checker
|
128 |
+
.pyre/
|
Dockerfile
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Choose our version of Python
|
2 |
+
FROM python:3.9-slim
|
3 |
+
|
4 |
+
# Set up a working directory
|
5 |
+
WORKDIR /code
|
6 |
+
|
7 |
+
|
8 |
+
# Copy just the requirements into the working directory so it gets cached by itself
|
9 |
+
COPY ./requirements.txt /code/requirements.txt
|
10 |
+
|
11 |
+
# Install the dependencies from the requirements file
|
12 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
13 |
+
|
14 |
+
# RUN pip install pydantic==1.10.8
|
15 |
+
# RUN pip install typing-inspect==0.8.0 typing_extensions==4.5.
|
16 |
+
# RUN pip install --force-reinstall typing-extensions==4.5
|
17 |
+
# RUN pip install --force-reinstall openai==1.8
|
18 |
+
|
19 |
+
# Copy the code into the working directory
|
20 |
+
COPY ./app /code/app
|
21 |
+
|
22 |
+
ENV PORT=8000
|
23 |
+
EXPOSE 8000
|
24 |
+
|
25 |
+
# execute the command python main.py (in the WORKDIR) to start the app
|
26 |
+
CMD ["python", "app/main.py"]
|
27 |
+
|
28 |
+
# Tell uvicorn to start spin up our code, which will be running inside the container now
|
29 |
+
# CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
|
30 |
+
# uvicorn app.main:app --host 0.0.0.0 --port 80
|
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2023 Rowan Lea
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
app/.env
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
MONGODB_ATLAS_CLUSTER_URI="mongodb+srv://devixgamer:JvUeeCdFvUgyn8sn@quran.z83sfpu.mongodb.net/?retryWrites=true&w=majority&appName=quran"
|
2 |
+
API_TOKEN="aQgdqCHYmga7sxDPqT8YCrtNmAZYZp5T4Adv4xEj2C2nDyNEjacKET9vzcUjkRS9"
|
3 |
+
PORT=8000
|
app/__init__.py
ADDED
File without changes
|
app/main.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException, Header, Depends
|
2 |
+
from pydantic import BaseModel
|
3 |
+
import os
|
4 |
+
from pymongo import MongoClient
|
5 |
+
from langchain.embeddings import SentenceTransformerEmbeddings
|
6 |
+
from langchain_community.vectorstores import MongoDBAtlasVectorSearch
|
7 |
+
import uvicorn
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
# MongoDB connection and Langchain setup (as provided)
|
13 |
+
MONGODB_ATLAS_CLUSTER_URI = os.getenv("MONGODB_ATLAS_CLUSTER_URI", None)
|
14 |
+
client = MongoClient(MONGODB_ATLAS_CLUSTER_URI)
|
15 |
+
DB_NAME = "quran_db"
|
16 |
+
COLLECTION_NAME = "tafsir"
|
17 |
+
ATLAS_VECTOR_SEARCH_INDEX_NAME = "langchain_index"
|
18 |
+
MONGODB_COLLECTION = client[DB_NAME][COLLECTION_NAME]
|
19 |
+
|
20 |
+
|
21 |
+
embeddings = SentenceTransformerEmbeddings(model_name="BAAI/bge-m3")
|
22 |
+
|
23 |
+
vector_search = MongoDBAtlasVectorSearch.from_connection_string(
|
24 |
+
MONGODB_ATLAS_CLUSTER_URI,
|
25 |
+
DB_NAME + "." + COLLECTION_NAME,
|
26 |
+
embeddings,
|
27 |
+
index_name=ATLAS_VECTOR_SEARCH_INDEX_NAME,
|
28 |
+
)
|
29 |
+
|
30 |
+
|
31 |
+
# FastAPI application setup
|
32 |
+
app = FastAPI()
|
33 |
+
|
34 |
+
# Existing API endpoints
|
35 |
+
@app.get("/")
|
36 |
+
async def read_root():
|
37 |
+
return {"message": "Welcome to our app"}
|
38 |
+
|
39 |
+
# New Query model for the POST request body
|
40 |
+
class Item(BaseModel):
|
41 |
+
question: str
|
42 |
+
|
43 |
+
EXPECTED_TOKEN = os.getenv("API_TOKEN")
|
44 |
+
|
45 |
+
def verify_token(authorization: str = Header(None)):
|
46 |
+
"""
|
47 |
+
Dependency to verify the Authorization header contains the correct Bearer token.
|
48 |
+
"""
|
49 |
+
# Prefix for bearer token in the Authorization header
|
50 |
+
prefix = "Bearer "
|
51 |
+
|
52 |
+
# Check if the Authorization header is present and correctly formatted
|
53 |
+
if not authorization or not authorization.startswith(prefix):
|
54 |
+
raise HTTPException(status_code=401, detail="Unauthorized: Missing or invalid token")
|
55 |
+
|
56 |
+
# Extract the token from the Authorization header
|
57 |
+
token = authorization[len(prefix):]
|
58 |
+
|
59 |
+
# Compare the extracted token to the expected token value
|
60 |
+
if token != EXPECTED_TOKEN:
|
61 |
+
raise HTTPException(status_code=401, detail="Unauthorized: Incorrect token")
|
62 |
+
|
63 |
+
# New API endpoint to get an answer using the chain
|
64 |
+
@app.post("/get_answer")
|
65 |
+
async def get_answer(item: Item, token: str = Depends(verify_token)):
|
66 |
+
try:
|
67 |
+
# Perform the similarity search with the provided question
|
68 |
+
matching_docs = vector_search.similarity_search(item.question, k=3)
|
69 |
+
|
70 |
+
return {"answers": [doc.page_content for doc in matching_docs]}
|
71 |
+
except Exception as e:
|
72 |
+
# If there's an error, return a 500 error with the error's details
|
73 |
+
raise HTTPException(status_code=500, detail=str(e))
|
74 |
+
|
75 |
+
if __name__ == "__main__":
|
76 |
+
uvicorn.run("main:app", host="0.0.0.0", port=8080, reload=False)
|
requirements.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi>=0.68.0,<0.69.0
|
2 |
+
pydantic>=1.8.0,<2.0.0
|
3 |
+
uvicorn>=0.15.0,<0.16.0
|
4 |
+
openai==1.13.3
|
5 |
+
langchain==0.1.9
|
6 |
+
sentence-transformers==2.5.1
|
7 |
+
unstructured==0.12.5
|
8 |
+
pypdf==4.0.2
|
9 |
+
pymongo>=3.11
|
10 |
+
tiktoken==0.6.0
|
11 |
+
langchain-openai==0.0.8
|
12 |
+
python-dotenv
|
13 |
+
upstash-redis
|