Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds

How-To Tutorials - Data Science

9 Articles
article-image-connecting-cloud-object-storage-with-databricks-unity-catalog
Pulkit Chadha
07 Nov 2024
10 min read
Save for later

Connecting Cloud Object Storage with Databricks Unity Catalog

Pulkit Chadha
07 Nov 2024
10 min read
This article is an excerpt from the book, Data Engineering with Databricks Cookbook, by Pulkit Chadha. This book shows you how to use Apache Spark, Delta Lake, and Databricks to build data pipelines, manage and transform data, optimize performance, and more. Additionally, you’ll implement DataOps and DevOps practices, and orchestrate data workflows.IntroductionDatabricks Unity Catalog allows you to manage and access data in cloud object storage using a unified namespace and a consistent set of APIs. With Unity Catalog, you can do the following: Create and manage storage credentials, external locations, storage locations, and volumes using SQL commands or the Unity Catalog UI Access data from various cloud platforms (AWS S3, Azure Blob Storage, or Google Cloud Storage) and storage formats (Parquet, Delta Lake, CSV, or JSON) using the same SQL syntax or Spark APIs Apply fine-grained access control and data governance policies to your data using Databricks SQL Analytics or Databricks Runtime In this article, you will learn what Unity Catalog is and how it integrates with AWS S3. Getting ready Before you start setting up and configuring Unity Catalog, you need to have the following prerequisites: A Databricks workspace with administrator privileges A Databricks workspace with the Unity Catalog feature enabled A cloud storage account (such as AWS S3, Azure Blob Storage, or Google Cloud Storage) with the necessary permissions to read and write data How to do it… In this section, we will first create a storage credential, the IAM role, with access to an s3 bucket. Then, we will create an external location in Databricks Unity Catalog that will use the storage credential to access the s3 bucket. Creating a storage credential You must create a storage credential to access data from an external location or a volume. In this example, you will create a storage credential that uses an IAM role taccess the S3 Bucket. The steps are as follows: 1. Go to Catalog Explorer: Click on Catalog in the left panel and go to Catalog Explorer. 2. Create storage credentials: Click on +Add and select Add a storage credential. Figure 10.1 – Add a storage credential 3. Enter storage credential details: Give the credential a name, the IAM role ARN that allows Unity Catalog to access the storage location on your cloud tenant, and a comment if you want, and click on Create. Figure 10.2 – Create a new storage credential Important note To learn more about IAM roles in AWS, you can reference the user guide here: https:// docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html. 4. Get External ID: In the Storage credential created dialog, copy the External ID value and click on Done. Figure 10.3 – External ID for the storage credential 5. Update the trust policy with an External ID: Update the trust policy associated with the IAM role and add the External ID value for sts:ExternalId:  Figure 10.4 – Updated trust policy with External ID Creating an external location An external location contains a reference to a storage credential and a cloud storage path. You need to create an external location to access data from a custom storage location that Unity Catalog uses to reference external tables. In this example, you will create an external location that points to the de-book-ext-loc folder in an S3 bucket. To create an external location, you can follow these steps: 1. Go to Catalog Explorer: Click on Catalog in the left panel to go to Catalog Explorer. 2. Create external location: Click on +Add and select Add an external location:  Figure 10.5 – Add an external location 3. Pick an external location creation method: Select Manual and then click on Next:  Figure 10.6 – Create a new external location 4. Enter external location details: Enter the external location name, select the storage credential, and enter the S3 URL; then, click on the Create button:  Figure 10.7 – Create a new external location manually 5. Test connection: Test the connection to make sure you have set up the credentials accurately and that Unity Catalog is able to access cloud storage:  Figure 10.8 – Test connection for external location If everything is set up right, you should see a screen like the following. Click on Done:  Figure 10.9 – Test connection results See also Databricks Unity Catalog: https://www.databricks.com/product/unity-catalog What is Unity Catalog: https://docs.databricks.com/en/data-governance/ unity-catalog/index.html Databricks Unity Catalog documentation: https://docs.databricks.com/en/ compute/access-mode-limitations.html Databricks SQL documentation: https://docs.databricks.com/en/datagovernance/unity-catalog/create-tables.html Databricks Unity Catalog: A Comprehensive Guide to Features, Capabilities, and Architecture: https://atlan.com/databricks-unity-catalog/ Step By Step Guide on Databricks Unity Catalog Setup and its key Features: https:// medium.com/@sauravkum780/step-by-step-guide-on-databricks-unitycatalog-setup-and-its-features-1d0366c282b7 Conclusion In summary, connecting to cloud object storage using Databricks Unity Catalog provides a streamlined approach to managing and accessing data across various cloud platforms such as AWS S3, Azure Blob Storage, and Google Cloud Storage. By utilizing a unified namespace, consistent APIs, and powerful governance features, Unity Catalog simplifies the process of creating and managing storage credentials and external locations. With built-in fine-grained access controls, you can securely manage data stored in different formats and cloud environments, all while leveraging Databricks' powerful data analytics capabilities. This guide walks through setting up an IAM role and creating an external location in AWS S3, demonstrating how easy it is to connect cloud storage with Unity Catalog. Author BioPulkit Chadha is a seasoned technologist with over 15 years of experience in data engineering. His proficiency in crafting and refining data pipelines has been instrumental in driving success across diverse sectors such as healthcare, media and entertainment, hi-tech, and manufacturing. Pulkit’s tailored data engineering solutions are designed to address the unique challenges and aspirations of each enterprise he collaborates with.
Read more
  • 0
  • 0
  • 1295

