How to Generate Text Automatically With Python? A Guide to the DeepAI API

Rate this post
[DeepAI Python] How to Generate Text Automatically With Python?

Do you want to enrich your Python script with powerful text-generation capabilities? You’re in the right place!

What does it do? I just discovered DeepAI’s API that automatically generates a body of text, given a sentence fragment or topic keyword.

How can it be used? You can use this as a basis to generate text automatically.

My opinion: The generated text makes sense (kind of) but you may need to further process it or guide it to generate longer meaningful content. The biggest opportunity, in my opinion, is to use it as a step in a more complex pipeline towards the automatic generation of valuable content. On its own, it wouldn’t generate too much meaning (apart from the entertainment value of reading machine-generated text).

Python Deep API Call

Ready? So, let’s have a look at the short Python script that asks the machine learning model to generate text for you—given a certain keyword such as ‘intelligence’, ‘Donald Trump’, or ‘Learn Python’.

import requests
r =
        'text': 'intelligence',
    headers={'api-key': 'quickstart-QUdJIGlzIGNvbWluZy4uLi4K'}

You import Python’s standard library requests to issue web requests and access the DeepAI API that is hosted at the URL "".

Here’s the output the code snippet generated in my Python shell:

Automatically-Generated Text Example

Intelligence officials were able to confirm the existence of at least some Russian hacking operations, including the one apparent aimed at the White House.

The CIA brief, the statement said, “was made official by the Russian Government on the third assessment of a U.S. official that Russian Government officials had interfered in the 2016 presidential election. In my opinion, assertions that were made in the clear and unequivocal testimony of the public as well as in the public release on any of the Russian accounts were grossly improper, misleading, and should be seen to be completely absent from any official documents as well.”

It added: “In addition, the CIA’s assessment asserted the Russian Government’s claims were grossly misleading, misleading and misleading in their assertions. The CIA has concluded to date, including publicly, it has given credible facts to support the Russia’s claims, and there would be no justification for further claims, if such assertions are to be proven to be false.”

The CIA brief also claimed the CIA “should not” have “repeated, misleading details of Russian officials’ conduct.”

CIA spokeswoman Jane Harman told Fox News: “CIA Director John Brennan fully agrees that Director Brennan’s testimony confirms what we have heard from various intelligence agencies.

“He is clear that Director Brennan gave additional testimony to Congress on the subject, in the first instance in which he spoke frankly about the role of Russian intelligence.”

But Harman also said Brennan should have given “more proof” of “firm Russian intervention into the U.S. political environment” if such claims were to be believed about the same thing.

Brennan told me the U.S would “make public its best communications in Russia for all time.”

Brennan’s testimony was confirmed by CIA Director Gina Haspel.

Brennan’s public admission that the Clinton campaign was hacked “was, in fact, rejected by the CIA by the president,” the CIA brief said.

In a memo prepared to be published early Friday, CIA Director R. John Brennan outlined an intelligence assessment that a Russian national and political operative in the United States was responsible for leaking classified information to the Russian media and opposition leaders.

The CIA has denied the CIA’s assessment. But Brennan argued that the Russian “public claims are simply false assertions that are misleading, unconfirmed and improper.”

In the report, US intelligence provided more details to the National Security Council than a year ago, the first time in the United States that Russia was involved in the hack.

The CIA concluded that the Russian government hacked the DNC to help Trump and was trying to influence the 2016 election.

Russia has denied US attempts to influence the campaign. But President Obama, in a letter to US Secretary of State Rex Tillerson, said the US acted in “complete coordination with the Russian government” and “with Russian military officials, whom we have discussed with other countries.”

Russian election meddling and hacking of DNC were part of an international pattern of human rights violations in which countries have accused their governments of using disinformation to advance a political candidate and undermine a democratic election.

Dmitry Peskov, Russia’s president, said in December that the hacking was meant to interfere with the presidential election and may violate the election regulations.

“The cyberwar waged in this new Russia will not be stopped, the threats and security of the country will be the only legitimate measure,” he said, adding that the hackers “will have no chance to stop.”

The Russian state-sponsored cybercrime group F-Secure, which was based in Moscow, has claimed responsibility for a number of Russian cyber incidents that have been claimed by the United States, the Associated Press reports.

The Russian state-sponsored hacking groups include the computer firms Kaspersky Lab and DigiPG, both known for their malware and research programs, and the Moscow-based anti-virus firm Elemental.

The hacking groups have also said that as part of the election, they were targeting an array of Democratic political candidates.

The Russian military is responsible for the attack, according to Kremlin spokesman Dmitry Peskov.

Russia is often accused of using cyber hacks for its own interests. In December 2016, an Obama administration official stated that “every step is worth watching carefully” in the war in Afghanistan in 2014.

But Russia’s President Vladimir Putin, a Russian citizen and former head of the Communist Party, has denied that Russian state-backed separatists used hacking to support the presidential elections.

“I call on all government officials and political parties to avoid interference and the international community to take up arms for the political and economic purposes of Russia. We are not engaged, and should not be used,” Putin said in a speech in Moscow in February.

He said the Kremlin is not to blame for a country’s cyber crimes against the country during the presidential election.

Putin also said that “there is nothing new or wrong with the election result.”

Short Discussion

You can see that the generated text is quite detailed and looks professional. But is it correct? And does it contain plagiarism? To check these questions, I checked it with Grammarly. Here’s the result:

The text has high writing quality and is original!

But it’s obviously fake news—otherwise, Grammarly should have found the quotes of “CIA officials”. That’s why I think that the powerful text-generation ability should be used in a pipeline or system that ensures to create some real value-add—rather than using it as a stand-alone tool.

Try It Yourself (Interactive Shell)


Challenge: Find ways to create real value using the Python API call in a more advanced code snippet and share it with the Finxter community. You can contact me by signing up on the Finxter Email Academy:

I’ll share the results of this poll in a follow-up blog article—so, stay tuned!