File size: 1,149 Bytes
ca2c26d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import GPT2Tokenizer, GPT2Model
from langchain.prompts import PromptTemplate

app = FastAPI()

tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = GPT2Model.from_pretrained('gpt2')


class TextRequest(BaseModel):
    text: str


def preprocess_text(text: str):
    return text.lower()


def classify_text(question: str):
    prompt_template = PromptTemplate(template="Answer the following question and classify it: {question}", input_variables = ["question"], output_variables=["answer", "classification"])
    # Model loading
    format_prompt = prompt_template.format(question=question)
    encoded_input = tokenizer(format_prompt, return_tensors='pt')
    output = model(encoded_input)
    # chain = LLMChain(llm=llm, prompt=prompt_template, verbose=True)
    # response = chain({"question": question})
    return output


@app.post("/classify")
async def classify_text_endpoint(request: TextRequest):
    preprocessed_text = preprocess_text(request.text)
    response = classify_text(preprocessed_text)
    answer = response['text']
    return {"answer": answer}