def hack(pdfFileObj): import Algorithmia pdfReader = PyPDF2.PdfFileReader(pdfFileObj) print(pdfReader.numPages) pageObj = pdfReader.getPage(55) print(pageObj.extractText()) inp = pageObj.extractText() client = Algorithmia.client('simeIg+RX6DGJbO8d0NmbFy2aAL1') algo = client.algo('nlp/Summarizer/0.1.6') print(algo.pipe(inp).result) stopWords = set(stopwords.words('english')) wordtokens = word_tokenize(inp) fil_sent = [w for w in wordtokens if not w in stopWords] f = {} for word in fil_sent: if word not in f: f[word] = 1 else: f[word] += 1 client = Algorithmia.client('simeIg+RX6DGJbO8d0NmbFy2aAL1') algo = client.algo('cindyxiaoxiaoli/KeywordExtraction/0.3.0') key = algo.pipe(inp).result for i in key: print(i) for url in search(i, stop=5): print(url)
def listener(messages): for m in messages: print(m.text) cliente = Algorithmia.client('simtcMfVcIi2EcFstBQAJlyiiKe1') algo = cliente.algo( 'shashankgutha/WebsiteLinksRecommenderForkeywords/1.0.1') algo.set_options(timeout=300) # optional print( algo.pipe(m.text).result[0]['abstract'] + ' know more in ' + algo.pipe(m.text).result[0]['url']) text = (algo.pipe(m.text).result[0]['abstract'] + ' know more in ' + algo.pipe(m.text).result[0]['url']) r = requests.get( "https://api.qwant.com/api/search/images", params={ 'count': 50, 'q': m.text, 't': 'images', 'safesearch': 1, 'locale': 'en_US', 'uiv': 4 }, headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36' }) response = r.json().get('data').get('result').get('items') urls = [r.get('media') for r in response] print(random.choice(urls)) input = {"image": random.choice(urls)} cliente = Algorithmia.client('simtcMfVcIi2EcFstBQAJlyiiKe1') algo = cliente.algo('util/SmartImageDownloader/0.2.18') algo.set_options(timeout=300) # optional save = algo.pipe(input).result save = save['savePath'] localAbsPath = cliente.file(str(save[0])).getFile().name username = '******' password = '******' image = localAbsPath text = text + "#jiujitsu #mma #legends #oss #gentleart" with client(username, password) as cli: cli.upload(image, text)
def get_emotion(): print("Getting emotion...") # API call input = bytearray(open("snapshots/pic.png", "rb").read()) client = Algorithmia.client('api-key') algo = client.algo('deeplearning/EmotionRecognitionCNNMBP/1.0.1') op = (algo.pipe(input).result)["results"] # Returned from API call if (op == []): current = "Neutral" else: emotion = ((op[0])["emotions"]) analyze = dict() for emo in emotion: analyze[str(emo["label"])] = float(emo["confidence"]) current = max(analyze, key=analyze.get) # Color code emotions emotion_color_dict = { 'Neutral': 11, 'Sad': 31, 'Disgust': 51, 'Fear': 61, 'Surprise': 41, 'Happy': 21, 'Angry': 1 } emot_list.append(emotion_color_dict[current]) print(emot_list) return current
def test_algo(regular_api_key, api_address, case_data, algo_name, algo_hash): client = Algorithmia.client(api_key=regular_api_key, api_address=api_address) failures = [] for case in case_data: input = case['input'] expected = case['expected_output'] name = case['case_name'] output = client.algo("{}/{}".format(algo_name, algo_hash)).pipe(input).result print("case: {}".format(name)) if output == expected: print("pass") else: failure = { "output": output, "expected_output": expected, "case_name": name } failures.append(failure) print("fail") if len(failures) > 0: fail_msg = "At least one test case failed:\n" for failure in failures: fail_msg += "case_name: {}\nexpected_output: {}\nreal_output: {}\n".format( failure['case_name'], failure['expected_output'], failure['output']) #raise Exception(fail_msg) print(fail_msg) else: print("all test cases pass for {}/{}".format(algo_name, algo_hash))
def main(filepath, outpath, length): story = '' client = Algorithmia.client(api_key.key) alg_path = "data://.algo/ngram/GenerateTrigramFrequencies/temp/trigrams.txt" generate_trigrams(filepath, alg_path) while len(re.findall(r'\w+', story)) < length: print "Generating new paragraph..." input = [ "data://.algo/ngram/GenerateTrigramFrequencies/temp/trigrams.txt", "xxBeGiN142xx", "xxEnD142xx", (randint(1, 9)) ] new_par = client.algo( '/lizmrush/GenerateParagraphFromTrigram/0.1.2').pipe(input) if len(re.findall(r'\w+', story)) + len(re.findall(r'\w+', new_par)) > length: break story += new_par.strip() story += '\n\n' print "Word count:" print len(re.findall(r'\w+', story)) with open(outpath, 'w') as f: f.write(story.encode('utf8')) f.close() print "Complete! Story written to " + outpath
def detect(filename, image): print(filename) #img = Image.open(filename) #img.show() #provide image path as an input #convert it into base 64 #buffer = StringIO.StringIO() #buffer.write(_content) #file_data = buffer.getvalue() #print (file_data) #image = base64.b64encode( open( filename.url, "rb").read()) input = {"image": 'data:image/jpg;base64,' + image.decode('ascii')} client = Algorithmia.client('simb+HoZt1y2rsh4qvaHZ4pGbdy1') algo = client.algo('algorithmiahq/DeepFashion/1.2.2') #here c contains type of dress and its boundind box in a list of dictionaries c = (algo.pipe(input).result) #print c b = c['articles'] print(b) a, percentage = colorz(filename, b) #break; return a, percentage #detect('/Users/rashmisahu/Desktop/rashmi/internship/3.jpg')
def predict(): client = Algorithmia.client('simtds2YG9Ed/wd5xucmvHy+U8G1') algo = client.algo('Dmitry_BV/predictor/1.1.2') algo.set_options(timeout=100) # optional # cgr = "C12(C(CCC1C3C(C4(C(C(C3)=C)=CC(C[->=]C4)=O)CC#C)CC2)=O)C.[O-]" # Test input smi = request.args.get('post') beamSize = request.args.get('beamSize') model = request.args.get('model') input_query = {"reaction": smi, 'beamWidth': int(beamSize), "model": model} answers = algo.pipe(input_query).result["product"] # If CGR string received, performing decomposition; # Generating 2 SMILES strings (reactants and products) and 2 SVG images for them result_dict = {} if model == "cgr": decomposed = smiles(answers) decomposed_smiles, svg_list = decompose_cgr(decomposed) img = get_svg(decomposed) result_dict['decomposed_smiles'] = decomposed_smiles result_dict['decomposed_svg'] = svg_list elif model == "smiles": answers = smi + ">>" + answers img = get_svg(smiles(answers)) result_dict['prediction'] = answers result_dict['reaction'] = img return json.dumps(result_dict)
def load(): output = {} output['client'] = Algorithmia.client() output['pach_client'] = python_pachyderm.Client( host=os.environ["PACH_HOST"], port=os.environ["PACH_PORT"], auth_token=os.environ["PACH_AUTH"], tls=True) # Download the model and config Path("/tmp/trained_model").mkdir(parents=True, exist_ok=True) with open("/tmp/trained_model/config.json", "wb") as f: f.write(output['pach_client'].get_file(("train_model", MODEL_VERSION), "config.json").read()) with open("/tmp/trained_model/pytorch_model.bin", "wb") as f: f.write(output['pach_client'].get_file(("train_model", MODEL_VERSION), "pytorch_model.bin").read()) output['classification_model_path'] = Path("/tmp/trained_model/") output[ 'classification_model'] = AutoModelForSequenceClassification.from_pretrained( output['classification_model_path'], cache_dir=None, num_labels=3) nltk.download('stopwords') nltk.download('punkt') nltk.download('wordnet') return output
def Translate(string): input = {"action": "translate", "text": string} client = Algorithmia.client('sim61hYbFv8RiXBOE0Vclt62ifF1') algo = client.algo('translation/GoogleTranslate/0.1.1') a = algo.pipe(input) #print(a) return a.result['translation']
def results2(): username = str(request.form['UserName']) sentence = str(request.form['review']) client = Algorithmia.client('simSZn2DdvecQYvltU1jrAhh2es1') algo = client.algo('nlp/ProfanityDetection/1.0.0') algo.set_options(timeout=300) # optional a = algo.pipe(sentence).result if len(a.keys()) == 0: sid = SentimentIntensityAnalyzer() ss = sid.polarity_scores(sentence) if ss['compound'] < 0: score = 10 - abs((ss['compound'] * 10)) + 0.5 else: score = (ss['compound'] * 10) - 0.5 #now we will add the above details to the users info db so that he/she can view it in the history section with sqlite3.connect('softwareproject.db') as con: cur = con.cursor() cur.execute('SELECT Password from User_Auth where UserId =?' '', (username, )) correct_pass = cur.fetchall() user_pass = correct_pass[0][0] with sqlite3.connect('softwareproject.db') as con: cur = con.cursor() cur.execute('''INSERT INTO User_Info VALUES (?,?,?,?)''', (username, user_pass, sentence, score)) return render_template('results.html', res=score) else: return render_template('profanity.html')
def on_data(self, data): global tweet_text global tweet_counter global tweet_time global mood_average global mood data_json = json.loads(data) tweet_text = removeNonsense(data_json["text"]) tweet_time = data_json["created_at"][11:19] # Authenticates with Algorithmia client = Algorithmia.client('simMN5+/QIIoGAfFTxZtf9uPjHQ1') algorithm = client.algo('nlp/SocialSentimentAnalysis/0.1.3') text_formatted = '{"sentence":' + tweet_text + '}' analyzed_text = algorithm.pipe(text_formatted.encode("utf-8")) # analyzed_text = algorithm.pipe(text_formatted) # print(analyzed_text) analyzed_text_dict = analyzed_text[0] print("Tweet:", tweet_text) print("Time:", tweet_time) current_mood = analyzed_text_dict.items()[4][1] print("Current mood:", current_mood) if current_mood != 0: # Adds to graph s.write(dict(x=tweet_time, y=current_mood, text=tweet_text)) s2.write(dict(x=tweet_time, y=mood_average)) mood += current_mood mood_average = mood / tweet_counter print("Average mood out of {}: {}\n".format( tweet_counter, mood_average)) tweet_counter += 1.0 else: print("Tweet is ignored: too short or made no sense\n") return True
def extract_text(url): data = url client = Algorithmia.client('simWe97NtJhjsrvkB0qrvKXPijo1') algo = client.algo('ocr/RecognizeCharacters/0.3.0') result = algo.pipe(data).result return {"result": result}
def __init__(self, apply_func, load_func=None, client=None): """ Creates the adk object :param apply_func: A required function that can have an arity of 1-2, depending on if loading occurs :param load_func: An optional supplier function used if load time events are required, if a model manifest is provided; the function may have a single `manifest` parameter to interact with the model manifest, otherwise must have no parameters. :param client: A Algorithmia Client instance that might be user defined, and is used for interacting with a model manifest file; if defined. """ self.FIFO_PATH = "/tmp/algoout" if client: self.client = client else: self.client = Algorithmia.client() apply_args, _, _, _, _, _, _ = inspect.getfullargspec(apply_func) self.apply_arity = len(apply_args) if load_func: load_args, _, _, _, _, _, _ = inspect.getfullargspec(load_func) self.load_arity = len(load_args) if self.load_arity not in (0, 1): raise Exception( "load function expects 0 parameters or 1 parameter to be used to store algorithm state" ) self.load_func = load_func else: self.load_func = None self.apply_func = apply_func self.is_local = not os.path.exists(self.FIFO_PATH) self.load_result = None self.loading_exception = None self.manifest_path = "model_manifest.json" self.model_data = self.init_manifest(self.manifest_path)
def __init__(self): super(SearchResult, self).__init__() self.search_input = dict(articleName="", lang="en") self.search_term = '' self.client = Algorithmia.client(auth_key['algorithmia']['apikey']) self.algo = self.client.algo('web/WikipediaParser/0.1.2') self.algo.set_options(timeout=300) # optional
def resumir(self): input = self.text['content'], 50 client = Algorithmia.client('simyw+zYbXC1hUyLm4AVdUorUMD1') algo = client.algo('nlp/Summarizer/0.1.8') algo.set_options(timeout=300) # optional resumo = algo.pipe(input).result return resumo
def test_algorithmia(name): apiKey = 'simXTatsEpxaO+Ehudqy2iC+a/j1' client = Algorithmia.client(apiKey) imgs_directory = client.dir("data://karimelghazouly/imgs_directory") if imgs_directory.exists() is False: imgs_directory.create() acl = imgs_directory.get_permissions() acl.read_acl == AclType.my_algos imgs_directory.update_permissions(ReadAcl.private) imgs_directory.get_permissions().read_acl == AclType.private img = "data://karimelghazouly/imgs_directory/" + name if client.file(img).exists() is False: client.file(img).putFile("data/" + name) input = {"image": "data://karimelghazouly/imgs_directory/" + name} algo = client.algo('deeplearning/ColorfulImageColorization/1.1.13') link = algo.pipe(input).result['output'] last = client.file(link).getBytes() image = Image.open(io.BytesIO(last)) type = name.split('.', 1)[1] h = len(str(type)) + 1 new_name = name[:-h] image = image.convert("RGB") image.save('data/colored-algorithmia/' + str(new_name) + "out." + str(type)) image.show()
def main(): parser = argparse.ArgumentParser() parser.add_argument('--api-key', required=True, help='algorithmia api key') parser.add_argument('--connector-path', required=True, help='s3 or dropbox path for the directory to scan') parser.add_argument( '--recursive', action='store_true', help='continue scanning all sub-directories of the connector path') args = parser.parse_args() # Initialize Algorithmia Python client client = Algorithmia.client(args.api_key) # Get the algorithm we plan to use on each picture algo = client.algo('deeplearning/ColorfulImageColorization/0.1.16') algo.set_options( timeout=600 ) # This is a slow algorithm, so let's bump up the timeout to 10 minutes # The root level directory that we will traverse top_level_dir = client.dir(args.connector_path) # Colorize the files if args.recursive: recursivelyColorize(algo, args.connector_path, top_level_dir) else: colorizeFilesInDirectory(algo, args.connector_path, top_level_dir) print 'Done processing!'
def fetchContentFromWikipedia(self, searchTerm): algorithmiaAutenticaded = Algorithmia.client('') wikipediaAlgorithm = algorithmiaAutenticaded.algo( 'web/WikipediaParser/0.1.2') query = {"articleName": searchTerm, "lang": "en"} wikipediaResponse = wikipediaAlgorithm.pipe(query).result return wikipediaResponse
def analyze(data): client = Algorithmia.client('simWZTDYnnpmlZ6s1ETpRT5+SmT1') algo = client.algo('nlp/SocialSentimentAnalysis/0.1.4') compounds = [] positives = [] negatives = [] neutrals = [] for jsonObj in data: analysis = algo.pipe(jsonObj['text']).result[0] jsonObj['negative'] = analysis['negative'] jsonObj['neutral'] = analysis['neutral'] jsonObj['positive'] = analysis['positive'] jsonObj['compound'] = analysis['compound'] #used later for stats compounds.append(analysis['compound']) positives.append(analysis['positive']) negatives.append(analysis['negative']) neutrals.append(analysis['neutral']) master = [] master.append(negatives) master.append(neutrals) master.append(positives) master.append(compounds) sentiment_stats(master, data)
def fetch_content_from_source(article_name, lang): client = Algorithmia.client(config.ALGORITHMIA_CONFIG["client"]) algo = client.algo(ALGORITHMIA_ALGO_VERSION) return algo.pipe({ "articleName": article_name, "lang": lang }).result["content"]
def fashionA(path): input = {"image": "path", "model": "small", "tags_only": True} client = Algorithmia.client('simBELmBzPwtLZeK/XxdN/fd9dz1') algo = client.algo('algorithmiahq/DeepFashion/1.3.0') algo.set_options(timeout=300) # optional print(algo.pipe(input).result)
def broadcast(algorithmia_api_key, aws_creds, kinesis_stream_name, stream_fps, dynamo_table_name=None, algo_address=None): if algo_address: client = Algorithmia.client(algorithmia_api_key, api_address=algo_address) else: client = Algorithmia.client(algorithmia_api_key) print("starting broadcast", flush=True) session = credential_auth(aws_creds) consumer = create_consumer(kinesis_stream_name, session, dynamo_table_name) start_nginx() main_loop(client, consumer, stream_fps)
def auth(self, apikey, apiaddress, profile): #store api key in local config file and read from it each time a client needs to be created key = self.getconfigfile() config = toml.load(key) if ('profiles' in config.keys()): if (profile in config['profiles'].keys()): config['profiles'][profile]['api_key'] = apikey config['profiles'][profile]['api_server'] = apiaddress else: config['profiles'][profile] = { 'api_key': apikey, 'api_server': apiaddress } else: config['profiles'] = { profile: { 'api_key': apikey, 'api_server': apiaddress } } with open(key, "w") as key: toml.dump(config, key) self.ls(path=None, client=Algorithmia.client(self.getAPIkey(profile)))
def load(src, conf, use_gpu=False): """Load a PSPNet. This will initialise an instance of PSPNet pre-trained on the Cityscapes dataset. Parameters ---------- src: str The pre-trained model weights. conf: dict The model conf. (Must match pre-trained weights) use_gpu: bool, optional) If True, will try to use GPU. (Requires CUDA) Returns ------- pspnet.PSPNet An instance of PSPNet """ algo_client = Algorithmia.client() model = algo_client.file(src).getFile().name t = time.time() psp_net = PSPNet(pretrained_model=model, **conf) print("model loaded in {:d}ms".format(int(1000*(time.time()-t)))) if use_gpu: #chainer.cuda.get_device_from_id(0).use() psp_net.to_gpu() return psp_net
def make_prediction_v2(hours): # Cargamos los datos de temperatura y humedad desde mongo client = pymongo.MongoClient(mongo_client) dataset = client.forecast.training_data.find({}, {'_id': 0}) df = pd.DataFrame(dataset) df = df.dropna() df_little = df[0:100] client = Algorithmia.client('simSRYa+NFCPv34fZp/hN0jvbRt1') algo = client.algo('TimeSeries/Forecast/0.2.1') algo.set_options(timeout=300) # optional input_temp = [ list(df_little['TEMP']), hours, # series (hours) 1 # maxNumPeriods ] input_hum = [ list(df_little['HUM']), hours, # series (hours) 1 # maxNumPeriods ] primera_fecha = datetime.now() + timedelta(hours=3) rango_fechas = pd.date_range(primera_fecha.replace(second=0, microsecond=0), periods=hours, freq='H') prediction_temp = algo.pipe(input_temp).result prediction_hum = algo.pipe(input_hum).result return zip_result(rango_fechas, prediction_temp, prediction_hum)
def sentiment(text): input = { "document": text } client = Algorithmia.client('simLxeLhdObYV+LWP0AMm2xEt7D1') algo = client.algo('nlp/SentimentAnalysis/1.0.4') return algo.pipe(input).result#returns list of dictionaries,"document","sentiment"
def forward(request): input_text = json.loads(request.get_data(as_text=True))['text'] input = {"text": input_text} client = Algorithmia.client(ACCESS_KEY) algo = client.algo('ct83/bunyip_gpt_detector') algo.set_options(timeout=300) # optional return algo.pipe(input).result, 200
def publish_algo(mgmt_api_key, api_address, algo_schema, algo_name, algo_hash): client = Algorithmia.client(api_key=mgmt_api_key, api_address=api_address) algo = client.algo("{}/{}".format(algo_name, algo_hash)) results = algo.versions(1, published=True).results if len(results) > 0: cur_version = results[0]['version_info'] print("--- last release version : {} ---".format(cur_version)) else: print("--- working with fresh project (no previous release found)") if algo_schema not in ["major", "minor", "revision"]: raise Exception( "{} is not considered a valid algorithm version schema".format( algo_schema)) print("--- releasing new {}".format(algo_schema)) algo.publish(version_info={ "version_type": algo_schema, "release_notes": "automatically deployed by CI" }, settings={"royalty_microcredits": 0}, details={"label": "CICD"}) latest_version = algo.versions(1, published=True).results[0]['version_info'] print("--- new version {} successfully published".format(latest_version))
def init_sess(request): if request.body: # [ k.delete() for k in Session_data.objects.all() ] js = json.loads(request.body) sess_id = js['id'] spp = js['ss'] total_img = js['tt'] usr_data = {str(k): [] for k in range(total_img)} usr_data = json.dumps(usr_data) q = Session_data( session_id = sess_id, usr_data = usr_data, total_img = total_img, spp = spp, lock = 0 ) q.save() try: data = { "image": open('emo/tst_img.txt', 'r').read() , "numResults": 7 } client = Algorithmia.client("sim2Va+Ug0bYOz38n3C88wg+kzz1") algo = client.algo('deeplearning/EmotionRecognitionCNNMBP/1.0.1') result = algo.pipe(data).result print('test success') except: print('test failed') return HttpResponse('200')
def data_input(request): if request.method == "POST": image_file_name = request.FILES["image"].name audio_file_name = request.FILES["audio"].name image_file_binary = request.FILES["image"].read() audio_file_binary = request.FILES["audio"].read() speed = request.POST.get("speed") # Image Analysis dbx = dropbox.Dropbox(DROPBOX_ACCESS_TOKEN) image_path = "/images/" + str(uuid.uuid1()) + "__" + image_file_name dbx.files_upload(image_file_binary, image_path, mute=True) client = Algorithmia.client(ALGO_ACCESS_KEY) algo = client.algo(ALGO_EMOTION_API) params = {} params["image"] = "dropbox://" + image_path params["numResults"] = 7 image_analysis = algo.pipe(params).result # speech Analysis speech_to_text = SpeechToTextV1(username=WATSON_SPT_SERVICE_USERNAME, password=WATSON_SPT_SERVICE_PASSWORD, x_watson_learning_opt_out=False) models = speech_to_text.models() us_model = speech_to_text.get_model('en-US_BroadbandModel') results = speech_to_text.recognize(audio_file_binary, content_type='audio/wav', timestamps=True, word_confidence=True, speaker_labels=True) transcripts = get_transcripts(json.dumps(results)) transcripts_str = ". ".join(transcripts) tone_analysis = call_to_watson_tone_analysis_api(transcripts_str) response = format(image_analysis, tone_analysis) response.update({"speed": float(speed), "weather": 0}) (score, (msg, aloc)) = decision_engine.decide(response) # a = decision_engine.decide(response) # # if a: # score = a[0] # msg = a[1][0] # aloc = a[1][1] #import ipdb; ipdb.set_trace() return render(request, 'results.html', { "score": score, "msg": msg, "score_breakup": response }) #return render(request, 'results.html') #return HttpResponseRedirect("/emoDrive/analyze/" + upload_path) else: return render(request, 'upload.html')
def abstractToKeyword(title): process = subprocess.Popen('python scholar.py -c %(count)d -A %(text)s --csv' \ % {"count": 1, "text": title}, shell=True, stdout=subprocess.PIPE) output=process.communicate()[0] elements = output.rsplit('|') lastIndex = len(elements) - 1 LDAinput = [[elements[0], elements[lastIndex]],1] client = Algorithmia.client('simfAKlzXJA516uRJm37b8tT9b31') algo = client.algo('kenny/LDA/0.1.3') results = algo.pipe(LDAinput) return list(results[0].keys())
def generate_sentence(filepath): ''' Generates a sentence given a trained trigram model PARAMETERS: <str> filepath: location that trained model is located in Algorithmia API RETURNS: <str> output: a randomly generated sentence ''' client = Algorithmia.client(api_key.key) input = [filepath, "xxBeGiN142xx", "xxEnD142xx"] algo = client.algo('ngram/RandomTextFromTrigram/0.1.1') print algo.pipe(input)
def test_create_acl(self): c = Algorithmia.client(os.environ['ALGORITHMIA_API_KEY']) dd = DataDirectory(c, 'data://.my/privatePermissions') if dd.exists(): dd.delete(True) dd.create(ReadAcl.private) dd_perms = DataDirectory(c, 'data://.my/privatePermissions').get_permissions() self.assertEquals(dd_perms.read_acl, AclType.private) dd.update_permissions(ReadAcl.public) dd_perms = DataDirectory(c, 'data://.my/privatePermissions').get_permissions() self.assertEquals(dd_perms.read_acl, AclType.public)
def random(): try: query = wikipedia.random(pages=1) input = wikipedia.WikipediaPage(title=query).summary title = wikipedia.WikipediaPage(title=query).title image = wikipedia.WikipediaPage(title=query).images[0] client = Algorithmia.client('Simple simR+{}'.format(api_key)) algo = client.algo('nlp/Summarizer/0.1.2') contents ={ 'image': image, 'title': title, 'summary': algo.pipe(input), 'link': 'https://en.wikipedia.org/wiki/{}'.format(wikipedia.random(pages=1)) } except: return json.dumps({ 'msg': "Sorry, we couldn't find a Wikipedia article matching your search." }) return json.dumps(contents)
def pull_tweets(): """Pull tweets from Twitter API via Algorithmia.""" input = { "query": q_input, "numTweets": "700", "auth": { "app_key": 'your_consumer_key', "app_secret": 'your_consumer_secret_key', "oauth_token": 'your_access_token', "oauth_token_secret": 'your_access_token_secret' } } client = Algorithmia.client('your_algorithmia_api_key') algo = client.algo('twitter/RetrieveTweetsWithKeyword/0.1.3') tweet_list = [{'user_id': record['user']['id'], 'retweet_count': record['retweet_count'], 'text': record['text']} for record in algo.pipe(input).result] return tweet_list
def summarise_img(src, options=False): ''' Retrieve meta-data for an image web resource. Use algorithmia, openshift or similar cloud service. ''' import Algorithmia client = Algorithmia.client(config.ALGORITHMIA['api_key']) algo = client.algo('deeplearning/IllustrationTagger/0.2.3') input = {"image":src} if options: # tags (optional) required probs for opt, value in options.items(): input[opt] = value # e.g. threshold 0.3 etc result = algo.pipe(input) return result
def main(filepath, outpath, length): story = '' client = Algorithmia.client(api_key.key) alg_path = "data://.algo/ngram/GenerateTrigramFrequencies/temp/trigrams.txt" generate_trigrams(filepath, alg_path) while len(re.findall(r'\w+', story)) < length: print "Generating new paragraph..." input = ["data://.algo/ngram/GenerateTrigramFrequencies/temp/trigrams.txt", "xxBeGiN142xx", "xxEnD142xx", (randint(1,9))] new_par = client.algo('/lizmrush/GenerateParagraphFromTrigram/0.1.2').pipe(input) if len(re.findall(r'\w+', story)) + len(re.findall(r'\w+', new_par)) > length: break story += new_par.strip() story += '\n\n' print "Word count:" print len(re.findall(r'\w+', story)) with open(outpath, 'w') as f: f.write(story.encode('utf8')) f.close() print "Complete! Story written to " + outpath
def generate_trigrams(corpus, filepath): ''' Generates a trained trigram model PARAMETERS: str[] corpus: array of strings generated from splitting the original corpus. Needs beginning and end tags in data <str> filepath: location that data is stored in Algorithmia data API RETURNS: filepath: location that data is stored in Algorithmia data API (as confirmation) ''' with open(corpus, 'r') as myfile: data = myfile.read().replace('\n', '') data = data.replace("xxEnD142xx", "xxEnD142xx qq") data = data.split(" qq ") input = [data, "xxBeGiN142xx", "xxEnD142xx", filepath] client = Algorithmia.client(api_key.key) algo = client.algo('ngram/GenerateTrigramFrequencies/0.1.1') print "Trigram Frequency txt in data api, filepath is:" print algo.pipe(input)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--api-key', required=True, help='algorithmia api key') parser.add_argument('--connector-path', required=True, help='s3 or dropbox path for the directory to scan') parser.add_argument('--recursive', action='store_true', help='continue scanning all sub-directories of the connector path') args = parser.parse_args() # Initialize Algorithmia Python client client = Algorithmia.client(args.api_key) # Get the algorithm we plan to use on each picture algo = client.algo('deeplearning/ColorfulImageColorization/1.0.1') algo.set_options(timeout=600) # This is a slow algorithm, so let's bump up the timeout to 10 minutes # The root level directory that we will traverse top_level_dir = client.dir(args.connector_path) # Colorize the files if args.recursive: recursivelyColorize(algo, args.connector_path, top_level_dir) else: colorizeFilesInDirectory(algo, args.connector_path, top_level_dir) print 'Done processing!'
import Algorithmia client = Algorithmia.client('sim4TVSxcIiUod91joxShOZIcMR1') docslist = [ "Machine Learning is Fun Part 5: Language Translation with Deep Learning and the Magic of Sequences", "Paddle: Baidu's open source deep learning framework", "An overview of gradient descent optimization algorithms", "Create a Chatbot for Telegram in Python to Summarize Text", "Image super-resolution through deep learning", "World's first self-driving taxis debut in Singapore", "Minds and machines: The art of forecasting in the age of artificial intelligence" ] # The LDA required input using a list of documents lda_input = { "docsList": docslist } # LDA algorithm: https://algorithmia.com/algorithms/nlp/LDA lda = client.algo('nlp/LDA/1.0.0') # Returns a list of dictionaries of trends result = lda.pipe(lda_input).result # LDA Mapping algorithm: https://algorithmia.com/algorithms/nlp/LDAMapper lda_mapper = client.algo( 'nlp/LDAMapper/0.1.1') # LDA Mapper input using the LDA algorithm's result as 'topics' value lda_mapper_input = {
import Algorithmia import json # The domain to crawl and number of links deep # More here: https://algorithmia.com/algorithms/web/SiteMap input = ["http://algorithmia.com",1] # Replace YOUR API KEY with you free Algorithmia key # https://algorithmia.com/signup client = Algorithmia.client('YOUR API KEY') # Here we call the Site Map algorithm res = client.algo('web/SiteMap/0.1.7').pipe(input) siteMap = res.result links = [] output = [] # Iterate through the key-value pairs from the site map graph # adding every URL to the links array for keyLink in siteMap: links.append(keyLink) for valLink in siteMap[keyLink]: links.append(valLink) # Remove duplicate links from the links array links = list(set(links)) # Iterate through the links calling Analyze URL on each # Then add the object to the output array
def setUp(self): self.client = Algorithmia.client(os.environ['ALGORITHMIA_API_KEY'])
import Algorithmia import requests # Algorithmia API key here client = Algorithmia.client("ALGORITHMIA_API_KEY") example_input = { "url": "http://algorithmia.com/", "depth": 3 } res = client.algo("web/ErrorScanner").set_options(timeout=2000).pipe(example_input) broken_links = res.result["brokenLinks"] email_str = "" # Iterate through our list of broken links # to create a string we can add to the body of our email for linkPair in broken_links: email_str += "broken link: " + linkPair["brokenLink"] + " (referring page: " + linkPair["refPage"] + ")" + "\n\n" # Print the result from the API call print email_str # Send the email def send_simple_message(): return requests.post( # Mailgun Documentation: https://documentation.mailgun.com/quickstart-sending.html#send-via-api "https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages", auth=("api", "YOUR_API_KEY"),
import re from collections import defaultdict, Counter import Algorithmia client = Algorithmia.client("your_algorithmia_api_key") def pull_tweets(): input = { "query": "Thanksgiving", "numTweets": "700", "auth": { "app_key": 'your_consumer_key', "app_secret": 'your_consumer_secret_key', "oauth_token": 'your_access_token', "oauth_token_secret": 'your_access_token_secret' } } twitter_algo = client.algo("twitter/RetrieveTweetsWithKeyword/0.1.3") result = twitter_algo.pipe(input).result tweet_list = [tweets['text'] for tweets in result] return tweet_list def process_text(): """Remove emoticons, numbers etc. and returns list of cleaned tweets.""" data = pull_tweets() regex_remove = "(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)|^RT|http.+?" stripped_text = [ re.sub(regex_remove, '', tweets).strip() for tweets in data
list(string.punctuation) + ['http', 'https']) tokens = word_tokenize(s) cleanup = [token.lower() for token in tokens if token.lower() not in stopset and len(token) > 2] return cleanup # data = load_json('summer_transfer.json')['tweets'] data = load_json('transfer.json')['tweets'] langs = [] text = '\n'.join([(d['text']) for d in data.values()]) # fdist = FreqDist(cleanupDoc(text)) # freq = pd.DataFrame(dict(fdist), index=['freq']).T # freq.sort_values('freq', ascending=False, inplace=True) # # freq.to_csv('freq_winter') # print(freq.head(100)) import Algorithmia input = [ text, 2, 5, False, True ] input = text # "An engineer is trying to design a faster submarine. \nWould she prefer to study a fish or a flock of birds?" client = Algorithmia.client('simkxwJR9Pt23FxpLaN6755Gq4U1') algo = client.algo('dbgannon/KeyPhrases/0.1.1') print(algo.pipe(input))
def search(self, tweet_content, algo_path): client = Algorithmia.client(settings.ALGORITHMIA_API_KEY) algo = client.algo(algo_path) return (algo.pipe(tweet_content))
import Algorithmia import shutil import os print(os.getcwd()) input = ["https://i.imgur.com/U9j0CZj.jpg", "data://.algo/temp/result.jpg"] Algorithmia.apiAddress = 'https://api-algorithmia-com-2wstoj4gszx4.runscope.net' client = Algorithmia.client('simIMbhKq/Y4wc/maGC8Nr30Jzc1') algo = client.algo('opencv/EyeDetection/0.1.1') filename = algo.pipe(input) print(filename) fileExists = client.file("data://.algo/opencv/ObjectDetectionWithModels/temp/result.jpg").exists() print(fileExists) ourfile = client.file("data://.algo/opencv/ObjectDetectionWithModels/temp/result.jpg").getFile() print(ourfile.name) shutil.copy(ourfile.name, "/Users/Ishan/Hackpoly-2016/EyeDetect/bin/result.jpg")
#Get the user name first x =raw_input('Enter your name:') print('Hello ' + x) y =raw_input('How are you doing today,' +x) import Algorithmia input = y client = Algorithmia.client('simsmuMGjwhXqpi7hcakzab+RoG1') algo = client.algo('nlp/SentimentAnalysis/0.1.2') print algo.pipe(input)
def somefunction(input): client = Algorithmia.client('simL4K0sq9xovn9rSUqxzGy19R/1') algo = client.algo('mtman/SentimentAnalysis/0.1.1') ans = algo.pipe(input).result return ans
import Algorithmia from flask import Flask from flask import render_template from settings import * from datetime import timedelta from withings import WithingsApi, WithingsCredentials ALGORITHMIA_CLIENT = Algorithmia.client(ALGORITHMIA_KEY) SIMPLE_MOVING_AVERAGE = ALGORITHMIA_CLIENT.algo('TimeSeries/SimpleMovingAverage/0.2.0') FORECAST = ALGORITHMIA_CLIENT.algo('TimeSeries/Forecast/0.2.0') app = Flask(__name__) global FORECAST_ON_AVERAGE FORECAST_ON_AVERAGE = False # naff cahce to say on API calls while testing global RESULTS RESULTS = False @app.route("/") def withings(): global RESULTS if not RESULTS:
import Algorithmia #Set your Algorithmia API Key apiKey = 'YOUR API KEY GOES HERE' #Initialize Algorithmia Python client client = Algorithmia.client(apiKey) #Pick Algorithm to use algo = client.algo('opencv/SmartThumbnail/1.0.4') #Set folder URI path uri = "dropbox://Camera Uploads" #Iterate over the Dropbox folder containing images for f in client.dir(uri).list(): #Check file type is an image if f.getName().lower().endswith(('.png','.jpg','.jpeg','.bmp','.gif')): #Image progress write print "Reading " + f.getName() #Define input for Algorithm + Parameters input = [uri + '/' + f.getName(), uri + '/thumbnail_' + f.getName(), 300, 300, "FALSE"] #Call Algorithm output = algo.pipe(input) print "Thumbnailing: thumbnail_" + f.getName()
import Algorithmia import urllib import urllib2 import json # 1.1 URL to text # retrieves text file from URL # Str -> Str input = "your URL here" client = Algorithmia.client('simfMcTKIsg3/P+fl/1ENYWrnQd1') algo = client.algo('util/ExtractText/0.1.0') doc = algo.pipe(input) # 1.2 Summarizer # summarizes info from 1.1 # Str -> Str input = doc client = Algorithmia.client('simfMcTKIsg3/P+fl/1ENYWrnQd1') algo = client.algo('nlp/Summarizer/0.1.2') summarized = algo.pipe(input) # 1.3 Sentence detection # breaks down contents from 1.2 # Str -> listof Str input = summarized client = Algorithmia.client('simfMcTKIsg3/P+fl/1ENYWrnQd1') algo = client.algo('ApacheOpenNLP/SentenceDetection/0.1.0') sentences = algo.pipe(input)
import Algorithmia import os import json client = Algorithmia.client('YOUR_API_KEY_HERE') algo = client.algo('nlp/ProfanityDetection') rootdir = './clean_books/set_one/' output_file = 'set_one_profanity_results.txt' results = '' for subdir, dirs, files in os.walk(rootdir): for filename in files: with open(rootdir + filename, 'r') as content_file: input = content_file.read() print "Detecting profanity in " + filename results += filename + "\n\n" results += json.dumps(algo.pipe(input)) results += "\n\n" with open(output_file, 'w') as f: f.write(results) f.close() print "Done!"
import Algorithmia input = "https://pbs.twimg.com/profile_images/714630467409018884/2ywNrMx2.jpg" client = Algorithmia.client('simmp0NmxBIAkbVwazmgI8QQvMg1') algo = client.algo('sfw/NudityDetection/1.1.4') print algo.pipe(input)
def __init__(self): self.client = Algorithmia.client()
def handle_data(self, data): if self.recording: completeName = os.path.join(path_pos,"newfile%s.txt" %i) file = open(completeName, "a") result = re.sub(",", ";", str(data)) file.write(result+'\n') file.close() completeName1 = os.path.join(path_neg,"newfile%s.txt" %i) file1 = open(completeName1, "a") result1 = re.sub(",", ";", str(data)) file1.write(result1+'\n') file1.close() completeName2 = os.path.join(path_neu,"newfile%s.txt" %i) file2 = open(completeName2, "a") result2 = re.sub(",", ";", str(data)) file2.write(result2+'\n') file2.close() if self.isp: if not str(data).startswith('Average') and not "Number of Reviews" in str(data): input = str(data) re.sub(r'[^\x00-\x7F]+','', input) if input != None and type(input) == type(u"") and input.find(u'\u2019') >= 0 : input = input.replace(u'\u2019', '\'') if input != None and type(input) == type(u"") and input.find(u'\u201c') >= 0 : input = input.replace(u'\u201c', '\"') if input != None and type(input) == type(u"") and input.find(u'\u2026') >= 0 : input = input.replace(u'\u2026', '\...') if input != None and type(input) == type(u"") and input.find(u'\u2013') >= 0 : input = input.replace(u'\u2013', '\-') if input != None and type(input) == type(u"") and input.find(u'\u2018') >= 0 : input = input.replace(u'\u2018', '\'') if input != None and type(input) == type(u"") and input.find(u'\u2011') >= 0 : input = input.replace(u'\u2011', '\-') if input != None and type(input) == type(u"") and input.find(u'\u2014') >= 0 : input = input.replace(u'\u2014', '\-') if input != None and type(input) == type(u"") and input.find(u'\u201d') >= 0 : input = input.replace(u'\u201d', '\"') client = Algorithmia.client('sim0Msfwf+ITgS1ZUBm6dB5X5dw1') algo = client.algo('nlp/SentimentAnalysis/0.1.1') polarity = str(algo.pipe(input)) if polarity == '1': completeName = os.path.join(path_neg,"newfile%s.txt" %i) file = open(completeName, "a") file.write(input + '\n') file.close() if polarity == '2': completeName = os.path.join(path_neu,"newfile%s.txt" %i) file = open(completeName, "a") file.write(input + '\n') file.close() if polarity == '3': completeName = os.path.join(path_pos,"newfile%s.txt" %i) file = open(completeName, "a") file.write(input + '\n') file.close() if self.israting: #if str(data).startswith('Average'): result = re.sub("<.*?>", "", str(data)) result = re.sub("\n\t\r", "", str(data)) result = result.strip() print (result) #if not result.startswith('Reviews') and not result.startswith('Fresh') and not result.startswith('Rotten'): if "/10" in result: completeName = os.path.join(path_pos,"newfile%s.txt" %i) file = open(completeName, "a") file.write(result+',') file.close() completeName1 = os.path.join(path_neg,"newfile%s.txt" %i) file1 = open(completeName1, "a") file1.write(result+',') file1.close() completeName2 = os.path.join(path_neu,"newfile%s.txt" %i) file2 = open(completeName2, "a") file2.write(result+',') file2.close()