Skip to content

Commit

Permalink
Changes for ironspeed.
Browse files Browse the repository at this point in the history
  • Loading branch information
lalenzos committed Apr 14, 2022
1 parent a1b61a1 commit 4f46db5
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 105 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
**/adverb-venv/
**/models/**
**/.vscode
**/src/**



Expand Down
50 changes: 27 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,56 @@ Download and install Anaconda from the official website https://docs.anaconda.co

### Installation

Clone the project
#### Clone the project
```
git clone https://github.com/giganticode/adverb-backend.git
```

#### Windows
Run the 'install_and_run.bat'-Script - it will create a virtual conda environment called 'adverb', install all dependencies and start the webservice.
#### Create conda environment 'adverb'
For GPU-use:
```
cd adverb-backend
.\install_and_run.bat
conda env create -f conda_env.yml
```

#### Unix
Run the 'install_and_run.sh'-Script - it will create a virtual conda environment called 'adverb', install all dependencies and start the webservice.
For CPU-use:
```
cd adverb-backend
sh install_and_run.sh
```

### Manual installation

#### Create conda environment 'adverb'
```
conda env create -f environment.yml
conda env create -f conda_env_cpu.yml
```

#### Activate the conda environment
For GPU-use:
```
conda activate adverb
```
For CPU-use:
```
conda activate adverb-cpu
```

### Start the webservice
### Manually start the webservice
```
python webservice.py
```

Optional parameters:

--port=8080 => Set the port of the webservice (default 8080)
--port=8090 => Set the port of the webservice (default 8090)

--host=127.0.0.1 => Set the host of the webservice (default 127.0.0.1 )
--host=127.0.0.1 => Set the host of the webservice (default 0.0.0.0)

--debug => Enable the debug mode (default false)


#### Start the webservice with the provided script
run the run.bat-script (Windows) or the run.sh-script (UNIX)
### Start the webservice with the provided script
Running the following script will start the webservice.
Windows
```
run.bat
```
Unix
```
run.sh
```

## CPU execution

Running this script will start the webservice.
Install the conda environment from the `conda_env_cpu.yml` file. In addition, note that if you are testing CPU execution on a machine that includes GPUs you might need to specify CUDA_VISIBLE_DEVICES="" as part of your command.
25 changes: 25 additions & 0 deletions conda_env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: adverb
channels:
- pytorch
- nvidia
- conda-forge
- defaults
dependencies:
- python=3.7
- cudatoolkit=11.1
- faiss-gpu=1.7.0
- flask
- flask-cors
- pytorch=1.9.0
- torchaudio
- torchvision
- pip=21.0
- pip:
- "--editable=git+https://github.com/stanford-futuredata/ColBERT.git@fast_search#egg=colbert"
- bitarray
- gitpython
- scipy
- spacy
- tqdm
- transformers==4.10.0
- ujson
20 changes: 20 additions & 0 deletions conda_env_cpu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: adverb-cpu
channels:
- pytorch
- conda-forge
- defaults
dependencies:
- python=3.7
- flask
- flask-cors
- pytorch-cpu
- pip=21.0
- pip:
- "--editable=git+https://github.com/stanford-futuredata/ColBERT.git@fast_search#egg=colbert"
- bitarray
- gitpython
- scipy
- spacy
- tqdm
- transformers==4.10.0
- ujson
5 changes: 3 additions & 2 deletions controllers/code_search_colbert_controller.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from flask import json
from flask.wrappers import Request
from colbert.infra import Run, RunConfig, ColBERTConfig
Expand All @@ -19,7 +20,7 @@ def index(self, request: Request):

# collection = Collection(path="C:\\adverb-backend\\controllers\\downloads\\lotte\\science\\dev\\collection - Kopie.tsv")

checkpoint = "C:\\adverb-backend\\models\\colbertv2.0"
checkpoint = os.path.join(os.path.dirname(__file__), "..", "models", "colbertv2.0")
nbits = 2 # encode each dimension with 2 bits
doc_maxlen = 300 # truncate passages at 300 tokens
with Run().context(RunConfig(nranks=4)):
Expand All @@ -44,4 +45,4 @@ def search_for_text(self, request: Request):

# results = searcher.search(query, k=3)

# return results
return ""
1 change: 0 additions & 1 deletion create_env-file.bat

This file was deleted.

2 changes: 0 additions & 2 deletions create_env-file.sh

This file was deleted.

9 changes: 0 additions & 9 deletions environment.yml

This file was deleted.

26 changes: 0 additions & 26 deletions install_and_run.bat

This file was deleted.

20 changes: 0 additions & 20 deletions install_and_run.sh

This file was deleted.

1 change: 0 additions & 1 deletion run.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
call conda activate adverb
set CUDA_VISIBLE_DEVICES=3 & python webservice.py
1 change: 0 additions & 1 deletion run.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/bin/sh
conda activate adverb
CUDA_VISIBLE_DEVICES=3, python webservice.py
$SHELL
41 changes: 21 additions & 20 deletions webservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
os.environ["CUDA_VISIBLE_DEVICES"]="3"
tf_device='/gpu:0'

import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src", "colbert", "colbert"))

import argparse
from flask import Flask, jsonify, request
from flask_cors import CORS

from controllers.code_summary_controller import CodeSummaryController
from controllers.code_symbol_controller import CodeSymbolController
from controllers.code_search_codebert_controller import CodeSearchCodeBertController
# from controllers.code_search_colbert_controller import CodeSearchColBertController
# from controllers.code_search_codebert_controller import CodeSearchCodeBertController
from controllers.code_search_colbert_controller import CodeSearchColBertController

DEBUG = True
PORT = 8090
Expand All @@ -22,8 +25,8 @@
# controllers
code_summary = CodeSummaryController()
code_symbol = CodeSymbolController()
code_search_code_bert = CodeSearchCodeBertController()
# code_search_col_bert = CodeSearchColBertController()
# code_search_code_bert = CodeSearchCodeBertController()
code_search_col_bert = CodeSearchColBertController()

# API routes
@app.route("/api/summary", methods = ["POST"])
Expand All @@ -34,8 +37,7 @@ def get_summary():
response = jsonify(summary)
response.headers.add("Access-Control-Allow-Origin", "*")
return response
except Exception as e:
print(e)
except:
pass
return "Bad request", "400"

Expand All @@ -47,34 +49,33 @@ def get_symbol_name():
response = jsonify(name)
response.headers.add("Access-Control-Allow-Origin", "*")
return response
except Exception as e:
print(e)
except:
pass
return "Bad request", "400"

@app.route("/api/search", methods = ["POST"])
def search():
try:
search_result = code_search_code_bert.search_for_text(request)
# search_result = code_search_col_bert.search_for_text(request)
# search_result = code_search_code_bert.search_for_text(request)
search_result = code_search_col_bert.search_for_text(request)
if search_result:
response = jsonify(search_result)
response.headers.add("Access-Control-Allow-Origin", "*")
return response
except Exception as e:
print(e)
print(str(e))
pass
return "Bad request", "400"

# @app.route("/api/search_index", methods = ["POST"])
# def search_index():
# try:
# code_search_col_bert.index(request)
# return "Success", "200"
# except Exception as e:
# print(e)
# pass
# return "Bad request", "400"
@app.route("/api/search_index", methods = ["POST"])
def search_index():
try:
code_search_col_bert.index(request)
return "Success", "200"
except Exception as e:
print(str(e))
pass
return "Bad request", "400"


if __name__ == "__main__":
Expand Down

0 comments on commit 4f46db5

Please sign in to comment.