Giter VIP home page Giter VIP logo

azure-functions-with-python's Introduction

Azure Functions with Python

If you haven't tried Azure Functions with Python, it is a great way to publish your REST services on a Serverless architecture and consume immediately. Here's a sample I created for text analytics to normalize text using Python and published as service.

After installing Azure Function tools on VSCode, it's really easy to test your Python function on your local and publish to Azure as an Azure Function App.

  1. For the demo I used Natural Language Toolkit - NLTK for removing stop words, also prepared a custom list to remove some words which I would like to avoid. After that I tokenized the words and extract most frequent words from paragraphs using FreqDist from nltk.probability

    All third party python libraries can be defined in requirement.txt file to include into your environment

    azure-functions==1.0.0a5
    azure-functions-worker==1.0.0a6
    grpcio==1.14.2
    grpcio-tools==1.14.2
    protobuf==3.6.1
    six==1.12.0
    nltk==3.4

    After defining those packages you can import in your python code.

    import azure.functions as func
    from string import punctuation
    import nltk
    nltk.download("punkt")
    nltk.download("stopwords")
    from nltk.corpus import stopwords
    from nltk.probability import FreqDist

    Here is the main function of our Python code.

    def main(req: func.HttpRequest) -> func.HttpResponse:
        logging.info('Python HTTP trigger function processed a request.')
    
        content = req.params.get('content')
        if not content:
            try:
                req_body = req.get_json()
            except ValueError:
                pass
            else:
                content = req_body.get('content')
        if content:
            # remove numeric digits
            text = ''.join(c for c in content if not c.isdigit())
            # remove punctuation and make lower case
            text = ''.join(c for c in text if c not in punctuation).lower()
            text = text.replace('โ€“', ' ')
            
            # remove stopwords from the text
            text = ' '.join([word for word in text.split() if word not in (stopwords.words('english'))])
    
            # remove custom stopword from a list
            with open("customstopwords.txt", "r") as f:
                customstopwords = f.read().splitlines()
            text = ' '.join(c for c in text.split(' ') if c not in customstopwords)
            
            # tokenize the words and extract most frequent words
            allWords = nltk.tokenize.word_tokenize(text)
            fdist = FreqDist(allWords)
            topWords = [word[0] for word in fdist.most_common(30)]
            
            #return the result
            return func.HttpResponse(f"{topWords}")
        else:
            return func.HttpResponse(
                "Please pass a content on the query string or in the request body",
                status_code=400
            )
  2. After you test your function select "Deploy to Function App" to publish your local Azure funtion app to Azure Functions App.

  3. After selecting your subscription and Azure Function App, your application will be deployed. All local environments and necessary libraries are zipped into PythonFunction.zip file.

  4. You'll notice that your zip packages is uploaded to your blob storage of your Azure Function.

  5. Then you can test your service using your Azure function endpoint.

Materials

Thanks!

azure-functions-with-python's People

Contributors

ikivanc avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

azure-functions-with-python's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.