Skip to content

Commit

Permalink
services script
Browse files Browse the repository at this point in the history
  • Loading branch information
manishshettym committed Feb 22, 2024
1 parent c191459 commit 0930d22
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 32 deletions.
80 changes: 48 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,40 +58,56 @@ How to train CodeScholar:
Refer to the [training README](./codescholar/representation/README.md) for a detailed description of how to train CodeScholar.


How to run pre-trained CodeScholar:
How to use CodeScholar:
-----------------------

```bash
# start an elasticsearch server (hosts programs)
docker run --rm -p 9200:9200 -p 9300:9300 -e "xpack.security.enabled=false" -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:8.7.0
```

```bash
# start a redis server (hosts embeddings)
docker run --rm -p 6379:6379 redis
```

```bash
# index the dataset using /search/elastic_search.py
cd codescholar/search
python elastic_search.py --dataset <dataset_name>
```

> TODO: index all embeddings into redis; currently index happens before each search
```bash
# run the codescholar query (say np.mean) using /search/search.py
python search.py --dataset <dataset_name> --seed np.mean
```

You can also use some arguments with the search query:
```bash
--min_idiom_size <int> # minimum size of idioms to be saved
--max_idiom_size <int> # maximum size of idioms to be saved
--max_init_beams <int> # maximum beams to initialize search
--stop_at_equilibrium # stop search when diversity = reusability of idioms
```
*note: see more configurations in [/search/search_config.py](./codescholar/search/search_config.py)*
1. Starting services
```bash
# start codescholar services (elasticsearch and redis)
./services.sh start
```
<details>
<summary>what does this do?</summary>

```bash
# start an elasticsearch server (hosts programs) in a tmux session
docker run --rm -p 9200:9200 -p 9300:9300 -e "xpack.security.enabled=false" -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:8.7.0
# start a redis server (hosts embeddings)
docker run --rm -p 6379:6379 redis
```
</details>

2. Indexing
```bash
# index search space programs into elasticsearch
./services.sh index <dataset_name>
```
<details>
<summary>what does this do?</summary>

```bash
# index the dataset using /search/elastic_search.py
cd codescholar/search
python elastic_search.py --dataset <dataset_name>
```

> TODO: index all embeddings into redis; currently index happens before each search
</details>

3. Searching
```bash
# run the codescholar query (say np.mean) using /search/search.py
python search.py --dataset <dataset_name> --seed np.mean
```

You can also use some arguments with the search query:
```bash
--min_idiom_size <int> # minimum size of idioms to be saved
--max_idiom_size <int> # maximum size of idioms to be saved
--max_init_beams <int> # maximum beams to initialize search
--stop_at_equilibrium # stop search when diversity = reusability of idioms
```
*note: see more configurations in [/search/search_config.py](./codescholar/search/search_config.py)*

How to run CodeScholar Streamlit App:
---------------------------
Expand Down
85 changes: 85 additions & 0 deletions codescholar/services.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/bin/bash

# Function to check if a tmux session exists
tmux_session_exists() {
tmux has-session -t "$1" 2>/dev/null
}

# Function to start Elasticsearch in a tmux session
start_elasticsearch() {
if tmux_session_exists "elasticsearch"; then
echo "Elasticsearch tmux session already exists."
else
echo "Starting Elasticsearch in a tmux session..."
tmux new-session -d -s "elasticsearch" docker run --name codescholar-elasticsearch --rm -p 9200:9200 -p 9300:9300 -e "xpack.security.enabled=false" -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:8.7.0
fi
}

# Function to start Redis in a tmux session
start_redis() {
if tmux_session_exists "redis"; then
echo "Redis tmux session already exists."
else
echo "Starting Redis in a tmux session..."
tmux new-session -d -s "redis" docker run --name codescholar-redis --rm -p 6379:6379 redis
fi
}

# Function to stop a service running in a tmux session
stop_service() {
if tmux_session_exists "$1"; then
echo "Stopping $1..."
tmux send-keys -t "$1" C-c
tmux kill-session -t "$1"
else
echo "No tmux session for $1 found."
fi
}

# Function to run the indexing script
run_indexing() {
dataset_name="$1"
if [ -z "$dataset_name" ]; then
echo "Please provide a dataset name."
return 1
fi
echo "Running the indexing script for dataset: $dataset_name"
tmux new-session -d -s "indexing" "cd search && python elastic_search.py --dataset $dataset_name"
}

# Function to display the status of the indexing session
show_indexing_status() {
if tmux_session_exists "indexing"; then
echo "Indexing session status:"
tmux capture-pane -p -t "indexing"
else
echo "No indexing session found."
fi
}

# Main logic based on the first argument
case "$1" in
start)
start_elasticsearch
start_redis
;;
stop)
stop_service "elasticsearch"
stop_service "redis"
;;
killall)
stop_service "elasticsearch"
stop_service "redis"
stop_service "indexing"
;;
index)
run_indexing "$2"
;;
status)
show_indexing_status
;;
*)
echo "Usage: $0 {start|stop|killall|index <dataset_name>}"
exit 1
;;
esac

0 comments on commit 0930d22

Please sign in to comment.