vakodiya commited on
Commit
ca2c26d
1 Parent(s): 2a6fd75

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +36 -0
main.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ from transformers import GPT2Tokenizer, GPT2Model
4
+ from langchain.prompts import PromptTemplate
5
+
6
+ app = FastAPI()
7
+
8
+ tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
9
+ model = GPT2Model.from_pretrained('gpt2')
10
+
11
+
12
+ class TextRequest(BaseModel):
13
+ text: str
14
+
15
+
16
+ def preprocess_text(text: str):
17
+ return text.lower()
18
+
19
+
20
+ def classify_text(question: str):
21
+ prompt_template = PromptTemplate(template="Answer the following question and classify it: {question}", input_variables = ["question"], output_variables=["answer", "classification"])
22
+ # Model loading
23
+ format_prompt = prompt_template.format(question=question)
24
+ encoded_input = tokenizer(format_prompt, return_tensors='pt')
25
+ output = model(encoded_input)
26
+ # chain = LLMChain(llm=llm, prompt=prompt_template, verbose=True)
27
+ # response = chain({"question": question})
28
+ return output
29
+
30
+
31
+ @app.post("/classify")
32
+ async def classify_text_endpoint(request: TextRequest):
33
+ preprocessed_text = preprocess_text(request.text)
34
+ response = classify_text(preprocessed_text)
35
+ answer = response['text']
36
+ return {"answer": answer}