Sick Gaming
[Tut] How to Generate Text Automatically With Python? A Guide to the DeepAI API - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] How to Generate Text Automatically With Python? A Guide to the DeepAI API (/thread-98189.html)



[Tut] How to Generate Text Automatically With Python? A Guide to the DeepAI API - xSicKxBot - 11-10-2020

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

<div><figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe title="[DeepAI Python] How to Generate Text Automatically With Python?" width="1400" height="788" src="https://www.youtube.com/embed/OhEDGy7wmm4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<p>Do you want to enrich your Python script with powerful text-generation capabilities? You’re in the right place! </p>
<p><em><strong>What does it do?</strong> I just discovered DeepAI’s API that automatically generates a body of text, given a sentence fragment or topic keyword. </em></p>
<p><strong><em>How can it be used? </em></strong><em>You can use this as a basis to generate text automatically. </em></p>
<p><strong><em>My opinion: </em></strong><em>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). </em></p>
<h2>Python Deep API Call</h2>
<p>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’. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import requests
r = requests.post( "https://api.deepai.org/api/text-generator", data={ 'text': 'intelligence', }, headers={'api-key': 'quickstart-QUdJIGlzIGNvbWluZy4uLi4K'}
)
print(r.json()['output'])
</pre>
<p>You import Python’s standard library <code data-enlighter-language="generic" class="EnlighterJSRAW">requests</code> to issue web requests and access the DeepAI API that is hosted at the URL <code data-enlighter-language="generic" class="EnlighterJSRAW">"https://api.deepai.org/api/text-generator"</code>. </p>
<p>Here’s the output the code snippet generated in my Python shell:</p>
<h2>Automatically-Generated Text Example</h2>
<p><em>Intelligence officials were able to confirm the existence of at least some Russian hacking operations, including the one apparent aimed at the White House.</em></p>
<p><em>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.”</em></p>
<p><em>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.”</em></p>
<p><em>The CIA brief also claimed the CIA “should not” have “repeated, misleading details of Russian officials’ conduct.”</em></p>
<p><em>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.</em></p>
<p><em>“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.”</em></p>
<p><em>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.</em></p>
<p><em>Brennan told me the U.S would “make public its best communications in Russia for all time.”</em></p>
<p><em>Brennan’s testimony was confirmed by CIA Director Gina Haspel.</em></p>
<p><em>Brennan’s public admission that the Clinton campaign was hacked “was, in fact, rejected by the CIA by the president,” the CIA brief said.</em></p>
<p><em>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.</em></p>
<p><em>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.”</em></p>
<p><em>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.</em></p>
<p><em>The CIA concluded that the Russian government hacked the DNC to help Trump and was trying to influence the 2016 election.</em></p>
<p><em>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.”</em></p>
<p><em>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.</em></p>
<p><em>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.</em></p>
<p><em>“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.”</em></p>
<p><em>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.</em></p>
<p><em>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.</em></p>
<p><em>The hacking groups have also said that as part of the election, they were targeting an array of Democratic political candidates.</em></p>
<p><em>The Russian military is responsible for the attack, according to Kremlin spokesman Dmitry Peskov.</em></p>
<p><em>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.</em></p>
<p><em>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.</em></p>
<p><em>“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.</em></p>
<p><em>He said the Kremlin is not to blame for a country’s cyber crimes against the country during the presidential election.</em></p>
<p><em>Putin also said that “there is nothing new or wrong with the election result.”</em></p>
<h2>Short Discussion</h2>
<p>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:</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="378" src="https://blog.finxter.com/wp-content/uploads/2020/11/image-15-1024x378.png" alt="" class="wp-image-16608" srcset="https://blog.finxter.com/wp-content/uploads/2020/11/image-15-1024x378.png 1024w, https://blog.finxter.com/wp-content/uploads/2020/11/image-15-300x111.png 300w, https://blog.finxter.com/wp-content/uploads/2020/11/image-15-768x284.png 768w, https://blog.finxter.com/wp-content/uploads/2020/11/image-15-1536x568.png 1536w, https://blog.finxter.com/wp-content/uploads/2020/11/image-15-150x55.png 150w, https://blog.finxter.com/wp-content/uploads/2020/11/image-15.png 1878w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<p>The text has high writing quality and is original!</p>
<p>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. </p>
<h2>Try It Yourself (Interactive Shell)</h2>
<p> <iframe height="400px" width="100%" src="https://repl.it/@finxter/MildRemoteExperiments?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p>
<h2>Challenge</h2>
<p><strong>Challenge</strong>: 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:</p>
<p>I’ll share the results of this poll in a follow-up blog article—so, <a href="https://blog.finxter.com/subscribe/" target="_blank" rel="noreferrer noopener" title="Subscribe">stay tuned</a>!</p>
<p>The post <a href="https://blog.finxter.com/python-api-deepai-generate-text/" target="_blank" rel="noopener noreferrer">How to Generate Text Automatically With Python? A Guide to the DeepAI API</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p>
</div>


https://www.sickgaming.net/blog/2020/11/08/how-to-generate-text-automatically-with-python-a-guide-to-the-deepai-api/