Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,15 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
# Load the tokenizer and model
|
5 |
-
tokenizer = T5Tokenizer.from_pretrained("alpeshsonar/lot-t5-small-filter")
|
6 |
model = T5ForConditionalGeneration.from_pretrained("alpeshsonar/lot-t5-small-filter")
|
7 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
8 |
model = model.to(device)
|
@@ -26,3 +33,25 @@ iface = gr.Interface(
|
|
26 |
|
27 |
# Launch the interface
|
28 |
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
3 |
+
import torch
|
4 |
+
from fastapi import FastAPI
|
5 |
+
from pydantic import BaseModel
|
6 |
+
|
7 |
+
|
8 |
+
# Initialize the FastAPI app
|
9 |
+
app = FastAPI()
|
10 |
|
11 |
# Load the tokenizer and model
|
12 |
+
tokenizer = T5Tokenizer.from_pretrained("alpeshsonar/lot-t5-small-filter", legacy=False)
|
13 |
model = T5ForConditionalGeneration.from_pretrained("alpeshsonar/lot-t5-small-filter")
|
14 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
15 |
model = model.to(device)
|
|
|
33 |
|
34 |
# Launch the interface
|
35 |
iface.launch()
|
36 |
+
|
37 |
+
|
38 |
+
# Define a request body model
|
39 |
+
class TextInput(BaseModel):
|
40 |
+
input_text: str
|
41 |
+
|
42 |
+
# Endpoint to generate text from the model
|
43 |
+
@app.post("/generate")
|
44 |
+
async def generate_text(input_data: TextInput):
|
45 |
+
input_text = input_data.input_text
|
46 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
|
47 |
+
|
48 |
+
# Generate the output
|
49 |
+
outputs = model.generate(inputs, max_new_tokens=1024)
|
50 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
51 |
+
|
52 |
+
return {"output": result}
|
53 |
+
|
54 |
+
# Health check endpoint
|
55 |
+
@app.get("/health")
|
56 |
+
async def health_check():
|
57 |
+
return {"status": "API is running"}
|