|
|
import gradio as gr |
|
|
from transformers import T5Tokenizer, T5ForConditionalGeneration |
|
|
import torch |
|
|
|
|
|
|
|
|
tokenizer = T5Tokenizer.from_pretrained("quynhthames/vietnamese-math-solver") |
|
|
model = T5ForConditionalGeneration.from_pretrained("quynhthames/vietnamese-math-solver") |
|
|
|
|
|
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
|
model.to(device) |
|
|
|
|
|
def solve_math_problem(problem): |
|
|
|
|
|
inputs = tokenizer(problem, return_tensors="pt", padding=True, truncation=True, max_length=64).to(device) |
|
|
|
|
|
|
|
|
outputs = model.generate(**inputs, max_length=128) |
|
|
|
|
|
|
|
|
solution = tokenizer.batch_decode(outputs, skip_special_tokens=True) |
|
|
|
|
|
return solution |
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=solve_math_problem, |
|
|
inputs="text", |
|
|
outputs="text", |
|
|
title="Vietnamese Math Problem Solver", |
|
|
description="Enter a math problem and get the solution." |
|
|
) |
|
|
|
|
|
iface.launch() |