File size: 1,354 Bytes
8c810e3 bc8c903 8c810e3 bc8c903 8c810e3 bc8c903 |
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 |
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
# Load the trained model and tokenizer
model_path = "path/to/save/model"
tokenizer_path = "path/to/save/tokenizer"
model = AutoModelForSequenceClassification.from_pretrained(model_path)
tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)
model.eval() # Set model to evaluation mode
def predict_paraphrase(sentence1, sentence2):
# Tokenize the input sentences
inputs = tokenizer(sentence1, sentence2, return_tensors="pt", padding=True, truncation=True)
with torch.no_grad():
outputs = model(**inputs)
# Get probabilities
probs = torch.nn.functional.softmax(outputs.logits, dim=-1).tolist()[0]
# Assuming the first class (index 0) is 'not paraphrase' and the second class (index 1) is 'paraphrase'
return {"Not Paraphrase": probs[0], "Paraphrase": probs[1]}
# Create Gradio interface
iface = gr.Interface(
fn=predict_paraphrase,
inputs=[gr.inputs.Textbox(lines=2, placeholder="Enter Sentence 1 Here..."),
gr.inputs.Textbox(lines=2, placeholder="Enter Sentence 2 Here...")],
outputs=gr.outputs.Label(num_top_classes=2),
title="Paraphrase Identification",
description="This model predicts whether two sentences are paraphrases of each other."
)
iface.launch()
|