Class: NLPCloud::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/nlpcloud.rb

Overview

Client requests the API.

Instance Method Summary collapse

Constructor Details

#initialize(model, token, gpu: false, lang: '', asynchronous: false) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/nlpcloud.rb', line 12

def initialize(model, token, gpu: false, lang: '', asynchronous: false)
  @headers = {
    'Authorization' => "Token #{token}",
    'Content-Type' => 'application/json',
    'User-Agent' => 'nlpcloud-ruby-client'
  }

  @root_url = "#{BASE_URL}/#{API_VERSION}/"

  if lang == "en"
    lang = ""
  end
  if lang == "eng_Latn"
    lang = ""
  end

  if gpu
    @root_url += "gpu/"
  end

  if asynchronous
    @root_url += "async/"
  end

  if lang != ""
    @root_url += lang + "/"
  end

  @root_url += model

end

Instance Method Details

#ad_generation(keywords) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/nlpcloud.rb', line 44

def ad_generation(keywords)
  payload = {
    'keywords' => keywords
  }
  response = RestClient.post("#{@root_url}/ad-generation", payload.to_json, @headers)
  JSON.parse(response.body)
end

#asr(url: nil, encoded_file: nil, input_language: nil) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/nlpcloud.rb', line 52

def asr(url: nil, encoded_file: nil, input_language: nil)
  payload = {
    'url' => url,
    'encoded_file' => encoded_file,
    'input_language' => input_language
  }
  response = RestClient.post("#{@root_url}/asr", payload.to_json, @headers)
  JSON.parse(response.body)
end

#async_result(url) ⇒ Object



62
63
64
65
# File 'lib/nlpcloud.rb', line 62

def async_result(url)
  response = RestClient.get(url, @headers)
  JSON.parse(response.body)
end

#chatbot(text, context: nil, history: nil) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/nlpcloud.rb', line 67

def chatbot(text, context: nil, history: nil)
  payload = {
    'text' => text,
    'context' => context,
    'history' => history
  }
  response = RestClient.post("#{@root_url}/chatbot", payload.to_json, @headers)
  JSON.parse(response.body)
end

#classification(text, labels: nil, multi_class: nil) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/nlpcloud.rb', line 77

def classification(text, labels: nil, multi_class: nil)
  payload = {
    'text' => text,
    'labels' => labels,
    'multi_class' => multi_class
  }
  response = RestClient.post("#{@root_url}/classification", payload.to_json, @headers)
  JSON.parse(response.body)
end

#code_generation(instruction) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/nlpcloud.rb', line 87

def code_generation(instruction)
  payload = {
    'instruction' => instruction
  }
  response = RestClient.post("#{@root_url}/code-generation", payload.to_json, @headers)
  JSON.parse(response.body)
end

#dependencies(text) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/nlpcloud.rb', line 95

def dependencies(text)
  payload = {
    'text' => text
  }
  response = RestClient.post("#{@root_url}/dependencies", payload.to_json, @headers)
  JSON.parse(response.body)
end

#embeddings(sentences) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/nlpcloud.rb', line 103

def embeddings(sentences)
  payload = {
    'sentences' => sentences
  }
  response = RestClient.post("#{@root_url}/embeddings", payload.to_json, @headers)
  JSON.parse(response.body)
end

#entities(text, searched_entity: nil) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/nlpcloud.rb', line 111

def entities(text, searched_entity: nil)
  payload = {
    'text' => text,
    'searched_entity' => searched_entity
  }
  response = RestClient.post("#{@root_url}/entities", payload.to_json, @headers)
  JSON.parse(response.body)
end

#generation(text, min_length: nil, max_length: nil, length_no_input: nil, end_sequence: nil, remove_input: nil, do_sample: nil, num_beams: nil, early_stopping: nil, no_repeat_ngram_size: nil, num_return_sequences: nil, top_k: nil, top_p: nil, temperature: nil, repetition_penalty: nil, length_penalty: nil, bad_words: nil, remove_end_sequence: nil, is_instruct: nil) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/nlpcloud.rb', line 120

def generation(text, min_length: nil, max_length: nil, length_no_input: nil,
               end_sequence: nil, remove_input: nil, do_sample: nil, num_beams: nil, early_stopping: nil,
               no_repeat_ngram_size: nil, num_return_sequences: nil, top_k: nil, top_p: nil,
               temperature: nil, repetition_penalty: nil, length_penalty: nil, bad_words: nil, remove_end_sequence: nil,
               is_instruct: nil)
  payload = {
    'text' => text,
    'min_length' => min_length,
    'max_length' => max_length,
    'length_no_input' => length_no_input,
    'end_sequence' => end_sequence,
    'remove_input' => remove_input,
    'do_sample' => do_sample,
    'num_beams' => num_beams,
    'early_stopping' => early_stopping,
    'no_repeat_ngram_size' => no_repeat_ngram_size,
    'num_return_sequences' => num_return_sequences,
    'top_k' => top_k,
    'top_p' => top_p,
    'temperature' => temperature,
    'repetition_penalty' => repetition_penalty,
    'length_penalty' => length_penalty,
    'bad_words' => bad_words,
    'remove_end_sequence' => remove_end_sequence,
    'is_instruct' => is_instruct
  }
  response = RestClient.post("#{@root_url}/generation", payload.to_json, @headers)
  JSON.parse(response.body)