article-image-the-big-brand-theory-freshness-flavour-and-innovation-thats-bakingo
Lalith h
13 Oct 2024
5 min read
Save for later

The Big Brand Theory | Freshness, Flavour and Innovation. That’s Bakingo!

Lalith h
13 Oct 2024
5 min read
The inception of Bakingo is a tale of foresight and ambition shared by three engineering friends—Shrey Sehgal, Himanshu Chawla, and Suman Patra. As passionate foodies, they felt that their sweet cravings were never fully satisfied.In many cities, only two or three prominent bakery brands thrived. What stood out, however, was the absence of a unified, national bakery brand — a gap that seemed impossible to ignore. This realization led Shrey, Himanshu, and Suman onto an exciting new venture!Founders of Bakingo: (From Left) Suman Patra, Himanshu Chawla, Shrey SehgalFrom simple concepts to sweet creationsThe roots of Bakingo trace back to the trio’s experience in running another venture, FlowerAura, a bootstrapped gifting business which they launched in 2011. What began in a modest basement steadily grew into a flourishing company. The valuable insights into logistics and operations that they got from those days, would later become pivotal to the creation and growth of Bakingo.As first-generation entrepreneurs, the founders understood the significant risk they were taking by starting their own business. Suman says, “We met in college, stayed connected, and eventually decided to take a leap of faith and launch our first venture together.”In 2014-15, while running FlowerAura, the founders noticed that ordering a cake was inconvenient—customers relied on local bakeries due to the absence of online delivery options. Moreover, the taste and quality of cakes varied widely from city to city, making it difficult for customers to find consistent, high-quality cakes across different parts of the country.This is how they came up with the idea of launching Bakingo in 2016. The vision was simple, yet ambitious – deliver freshly baked, high-quality cakes and desserts right to customers’ doorsteps.Since they had previously relied on vendors who were primarily spread across different states, they wished to monitor operations more closely when they established Bakingo. Recognizing the value of quality control, they set up dark stores which were entirely run by the brand.Suman says “Our learnings from running FlowerAura helped us leverage our technological acumen to streamline operations, minimize wastage, and ensure customer-centric values were at the forefront of our business model”.By actively seeking and valuing customer feedback, Bakingo continually refines its products and services. This ensures that each interaction with the brand is memorable and delightful.The brand also focuses on creating customized cakes for specific themes and occasions, such as birthdays, anniversaries, and festivals, ensuring that customers can find the perfect cake for every celebration.Bakingo sets itself apart by using technology extensively to ensure that every process, from baking to delivery, is meticulously monitored and optimized. This results in minimal wastage and maximized freshness, guaranteeing that customers receive the best products possible.
Read more
  • 0
  • 0
  • 506

