Skip to content

Instantly share code, notes, and snippets.

@5agado
Created January 19, 2018 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5agado/78120432902f010e4ca3fafe70d2331f to your computer and use it in GitHub Desktop.
Save 5agado/78120432902f010e4ca3fafe70d2331f to your computer and use it in GitHub Desktop.
Sublime3 plugin for text-generation via REST API
import sublime
import sublime_plugin
import http.client
host = "127.0.0.1"
port = "9000"
conn = http.client.HTTPConnection("{}:{}".format(host, port))
class TextGenCommand(sublime_plugin.TextCommand):
def run(self, edit, model_id, min_nb_words=5):
# get seed text, which in our case is the currently selected text in the editor
seed = self.view.substr(self.view.sel()[0]).replace(" ", "%20")
# call the REST API with given parameters
conn.request("GET",
"/models/{}/generate?min_nb_words={}&seed={}".format(model_id, min_nb_words, seed))
res = conn.getresponse()
data = res.read()
# decode text and insert after cursor current position
text = data.decode("utf-8")
self.view.insert(edit, self.view.sel()[0].end(), " " + text)
# Command to run from Sublime console
# view.run_command("text_gen", {"model_id":"bible_tf", "min_nb_words":5})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment