Created
January 19, 2018 14:52
-
-
Save 5agado/78120432902f010e4ca3fafe70d2331f to your computer and use it in GitHub Desktop.
Sublime3 plugin for text-generation via REST API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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