article-image-what-is-metas-llama-31-405b-how-it-works-use-cases-more
Sami Salkosuo
08 Aug 2024
6 min read
Save for later

What Is Meta's Llama 3.1 405B? How It Works, Use Cases & More

Sami Salkosuo
08 Aug 2024
6 min read
Having 405 billion parameter puts it in contention for a high position on the LMSys Chatbot Arena Leaderboard, a measure of performance scored from blind user votes.In recent months, the top spot has alternated between versions of OpenAI GPT-4, Anthropic Claude 3, and Google Gemini. Currently, GPT-4o holds the crown, but the smaller Claude 3.5 Sonnet takes the second spot, and the impending Claude 3.5 Opus is likely to take the first position if it can be released before OpenAI updates GPT-4o.That means competition at the high end is tough, and it will be interesting to see how Llama 3.1 405B stacks up to these competitors. While we wait for Llama 3.1 405B to appear on the leaderboard, some benchmarks are provided later in the article.Multi-lingual capabilitiesThe main update from Llama 3 to Llama 3.1 is better non-English support. The training data for Llama 3 was 95% English, so it performed poorly in other languages. The 3.1 update provides support for German, French, Italian, Portuguese, Hindi, Spanish, and Thai.Longer contextLlama 3 models had a context window—the amount of text that can be reasoned about at once—of 8k tokens (around 6k words). Llama 3.1 brings this up to a more modern 128k, making it competitive with other state-of-the-art LLMs.This fixes an important weakness for the Llama family. For enterprise use cases like summarizing long documents, generating code that involves context from a large codebase, or extended support chatbot conversations, a long context window that can store hundreds of pages of text is essential.Open model license agreementThe Llama 3.1 models are available under Meta's custom Open Model License Agreement. This permissive license grants researchers, developers, and businesses the freedom to use the model for both research and commercial applications.In a significant update, Meta has also expanded the license to allow developers to utilize the outputs from Llama models, including the 405B model, to enhance other models.In essence, this means that anyone can utilize the model's capabilities to advance their work, create new applications, and explore the possibilities of AI, as long as they adhere to the terms outlined in the agreement.How Llama 3.1 405B Works?This section explains the technical details of how Llama 3.1 405B works, including its architecture, training process, data preparation, computational requirements, and optimization techniques.Transformer architecture with tweaks Llama 3.1 405B is built upon a standard decoder-only Transformer architecture, a design common to many successful large language models.While the core structure remains consistent, Meta has introduced minor adaptations to enhance the model's stability and performance during training. Notably, the Mixture-of-Experts (MoE) architecture is intentionally excluded, prioritizing stability and scalability in the training process.Source: Meta AIThe diagram illustrates how Llama 3.1 405B processes language. It starts with the input text being divided into smaller units called tokens and then converted into numerical representations called token embeddings.These embeddings are then processed through multiple layers of self-attention, where the model analyzes the relationships between different tokens to understand their significance and context within the input.The information gathered from the self-attention layers is then passed through a feedforward network, which further processes and combines the information to derive meaning. This process of self-attention and feedforward processing is repeated multiple times to deepen the model's understanding.Finally, the model uses this information to generate a response token by token, building upon previous outputs to create a coherent and relevant text. This iterative process, known as autoregressive decoding, enables the model to produce a fluent and contextually appropriate response to the input prompt.Multi-phase training processDeveloping Llama 3.1 405B involved a multi-phase training process. Initially, the model underwent pre-training on a vast and diverse collection of datasets encompassing trillions of tokens. This exposure to massive amounts of text allows the model to learn grammar, facts, and reasoning abilities from the patterns and structures it encounters.Following pre-training, the model undergoes iterative rounds of supervised fine-tuning (SFT) and direct preference optimization (DPO). SFT involves training on specific tasks and datasets with human feedback, guiding the model to produce desired outputs.DPO, on the other hand, focuses on refining the model's responses based on preferences gathered from human evaluators. This iterative process progressively enhances the model's ability to follow instructions, improve the quality of its responses, and ensure safety.Data quality and quantityMeta claims to have strongly emphasized the quality and quantity of training data. For Llama 3.1 405B, this involved a rigorous data preparation process, including extensive filtering and cleaning to enhance the overall quality of the datasets.Interestingly, the 405B model itself is used to generate synthetic data, which is then incorporated into the training process to further refine the model's capabilities.Scaling up computationallyTraining a model as large and complex as Llama 3.1 405B requires a tremendous amount of computing power. To put it in perspective, Meta used over 16,000 of NVIDIA's most powerful GPUs, the H100, to train this model efficiently.They also made significant improvements to their entire training infrastructure to ensure it could handle the immense scale of the project, allowing the model to learn and improve effectively.Quantization for inferenceTo make Llama 3.1 405B more usable in real-world applications, Meta applied a technique called quantization, which involves converting the model's weights from 16-bit precision (BF16) to 8-bit precision (FP8). This is like switching from a high-resolution image to a slightly lower resolution: it preserves the essential details while reducing the file size.Similarly, quantization simplifies the model's internal calculations, making it run much faster and more efficiently on a single server. This optimization makes it easier and more cost-effective for others to utilize the model's capabilities.Llama 3.1 405B Use CasesLlama 3.1 405B offers various potential applications thanks to its open-source nature and large capabilities.Synthetic data generationThe model's ability to generate text that closely resembles human language can be used to create large amounts of synthetic data.This synthetic data can be valuable for training other language models, enhancing data augmentation techniques (making existing data more diverse), and developing realistic simulations for various applications.Model distillationThe knowledge embedded within the 405B model can be transferred to smaller, more efficient models through a process called distillation.Think of model distillation as teaching a student (a smaller AI model) the knowledge of an expert (the larger Llama 3.1 405B model). This process allows the smaller model to learn and perform tasks without needing the same level of complexity or computational resources as the larger model.This makes it possible to run advanced AI capabilities on devices like smartphones or laptops, which have limited power compared to the powerful servers used to train the original model.A recent example of model distillation is OpenAI’s GPT-4o mini, which is a distilled version of GPT-4o.Research and experimentationLlama 3.1 405B serves as a valuable research tool, enabling scientists and developers to explore new frontiers in natural language processing and artificial intelligence.Its open nature encourages experimentation and collaboration, accelerating the pace of discovery.Industry-specific solutionsBy adapting the model to data specific to particular industries, such as healthcare, finance, or education, it's possible to create custom AI solutions that address the unique challenges and requirements of those domains.
Read more
  • 3
  • 12
  • 1060
