Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
refactor: project according to the new api data
Browse files Browse the repository at this point in the history
  • Loading branch information
eureka320 committed Mar 27, 2024
1 parent 5d10d7b commit e0d8d9c
Show file tree
Hide file tree
Showing 38 changed files with 750 additions and 43 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ venv\Scripts\Activate
MacOS:
venv\bin\activate
pip uninstall -r requirements.txt -y
pip install -r requirements.txt
```
Expand Down Expand Up @@ -51,3 +52,35 @@ pip install -r requirements.txt
py main.py
```

### Create, Delete, Check API Key
- Create API Key
```
py .\create_api_key.py --payload_dir="payload_dir" --user="user email" --title=[Optional] --description=[Optional]
example:
py .\create_api_key.py --payload_dir="./test/regression/regression_testxxx/payload/mongodb_payload.json" --user="user@gmail.com" --title="title" --description="description"
```

- Delete API Key
```
py .\delete_api_key.py --payload_dir="payload_dir" --user="user email" --api_key="api key"
example:
py .\delete_api_key.py --payload_dir="./test/regression/regression_testxxx/payload/mongodb_payload.json" --user="user@gmail.com" --api_key="api_key"
```

- Check API key
```
py .\check_api_key.py --payload_dir="payload_dir" --user="user email" --api_key="api key"
example:
py .\check_api_key.py --payload_dir="./test/regression/regression_testxxx/payload/mongodb_payload.json" --user="user@gmail.com" --api_key="api_key"
```

### MongoDB:
{
"mongo_uri": "mongodb+srv://{user_name}:{password}@cluster0.ill5gnu.mongodb.net",
"db_name": "oridosai",
"collection_name": "apis"
}
34 changes: 26 additions & 8 deletions chatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from src.utils.read_json import read_json
from src.chatting.ChattingClass import ChattingClass
from src.mongodb.MongoDBClass import MongoDBClass

def chatting(args):
"""
Expand All @@ -15,14 +16,26 @@ def chatting(args):
payload_data = read_json(args.payload_dir)

# Call class instance
chatting = ChattingClass(
data_path=payload_data["data_path"],
api_key=payload_data["api_key"],
model_id=payload_data["model_id"],
temperature=payload_data["temperature"])

response = chatting.ask_question(args.question)
print(response)
mongodb = MongoDBClass(
db_name=payload_data["db_name"],
collection_name=payload_data["collection_name"],
mongo_uri=payload_data["mongo_uri"])

is_available = mongodb.check_validation_api(api_key=str(Path(args.api_key)), user=str(Path(args.user)))

if is_available:
print("valid api key")
# Call class instance
chatting = ChattingClass(
data_path=payload_data["data_path"],
api_key=payload_data["api_key"],
model_id=payload_data["model_id"],
temperature=payload_data["temperature"])

response = chatting.ask_question(args.question)
print(response)
else:
print("invalide api key")

gc.collect()

Expand All @@ -41,11 +54,16 @@ def chatting(args):
payload_name = "chatting_payload.json"
payload_dir = os.path.join(current_dir, "test", "regression", test_name, "payload", payload_name)

user = "user@gmail.com"
api_key = "AMEYbpdcmrUxNu_Fb80qutukUZdlsmYiH4g7As5LzNA1"

# Add options
p = argparse.ArgumentParser()
p = argparse.ArgumentParser(description="Conversational Agent.")
p.add_argument("--payload_dir", type=Path, default=payload_dir, help="payload directory to the test example")
p.add_argument("--question", type=str)
p.add_argument("--user", type=Path, default=user, help="user")
p.add_argument("--api_key", type=Path, default=api_key, help="title")
args = p.parse_args()

chatting(args)
22 changes: 18 additions & 4 deletions check_api_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,38 @@
import gc
import argparse
from pathlib import Path
from urllib.parse import quote_plus

from src.utils.read_json import read_json
from src.mongodb.MongoDBClass import MongoDBClass
from pathlib import Path

def delete_api_key(args):

def check_api_key(args):
"""
main entry point
"""

# Payload
payload_data = read_json(args.payload_dir)

# Your MongoDB Atlas connection details
mongodb_username = payload_data["mongodb_username"]
mongodb_password = payload_data["mongodb_password"]
mongodb_cluster_name = payload_data["mongodb_cluster_name"]
mongodb_database_name = payload_data["mongodb_database_name"]

# Escape the mongodb_username and mongodb_password
mongodb_escaped_username = quote_plus(mongodb_username)
mongodb_escaped_password = quote_plus(mongodb_password)

# Construct the MongoDB Atlas URI
mongo_uri = f"mongodb+srv://{mongodb_escaped_username}:{mongodb_escaped_password}@{mongodb_cluster_name}.mongodb.net/{mongodb_database_name}"

# Call class instance
mongodb = MongoDBClass(
db_name=payload_data["db_name"],
collection_name=payload_data["collection_name"],
mongo_uri=payload_data["mongo_uri"])
mongo_uri=mongo_uri)

mongodb.check_validation_api(api_key=str(Path(args.api_key)), user=str(Path(args.user)))

Expand Down Expand Up @@ -52,4 +66,4 @@ def delete_api_key(args):
p.add_argument("--api_key", type=Path, default=api_key, help="title")
args = p.parse_args()

delete_api_key(args)
check_api_key(args)
19 changes: 16 additions & 3 deletions create_api_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import argparse
from pathlib import Path
from datetime import datetime
from urllib.parse import quote_plus

from src.utils.read_json import read_json
from src.mongodb.MongoDBClass import MongoDBClass
from src.utils.utils import generate_api_key
from src.utils.utils_funcs import generate_api_key
from src.models.api_model import APIModel
from pathlib import Path

def create_api_key(args):
"""
Expand All @@ -19,11 +19,24 @@ def create_api_key(args):
# Payload
payload_data = read_json(args.payload_dir)

# Your MongoDB Atlas connection details
mongodb_username = payload_data["mongodb_username"]
mongodb_password = payload_data["mongodb_password"]
mongodb_cluster_name = payload_data["mongodb_cluster_name"]
mongodb_database_name = payload_data["mongodb_database_name"]

# Escape the mongodb_username and mongodb_password
mongodb_escaped_username = quote_plus(mongodb_username)
mongodb_escaped_password = quote_plus(mongodb_password)

# Construct the MongoDB Atlas URI
mongo_uri = f"mongodb+srv://{mongodb_escaped_username}:{mongodb_escaped_password}@{mongodb_cluster_name}.mongodb.net/{mongodb_database_name}"

# Call class instance
mongodb = MongoDBClass(
db_name=payload_data["db_name"],
collection_name=payload_data["collection_name"],
mongo_uri=payload_data["mongo_uri"])
mongo_uri=mongo_uri)

api_key = generate_api_key()

Expand Down
17 changes: 15 additions & 2 deletions delete_api_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import gc
import argparse
from pathlib import Path
from urllib.parse import quote_plus

from src.utils.read_json import read_json
from src.mongodb.MongoDBClass import MongoDBClass
from pathlib import Path

def delete_api_key(args):
"""
Expand All @@ -16,11 +16,24 @@ def delete_api_key(args):
# Payload
payload_data = read_json(args.payload_dir)

# Your MongoDB Atlas connection details
mongodb_username = payload_data["mongodb_username"]
mongodb_password = payload_data["mongodb_password"]
mongodb_cluster_name = payload_data["mongodb_cluster_name"]
mongodb_database_name = payload_data["mongodb_database_name"]

# Escape the mongodb_username and mongodb_password
mongodb_escaped_username = quote_plus(mongodb_username)
mongodb_escaped_password = quote_plus(mongodb_password)

# Construct the MongoDB Atlas URI
mongo_uri = f"mongodb+srv://{mongodb_escaped_username}:{mongodb_escaped_password}@{mongodb_cluster_name}.mongodb.net/{mongodb_database_name}"

# Call class instance
mongodb = MongoDBClass(
db_name=payload_data["db_name"],
collection_name=payload_data["collection_name"],
mongo_uri=payload_data["mongo_uri"])
mongo_uri=mongo_uri)

mongodb.delete_api(api_key=str(Path(args.api_key)), user=str(Path(args.user)))

Expand Down
22 changes: 18 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time
import argparse
from pathlib import Path
from urllib.parse import quote_plus
import concurrent.futures
from datetime import datetime
import json
Expand All @@ -16,18 +17,31 @@
from src.mathpix.Mathpix import Mathpix
from src.mongodb.MongoDBClass import MongoDBClass

from src.utils.utils import is_image_file, is_pdf_file, is_text_file, copy_file_to_folder, get_image_pages_percentage
from src.utils.utils_funcs import is_image_file, is_pdf_file, is_text_file, copy_file_to_folder, get_image_pages_percentage

def main(args):
def total_process(args):
start_time = time.time()

payload_data = read_json(args.payload_dir)

# Your MongoDB Atlas connection details
mongodb_username = payload_data["mongodb_username"]
mongodb_password = payload_data["mongodb_password"]
mongodb_cluster_name = payload_data["mongodb_cluster_name"]
mongodb_database_name = payload_data["mongodb_database_name"]

# Escape the mongodb_username and mongodb_password
mongodb_escaped_username = quote_plus(mongodb_username)
mongodb_escaped_password = quote_plus(mongodb_password)

# Construct the MongoDB Atlas URI
mongo_uri = f"mongodb+srv://{mongodb_escaped_username}:{mongodb_escaped_password}@{mongodb_cluster_name}.mongodb.net/{mongodb_database_name}"

# Call class instance
mongodb = MongoDBClass(
db_name=payload_data["db_name"],
collection_name=payload_data["collection_name"],
mongo_uri=payload_data["mongo_uri"])
mongo_uri=mongo_uri)

is_available = mongodb.check_validation_api(api_key=str(Path(args.api_key)), user=str(Path(args.user)))

Expand Down Expand Up @@ -268,4 +282,4 @@ def separate_data(path, threasold):
p.add_argument("--api_key", type=Path, default=api_key, help="title")
args = p.parse_args()

main(args)
total_process(args)
49 changes: 49 additions & 0 deletions mongodb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

import os
import gc
import argparse
from pathlib import Path

from src.utils.read_json import read_json
from src.mongodb.MongoDBClass import MongoDBClass

def mongodb(args):
"""
main entry point
"""

# Payload
payload_data = read_json(args.payload_dir)

# Call class instance
mongodb = MongoDBClass(
db_name=payload_data["db_name"],
collection_name=payload_data["collection_name"],
mongo_uri=payload_data["mongo_uri"])

mongodb.mongo_connect()

gc.collect()

if __name__ == "__main__":
"""
Form command lines
"""
# Clean up buffer memory
gc.collect()

# Current directory
current_dir = os.path.dirname(os.path.abspath(__file__))

# Payload directory
test_name = "regression_test013"
payload_name = "mongodb_payload.json"
payload_dir = os.path.join(current_dir, "test", "regression", test_name, "payload", payload_name)

# Add options
p = argparse.ArgumentParser()
p = argparse.ArgumentParser(description="Translate text within an image.")
p.add_argument("--payload_dir", type=Path, default=payload_dir, help="payload directory to the test example")
args = p.parse_args()

mongodb(args)
40 changes: 39 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import io
import os
import sys

Expand All @@ -12,16 +11,55 @@
sys.path.append(project_root)
sys.path.append(current_script_directory)

from fastapi.middleware.cors import CORSMiddleware
from fastapi import FastAPI
from starlette.responses import RedirectResponse
from starlette.status import HTTP_201_CREATED

from src.models.main_model import MainModel


# Create a FastAPI application
app = FastAPI(swagger_ui_parameters={"tryItOutEnabled": True})

# Configure CORS
origins = [
"http://localhost",
"http://localhost:8080",
"http://localhost:3000",
"https://example.com",
"https://www.example.com",
]

app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)


# Define a route to handle the root endpoint and redirect to the API documentation
@app.get("/")
async def root():
return RedirectResponse(app.docs_url)


if request_body.user == "":
user = "user@gmail.com"
else:
user = request_body.user

if request_body.api_key == "":
api_key = "AMEYbpdcmrUxNu_Fb80qutukUZdlsmYiH4g7As5LzNA1"
else:
api_key = request_body.api_key

args = {
'payload_dir' : payload_dir,
'user' : user,
'api_key' : api_key
}

total_process(args)
Loading

0 comments on commit e0d8d9c

Please sign in to comment.