How to create a C# AI application that takes a user description and returns summerized text

JERRY Warra 121 Reputation points
2024-11-14T16:41:52.1366667+00:00

Hello,

I'm not sure if the scenario that I'm working on researching is even possible, but I wanted to check.

Is there a way to create a web tool that will take all the text that a user types in and based on what is typed it returns a summarized version?

Say I'm asking the user what their job description is, I want to take what they enter and filter it down to a couple of words.

User types in "I work in a butcher shop cutting meat"

AI returns "Butcher" as the job.

Thank you

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,054 questions
Azure AI Language
Azure AI Language
An Azure service that provides natural language capabilities including sentiment analysis, entity extraction, and automated question answering.
426 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sina Salam 12,816 Reputation points
    2024-11-14T22:39:22.56+00:00

    Hello JERRY Warra,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that you would like to use C# to create an AI application that takes a user description and returns summarized text.

    Yes, it’s definitely possible to create a web tool that can summarize text input from users! This kind of functionality can be achieved using natural language processing (NLP) techniques. Your approach should be similar as building such a tool:

    1. Text Input: Capture the text input from the user through a web form.
    2. Preprocessing: Clean and preprocess the text to remove any unnecessary characters or formatting.
    3. NLP Model: Use an NLP model to analyze the text. For your specific example, you could use a model trained to recognize job descriptions and map them to job titles. Pre-trained models like BERT or GPT-4 can be fine-tuned for this purpose.
    4. Summarization: Implement a summarization algorithm that condenses the input text into a concise form. This could involve keyword extraction, named entity recognition, or other techniques.
    5. Output: Display the summarized text back to the user.

    Using C# can be done by integrating an NLP model, such as one from the Hugging Face Transformers library, with your C# application and this is just a high-level overview of how you can achieve this below, and you can search for more information, also utilize training and documents available in the additional resources by the right side of this page:

    • Set Up Your Environment by install Python and necessary libraries (like transformers).
    • Set up a C# project by creating a Python Script for Summarization and write a Python script that uses a pre-trained model to summarize text.
    • Integrate Python with C# using a library like Python.NET or create a REST API with Flask to call the Python script from your C# application.

    With all said, this how you can approach it:

    • First, create a Python script (summarizer.py) that uses the Hugging Face Transformers library:
        from transformers import pipeline
        # Load a pre-trained summarization pipeline
        summarizer = pipeline("summarization")
        def summarize_job_description(description):
            # Summarize the job description
            summary = summarizer(description, max_length=10, min_length=5, do_sample=False)
            return summary[0]['summary_text']
        if __name__ == "__main__":
            import sys
            description = sys.argv[1]
            print(summarize_job_description(description))
      
    • If you prefer to use a REST API (Optional), you can create a simple Flask app:
        from flask import Flask, request, jsonify
        from transformers import pipeline
        app = Flask(__name__)
        summarizer = pipeline("summarization")
        @app.route('/summarize', methods=['POST'])
        def summarize():
            description = request.json['description']
            summary = summarizer(description, max_length=10, min_length=5, do_sample=False)
            return jsonify({'summary': summary[0]['summary_text']})
        if __name__ == "__main__":
            app.run(debug=True)
      
    • Integrate above in your C# application, you can call the Python script or the REST API. Here’s an example using Python.NET to call the Python script directly:
        # You can install Python.NET via NuGet Package Manager in Visual Studio.
        # C# Code to Call Python Script:
        using System;
        using Python.Runtime;
        class Program
        {
            static void Main(string[] args)
            {
                string description = "I work in a butcher shop cutting meat";
                string summary = SummarizeJobDescription(description);
                Console.WriteLine(summary);
            }
            static string SummarizeJobDescription(string description)
            {
                using (Py.GIL())
                {
                    dynamic summarizer = Py.Import("summarizer");
                    dynamic result = summarizer.summarize_job_description(description);
                    return result.ToString();
                }
            }
        }
      
    • Alternatively, if you are using the Flask API, you can use HttpClient in C# to make a POST request:
        using System;
        using System.Net.Http;
        using System.Text;
        using System.Threading.Tasks;
        using Newtonsoft.Json;
        class Program
        {
            static async Task Main(string[] args)
            {
                string description = "I work in a butcher shop cutting meat";
                string summary = await SummarizeJobDescription(description);
                Console.WriteLine(summary);
            }
            static async Task<string> SummarizeJobDescription(string description)
            {
                using (HttpClient client = new HttpClient())
                {
                    var content = new StringContent(JsonConvert.SerializeObject(new { description }), Encoding.UTF8, "application/json");
                    HttpResponseMessage response = await client.PostAsync("http://localhost:5000/summarize", content);
                    response.EnsureSuccessStatusCode();
                    string responseBody = await response.Content.ReadAsStringAsync();
                    dynamic result = JsonConvert.DeserializeObject(responseBody);
                    return result.summary;
                }
            }
        }
      

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.


    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.