Banner background image
Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at €14.99/month. Cancel anytime
article-image-test
test
13 Feb 2024
1 min read
Save for later

test

test
13 Feb 2024
1 min read
test egni4 nrgijbn 4irgbwr hubvuhiw brhuw4b higb ewruhgb hurwbg hiuwbr3hig brwhigbh uwebrgu ybwr3iygbihew rbghiweinhniuiubnibni Test
Read more
  • 0
  • 0
  • 781

article-image-test-title-54567
661
23 May 2023
1 min read
Save for later

Test title 54567

661
23 May 2023
1 min read
This is text and something. aasasasLorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Read more
  • 0
  • 0
  • 663

article-image-test-blog-1234
661
23 May 2023
7 min read
Save for later

Test Blog 1234

661
23 May 2023
7 min read
Lorem Ipsum"Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...""There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..."What is Lorem Ipsum?Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Why do we use it?It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). Where does it come from?Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.Where can I get some?There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.  paragraphs words bytes lists Start with 'Loremipsum dolor sit amet...'   Translations: Can you help translate this site into a foreign language ? Please email us with details if you can help.There is a set of mock banners available here in three colours and in a range of standard banner sizes:Donate: If you use this site regularly and would like to help keep the site on the Internet, please consider donating a small sum to help pay for the hosting and bandwidth bill. There is no minimum donation, any sum is appreciated - click here to donate using PayPal. Thank you for your support.Donate Bitcoin: 16UQLq1HZ3CNwhvgrarV6pMoA2CDjb4tyFNodeJS Python Interface GTK Lipsum Rails .NET GroovyThe standard Lorem Ipsum passage, used since the 1500s"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"1914 translation by H. Rackham"But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"Section 1.10.33 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC"At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat."1914 translation by H. Rackham"On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains."[email protected] Policy
Read more
  • 0
  • 2
  • 474
