by Lazy Sloth
Fast API endpoint for Text Classification using GPT 4
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel, Field
from typing import List
from abilities import llm_prompt
from concurrent.futures import ThreadPoolExecutor, TimeoutError
import json
app = FastAPI()
class Item(BaseModel):
text: str = Field(..., description="The text item to be classified")
class Category(BaseModel):
name: str = Field(..., description="The category name")
def classify_item(item, categories):
try:
prompt = f"Classify the following item: [{item.text}], into one of the following categories: [{', '.join([category.name for category in categories])}]. Respond with only the name of the category, leave empty if nothing matches."
response = llm_prompt(prompt, model="gpt-4", temperature=0.1)
print(response)
try:
response = json.loads(response)
Fast API endpoint for Text Classification using GPT 4
Created: | Last Updated:
Introduction to the FastAPI Text Classification Template
Welcome to the FastAPI Text Classification Template using GPT-4! This template is designed to help you quickly set up an API that can classify text items into categories using the power of GPT-4. Whether you're building a content categorization tool, a customer support automation system, or any other application that requires text classification, this template will get you started without the hassle of environment setup or deployment concerns.
Clicking Start with this Template
To begin using this template, simply click on the "Start with this Template" button. This will set up the template in your Lazy builder interface, pre-populating the code and allowing you to customize it to your needs.
Initial Setup
There are no environment secrets to set up for this template, as all necessary modules and functionalities are built-in within the Lazy platform. This means you can proceed without any additional configuration.
Test: Pressing the Test Button
Once you have started with the template, you can test the functionality by pressing the "Test" button. This will deploy your application and launch the Lazy CLI. The CLI will prompt you for any required user input, if necessary.
Entering Input
For this template, user input through the CLI is not required as the API endpoints are designed to receive input through HTTP requests. Therefore, you can skip this section and move on to using the app.
Using the App
After testing, Lazy will provide you with a dedicated server link to use the API. Additionally, since this template uses FastAPI, you will also receive a link to the automatically generated documentation for your API endpoints. This documentation will guide you on how to interact with the API, detailing the request formats and available endpoints.
Integrating the App
If you need to integrate this API into an external service or frontend, you can use the server link provided by Lazy. Here's how you can make a sample request to the "/classify_single" endpoint:
`import requests
Replace 'your_server_link' with the actual server link provided by Lazy
url = 'your_server_link/classify_single'
Sample data to classify
data = {
"items": [{"text": "Sample text to classify"}],
"categories": [{"name": "Category1"}, {"name": "Category2"}]
}
Make a POST request to the API
response = requests.post(url, json=data)
Print the response from the API
print(response.json())` And here's an example of what the response might look like:
[
{
"item": "Sample text to classify",
"category": "Category1"
}
]
Remember to replace 'your_server_link' with the actual link provided after deployment. Use the provided server link to integrate the API into your application or service as needed. If your integration requires specific scopes or code placement, ensure you follow the guidelines of the external tool you are integrating with.
By following these steps, you should be able to successfully set up and integrate the FastAPI Text Classification Template into your project. Happy building!
Technologies
![Fast API](https://gitlab.com/uploads/-/system/project/avatar/37467835/fastapi.png)