Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
|
|
|
| 3 |
|
| 4 |
-
# Load tokenizer and model (small T5 variant, CPU only)
|
| 5 |
model_name = "t5-small"
|
| 6 |
|
| 7 |
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 8 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def generate_text(input_text):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 17 |
return result
|
| 18 |
|
| 19 |
-
# Build Gradio interface
|
| 20 |
demo = gr.Interface(
|
| 21 |
fn=generate_text,
|
| 22 |
inputs=gr.Textbox(lines=5, label="Input Text"),
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 3 |
+
import torch
|
| 4 |
|
|
|
|
| 5 |
model_name = "t5-small"
|
| 6 |
|
| 7 |
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 8 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 9 |
+
model.eval() # set model to evaluation mode
|
| 10 |
+
|
| 11 |
+
device = torch.device("cpu") # explicitly set device to CPU
|
| 12 |
+
model.to(device)
|
| 13 |
|
| 14 |
def generate_text(input_text):
|
| 15 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device)
|
| 16 |
+
outputs = model.generate(
|
| 17 |
+
input_ids,
|
| 18 |
+
max_length=100,
|
| 19 |
+
num_beams=5,
|
| 20 |
+
early_stopping=True
|
| 21 |
+
)
|
| 22 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 23 |
return result
|
| 24 |
|
|
|
|
| 25 |
demo = gr.Interface(
|
| 26 |
fn=generate_text,
|
| 27 |
inputs=gr.Textbox(lines=5, label="Input Text"),
|