end

#gs_correction(text) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/nlpcloud.rb', line 150

def gs_correction(text)
  payload = {
    'text' => text
  }
  response = RestClient.post("#{@root_url}/gs-correction", payload.to_json, @headers)
  JSON.parse(response.body)
end

#image_generation(text) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/nlpcloud.rb', line 158

def image_generation(text)
  payload = {
    'text' => text
  }
  response = RestClient.post("#{@root_url}/image-generation", payload.to_json, @headers)
  JSON.parse(response.body)
end

#intent_classification(text) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/nlpcloud.rb', line 166

def intent_classification(text)
  payload = {
    'text' => text
  }
  response = RestClient.post("#{@root_url}/intent-classification", payload.to_json, @headers)
  JSON.parse(response.body)
end

#kw_kp_extraction(text) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/nlpcloud.rb', line 174

def kw_kp_extraction(text)
  payload = {
    'text' => text
  }
  response = RestClient.post("#{@root_url}/kw-kp-extraction", payload.to_json, @headers)
  JSON.parse(response.body)
end

#langdetection(text) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/nlpcloud.rb', line 182

def langdetection(text)
  payload = {
    'text' => text
  }
  response = RestClient.post("#{@root_url}/langdetection", payload.to_json, @headers)
  JSON.parse(response.body)
end

#lib_versionsObject



190
191
192
193
# File 'lib/nlpcloud.rb', line 190

def lib_versions
  response = RestClient.get("#{@root_url}/versions", @headers)
  JSON.parse(response.body)
end

#paraphrasing(text) ⇒ Object



195
196
197
198
199
200
201
# File 'lib/nlpcloud.rb', line 195

def paraphrasing(text)
  payload = {
    'text' => text
  }
  response = RestClient.post("#{@root_url}/paraphrasing", payload.to_json, @headers)
  JSON.parse(response.body)
end

#question(question, context: nil) ⇒ Object



203
204
205
206
207
208
209
210
# File 'lib/nlpcloud.rb', line 203

def question(question, context: nil)
  payload = {
    'question' => question,
    'context' => context
  }
  response = RestClient.post("#{@root_url}/question", payload.to_json, @headers)
  JSON.parse(response.body)
end

#semantic_search(text, num_results: nil) ⇒ Object



212
213
214
215
216
217
218
219
# File 'lib/nlpcloud.rb', line 212

def semantic_search(text, num_results: nil)
  payload = {
    'text' => text,
    'num_results' => num_results
  }
  response = RestClient.post("#{@root_url}/semantic_search", payload.to_json, @headers)
  JSON.parse(response.body)
end

#semantic_similarity(sentences) ⇒ Object



221
222
223
224
225
226
227
# File 'lib/nlpcloud.rb', line 221

def semantic_similarity(sentences)
  payload = {
    'sentences' => sentences
  }
  response = RestClient.post("#{@root_url}/semantic_similarity", payload.to_json, @headers)
  JSON.parse(response.body)
end

#sentence_dependencies(text) ⇒ Object



229
230
231
232
233
234
235
# File 'lib/nlpcloud.rb', line 229

def sentence_dependencies(text)
  payload = {
    'text' => text
  }
  response = RestClient.post("#{@root_url}/sentence-dependencies", payload.to_json, @headers)
  JSON.parse(response.body)
end

#sentiment(text) ⇒ Object



237
238
239
240
241
242
243
# File 'lib/nlpcloud.rb', line 237

def sentiment(text)
  payload = {
    'text' => text
  }
  response = RestClient.post("#{@root_url}/sentiment", payload.to_json, @headers)
  JSON.parse(response.body)
end

#speech_synthesis(text, voice: nil) ⇒ Object



245
246
247
248
249
250
251
252
# File 'lib/nlpcloud.rb', line 245

def speech_synthesis(text, voice: nil)
  payload = {
    'text' => text,
    'voice' => voice
  }
  response = RestClient.post("#{@root_url}/speech-synthesis", payload.to_json, @headers)
  JSON.parse(response.body)
end

#summarization(text, size: nil) ⇒ Object



254
255
256
257
258
259
260
261
# File 'lib/nlpcloud.rb', line 254

def summarization(text, size: nil)
  payload = {
    'text' => text,
    'size' => size
  }
  response = RestClient.post("#{@root_url}/summarization", payload.to_json, @headers)
  JSON.parse(response.body)
end

#tokens(text) ⇒ Object



263
264
265
266
267
268
269
# File 'lib/nlpcloud.rb', line 263

def tokens(text)
  payload = {
    'text' => text
  }
  response = RestClient.post("#{@root_url}/tokens", payload.to_json, @headers)
  JSON.parse(response.body)
end

#translation(text, source, target) ⇒ Object



271
272
273
274
275
276
277
278
279
# File 'lib/nlpcloud.rb', line 271

def translation(text, source, target)
  payload = {
    'text' => text,
    'source' => source,
    'target' => target
  }
  response = RestClient.post("#{@root_url}/translation", payload.to_json, @headers)
  JSON.parse(response.body)
end