File size: 1,671 Bytes
0caec9c
e2701ca
 
 
 
5e41748
 
e2701ca
5e41748
 
f95bb71
5e41748
e2701ca
5e41748
e2701ca
5e41748
e2701ca
 
 
 
 
 
5e41748
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
import gradio as gr
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import T5ForConditionalGeneration, T5Tokenizer
import torch
import threading
import uvicorn

# Load model & tokenizer
model_path = "./t5-summarizer"
tokenizer = T5Tokenizer.from_pretrained(model_path, legacy=False)
model = T5ForConditionalGeneration.from_pretrained(model_path)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

# FastAPI setup
app = FastAPI()
class TextInput(BaseModel):
    text: str

@app.post("/summarize/")
def summarize_text(input: TextInput):
    inputs = tokenizer("summarize: " + input.text.replace("\n"," "),
                       return_tensors="pt", max_length=512, truncation=True)
    outputs = model.generate(inputs.input_ids.to(device),
                             max_length=150, min_length=30,
                             length_penalty=2.0, num_beams=4, early_stopping=True)
    return {"summary": tokenizer.decode(outputs[0], skip_special_tokens=True)}

def run_fastapi():
    uvicorn.run(app, host="0.0.0.0", port=8000)

# Gradio UI
iface = gr.Interface(
    fn=lambda text: summarize_text(TextInput(text=text))["summary"],
    inputs=gr.Textbox(lines=10, placeholder="Paste text here..."),
    outputs=gr.Textbox(label="Summary"),
    title="Text Summarizer",
    description="Fine-tuned T5 summarizer",
    flagging_mode="never",               # Disable flagging
    examples=[["Your example text here..."]]  # Pre-load examples
)

# Start FastAPI in background, then launch Gradio
threading.Thread(target=run_fastapi, daemon=True).start()
iface.launch(server_name="0.0.0.0", server_port=7860)