Shankarm08 commited on
Commit
7340eaf
·
verified ·
1 Parent(s): b01e38b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import BertTokenizer, BertModel
4
+ from fastapi import FastAPI, HTTPException
5
+ from pydantic import BaseModel
6
+
7
+ app = FastAPI()
8
+
9
+ class TextClassificationRequest(BaseModel):
10
+ text: str
11
+
12
+ @app.post("/classify")
13
+ async def classify_text(request: TextClassificationRequest):
14
+ # Load the pre-trained BERT model and tokenizer
15
+ model_name = "bert-base-uncased"
16
+ tokenizer = BertTokenizer.from_pretrained(model_name)
17
+ model = BertModel.from_pretrained(model_name)
18
+
19
+ # Preprocess the input text
20
+ inputs = tokenizer.encode_plus(
21
+ request.text,
22
+ add_special_tokens=True,
23
+ max_length=512,
24
+ return_attention_mask=True,
25
+ return_tensors='pt'
26
+ )
27
+
28
+ # Create a dictionary to store the output
29
+ output = {}
30
+
31
+ # Use the pre-trained BERT model to extract features from the input text
32
+ outputs = model(**inputs)
33
+
34
+ # Extract the features
35
+ features = outputs.last_hidden_state[:, 0, :]
36
+
37
+ # Store the output
38
+ output["features"] = features.tolist()
39
+
40
+ return output
41
+
42
+ # Create a Gradio interface
43
+ interface = gr.Interface(
44
+ fn=classify_text,
45
+ inputs="pdf",
46
+ outputs="text",
47
+ title="PDF Text Classification",
48
+ description="Upload a PDF file to classify its text"
49
+ )
50
+
51
+ # Launch the interface
52
+ interface.launch()