article-image-test-title
859
19 May 2023
7 min read
Save for later

Test Title

859
19 May 2023
7 min read
This article provides a quick guide to using the OpenAI API to jump-start ChatGPT. The guide includes instructions on how to use a microphone to speak to ChatGPT and how to create a ChatGPT request with variables. Additionally, the article explains how to use Google gTTS, a text-to-speech tool, to listen to ChatGPT's response. By following these steps, you can have a more interactive experience with ChatGPT and make use of its advanced natural language processing capabilities. We’re using the GPT-3.5-Turbo architecture in this example. We are also running the examples within Google Colab, but they should be applicable to other environments.In this article, we’ll cover: Installing OpenAI, your API key, and Google gTTS for Text-to-SpeechGenerating content with ChatGPTSpeech-to-text ChatGPT's responseTranscribing with WhisperTo understand GPT-3 Transformers in detail, read Transformers for NLP, 2nd Edition 1. Installing OpenAI, gTTS, and your API Key There are a few libraries that we’ll need to install into Colab for this project. We’ll install them as required, starting with OpenAI. Installing and Importing OpenAI To start using OpenAI's APIs and tools, we'll need to install the OpenAI Python package and import it into your project. To do this, you can use pip, a package manager for Python. First, make sure you have pip installed on your system.  !pip install --upgrade pipNext, run the following script in your notebook to install the OpenAI package. It should come pre-installed in Colab: #Importing openai try: import openai except: !pip install openai import openai Installing gTTS Next, install Google gTTS a Python library that provides an easy-to-use interface for text-to-speech synthesis using the Google Text-to-Speech API: #Importing gTTS try: from gtts import gTTS except: !pip install gTTS from gtts import gTTS API Key Finally, import your API key. Rather than enter your key directly into your notebook, I recommend keeping it in a local file and importing it from your script. You will need to provide the correct path and filename in the code below. from google.colab import drive drive.mount('/content/drive') f = open("drive/MyDrive/files/api_key.txt", "r") API_KEY=f.readline() f.close() #The OpenAI Key import os os.environ['OPENAI_API_KEY'] =API_KEY openai.api_key = os.getenv("OPENAI_API_KEY") 2. Generating Content Let’s look at how to pass prompts into the OpenAI API to generate responses. Speech to text When it comes to speech recognition, Windows provides built-in speech-to-text functionality. However, third-party speech-to-text modules are also available, offering features such as multiple language support, speaker identification, and audio transcription. For simple speech-to-text, this notebook uses the built-in functionality in Windows. Press Windows key + H to bring up the Windows speech interface. You can read the documentation for more information.Note: For this notebook, press Enter when you have finished asking for a request in Colab. You could also adapt the function in your application with a timed input function that automatically sends a request after a certain amount of time has elapsed. Preparing the Prompt Note: you can create variables for each part of the OpenAI messages object. This object contains all the information needed to generate a response from ChatGPT, including the text prompt, the model ID, and the API key. By creating variables for each part of the object, you can make it easier to generate requests and responses programmatically. For example, you could create a prompt variable that contains the text prompt for generating a response. You could also create variables for the model ID and API key, making it easier to switch between different OpenAI models or accounts as needed.For more on implementing each part of the messages object, take a look at: Prompt_Engineering_as_an_alternative_to_fine_tuning.ipynb.Here’s the code for accepting the prompt and passing the request to OpenAI: #Speech to text. Use OS speech-to-text app. For example, Windows: press Windows Key + H  def prepare_message(): #enter the request with a microphone or type it if you wish # example: "Where is Tahiti located?" print("Enter a request and press ENTER:") uinput = input("") #preparing the prompt for OpenAI  role="user" #prompt="Where is Tahiti located?" #maintenance or if you do not want to use a microphone line = {"role": role, "content": uinput} #creating the message assert1={"role": "system", "content": "You are a helpful assistant."} assert2={"role": "assistant", "content": "Geography is an important topic if you are going on a once in a lifetime trip."} assert3=line iprompt = [] iprompt.append(assert1) iprompt.append(assert2) iprompt.append(assert3) return iprompt #run the cell to start/continue a dialog iprompt=prepare_message() #preparing the messages for ChatGPT response=openai.ChatCompletion.create(model="gpt-3.5-turbo",messages=iprompt) #ChatGPT dialog text=response["choices"][0]["message"]["content"] #response in JSON print("ChatGPT response:",text) Here's a sample of the output: Enter a request and press ENTER: Where is Tahiti located ChatGPT response: Tahiti is located in the South Pacific Ocean, specifically in French Polynesia. It is part of a group of islands called the Society Islands and is located approximately 4,000 kilometers (2,500 miles) south of Hawaii and 7,850 kilometers (4,880 miles) east of Australia. 3. Speech-to-text the response GTTS and IPython Once you've generated a response from ChatGPT using the OpenAI package, the next step is to convert the text into speech using gTTS (Google Text-to-Speech) and play it back using  IPython audio. from gtts import gTTS from IPython.display import Audio tts = gTTS(text) tts.save('1.wav') sound_file = '1.wav' Audio(sound_file, autoplay=True)  4. Transcribing with Whisper If your project requires the transcription of audio files, you can use OpenAI’s Whisper.First, we’ll install the ffmpeg audio processing library. ffmpeg is a popular open-source software suite for handling multimedia data, including audio and video files: !pip install ffmpegNext, we’ll install Whisper: !pip install git+https://github.com/openai/whisper.git With that done, we can use a simple command to transcribe the WAV file and store it as a JSON file with the same name: !whisper 1.wavYou’ll see Whisper transcribe the file in chunks: [00:00.000 --> 00:06.360] Tahiti is located in the South Pacific Ocean, specifically in the archipelago of society [00:06.360 --> 00:09.800] islands and is part of French Polynesia. [00:09.800 --> 00:22.360] It is approximately 4,000 miles, 6,400 km, south of Hawaii and 5,700 miles, 9,200 km, [00:22.360 --> 00:24.640] west of Santiago, Chile.Once that’s done, we can read the JSON file and display the text object: import json with open('1.json') as f: data = json.load(f) text = data['text'] print(text)This gives the following output: Tahiti is located in the South Pacific Ocean, specifically in the archipelago of society islands and is part of French Polynesia. It is approximately 4,000 miles, 6,400 km, south of Hawaii and 5,700 miles, 9,200 km, west of Santiago, Chile. By using Whisper in combination with ChatGPT and gTTS, you can create a fully featured AI-powered application that enables users to interact with your system using natural language inputs and receive audio responses. This might be useful for applications that involve transcribing meetings, conferences, or other audio files. About the Author Denis Rothman graduated from Sorbonne University and Paris-Diderot University, designing one of the very first word2matrix patented embedding and patented AI conversational agents. He began his career authoring one of the first AI cognitive natural language processing (NLP) chatbots applied as an automated language teacher for Moet et Chandon and other companies. He authored an AI resource optimizer for IBM and apparel producers. He then authored an advanced planning and scheduling (APS) solution used worldwide.You can follow Denis on LinkedIn:  https://www.linkedin.com/in/denis-rothman-0b034043/Copyright 2023 Denis Rothman, MIT License
Read more
  • 0
  • 0
  • 308

article-image-python-object-oriented-programming-test
430
18 May 2023
1 min read
Save for later

Python Object-Oriented Programming Test

430
18 May 2023
1 min read
How to use the OpenAI Python library to interact with the ChatGPT API 
Read more
  • 0
  • 0
  • 169

article-image-python-object-oriented-programming
430
18 May 2023
1 min read
Save for later

Python Object-Oriented Programming

430
18 May 2023
1 min read
How to use the OpenAI Python library to interact with the ChatGPT API 
Read more
  • 0
  • 0
  • 306