translation / app.py
jjuarez's picture
Update app.py
3f05bd9
raw
history blame
1.59 kB
import gradio as gr
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
# Loading translation model
tokenizer = AutoTokenizer.from_pretrained("ieuniversity/sciencebrief_translation")
model = AutoModelForSeq2SeqLM.from_pretrained("ieuniversity/sciencebrief_translation")
# Loading summarization model
summarization_tokenizer = AutoTokenizer.from_pretrained("ieuniversity/sciencebrief_summarization")
summarization_model = AutoModelForSeq2SeqLM.from_pretrained("ieuniversity/sciencebrief_summarization")
#translation function
def translate_text(text):
input_ids = tokenizer.encode(text, return_tensors="pt")
outputs = model.generate(input_ids)
decoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
return decoded_output
#summarization function
def summarize_text(text):
input_ids = summarization_tokenizer.encode(text, return_tensors="pt")
output_ids = summarization_model.generate(input_ids)
summary = summarization_tokenizer.decode(output_ids[0], skip_special_tokens=True)
return summary
# Building the gradio interface
input_text = gr.inputs.Textbox(label="Input Text")
summarization_output = gr.outputs.Textbox(label="Summarization Output")
translation_output = gr.outputs.Textbox(label="Translation Output")
gr.Interface(
fn=[summarize_text, translate],
inputs=input_text,
outputs=[summarization_output, translation_output],
layout="vertical",
title="Scientific Papers Text Summarization and Translation",
description="Enter some text and get a summary and translation in Spanish."
).launch()