Skip to content

Commit fa1cf65

Browse files
committed
Add --server to override any baseurl
1 parent fb544b0 commit fa1cf65

4 files changed

Lines changed: 56 additions & 4 deletions

File tree

Readme.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ usage: grobid_client [-h] [--input INPUT] [--output OUTPUT] [--config CONFIG]
5555
[--n N] [--generateIDs] [--consolidate_header]
5656
[--consolidate_citations] [--include_raw_citations]
5757
[--include_raw_affiliations] [--force] [--teiCoordinates]
58-
[--verbose] [--flavor FLAVOR]
58+
[--verbose] [--flavor FLAVOR] [--server SERVER]
5959
service
6060
6161
Client for GROBID services
@@ -75,6 +75,7 @@ optional arguments:
7575
(optional)
7676
--config CONFIG path to the config file, default is ./config.json
7777
--n N concurrency for service usage
78+
--server SERVER GROBID server URL (default: http://localhost:8070)
7879
--generateIDs generate random xml:id to textual XML elements of the
7980
result files
8081
--consolidate_header call GROBID with consolidation of the metadata
@@ -132,6 +133,15 @@ The following command example will process all the PDF files present in the inpu
132133
> grobid_client --input ~/tmp/in2 --output ~/tmp/out --teiCoordinates --segmentSentences processFulltextDocument
133134
```
134135

136+
To use a different GROBID server (e.g., a hosted service), use the `--server` argument:
137+
138+
```console
139+
> grobid_client --server https://lfoppiano-grobid.hf.space --input ~/tmp/in2 --output ~/tmp/out processFulltextDocument
140+
```
141+
142+
> [!NOTE]
143+
> The `--server` argument will override the server URL specified in the config file. If both are provided, the CLI argument takes precedence.
144+
135145
The file `example.py` gives an example of usage as a library, from a another python script.
136146

137147
## Using the client in your python
@@ -141,16 +151,24 @@ Import and call the client as follow:
141151
```python
142152
from grobid_client.grobid_client import GrobidClient
143153

154+
# Using default localhost server
144155
client = GrobidClient(config_path="./config.json")
145156
client.process("processFulltextDocument", "/mnt/data/covid/pdfs", n=20)
157+
158+
# Using a custom server
159+
client = GrobidClient(grobid_server="https://lfoppiano-grobid.hf.space", config_path="./config.json")
160+
client.process("processFulltextDocument", "/mnt/data/covid/pdfs", n=20)
146161
```
147162

148163
See also `example.py`.
149164

150165
## Configuration of the client
151166

152167
> [!TIP]
153-
> from version 0.0.12 the `config.json` will be optional, by default the client will connect to the local server (`http://localhost:8070`).
168+
> from version 0.0.12 the `config.json` will be optional, by default the client will connect to the local server (`http://localhost:8070`).
169+
170+
> [!NOTE]
171+
> When using the CLI, the `--server` argument will override the `grobid_server` value in the config file. This allows you to use a config file for most settings while easily switching servers via command line.
154172
155173
There are a few parameters that can be set with the `config.json` file.
156174

example.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
from grobid_client.grobid_client import GrobidClient
22

33
if __name__ == "__main__":
4+
# Example with default localhost server
45
client = GrobidClient(config_path="./config.json")
56
client.process("processFulltextDocument", "./resources/test_pdf", output="./resources/test_out/", consolidate_citations=True, tei_coordinates=True, force=True)
7+
8+
# Example with custom server (uncomment to use)
9+
# client = GrobidClient(grobid_server="https://lfoppiano-grobid.hf.space", config_path="./config.json")
10+
# client.process("processFulltextDocument", "./resources/test_pdf", output="./resources/test_out/", consolidate_citations=True, tei_coordinates=True, force=True)

grobid_client/grobid_client.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,33 @@ def __init__(
8181
}
8282
}
8383

84+
# Store constructor parameters to ensure they take precedence over config file
85+
# We'll use these to override config file values after loading
86+
self._constructor_params = {
87+
'grobid_server': grobid_server,
88+
'batch_size': batch_size,
89+
'coordinates': coordinates,
90+
'sleep_time': sleep_time,
91+
'timeout': timeout
92+
}
93+
8494
# Load config first (which may override logging settings)
8595
if config_path:
8696
self._load_config(config_path)
97+
98+
# Ensure constructor parameters take precedence over config file values
99+
# This ensures CLI arguments override config file values
100+
# Only override if the parameter was explicitly provided (not the default)
101+
if grobid_server != 'http://localhost:8070':
102+
self.config['grobid_server'] = grobid_server
103+
if batch_size != 1000:
104+
self.config['batch_size'] = batch_size
105+
if coordinates is not None: # coordinates has a complex default, so check if explicitly provided
106+
self.config['coordinates'] = coordinates
107+
if sleep_time != 5:
108+
self.config['sleep_time'] = sleep_time
109+
if timeout != 180:
110+
self.config['timeout'] = timeout
87111

88112
# Configure logging based on config
89113
self._configure_logging()
@@ -718,6 +742,11 @@ def main():
718742
default=None,
719743
help="Define the flavor to be used for the fulltext extraction",
720744
)
745+
parser.add_argument(
746+
"--server",
747+
default="http://localhost:8070",
748+
help="GROBID server URL (default: http://localhost:8070)",
749+
)
721750

722751
args = parser.parse_args()
723752

@@ -736,7 +765,7 @@ def main():
736765

737766
# Initialize GrobidClient which will configure logging based on config.json
738767
try:
739-
client = GrobidClient(config_path=config_path)
768+
client = GrobidClient(grobid_server=args.server, config_path=config_path)
740769
# Now use the client's logger for all subsequent logging
741770
logger = client.logger
742771
except ServerUnavailableException as e:

tests/test_grobid_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_init_default_values(self, mock_configure_logging, mock_test_server):
4343
assert client.config['grobid_server'] == 'http://localhost:8070'
4444
assert client.config['batch_size'] == 1000
4545
assert client.config['sleep_time'] == 5
46-
assert client.config['timeout'] == 60
46+
assert client.config['timeout'] == 180
4747
assert 'persName' in client.config['coordinates']
4848
mock_configure_logging.assert_called_once()
4949

0 commit comments

Comments
 (0)