Translate_It / app.py
Jesuscarr's picture
Update app.py
d24de68
raw
history blame
1.7 kB
import gradio as gr
from transformers import MarianMTModel, MarianTokenizer, GPT2LMHeadModel, GPT2Tokenizer
def translate(text, target_language):
# ... (keep the existing code for translation here)
def generate_text(prompt):
model_name = 'gpt2'
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
model = GPT2LMHeadModel.from_pretrained(model_name)
inputs = tokenizer.encode(prompt, return_tensors='pt')
outputs = model.generate(inputs, max_length=100, num_return_sequences=1)
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
return generated_text
language_options = [
# ... (keep the existing language options here)
]
iface_translation = gr.Interface(
fn=translate,
# ... (keep the existing translation inputs and outputs here)
)
iface_generation = gr.Interface(
fn=generate_text,
inputs=gr.inputs.Textbox(lines=5, label="Enter a prompt for text generation:"),
outputs=gr.outputs.Textbox(label="Generated Text"),
)
# Combine the two interfaces into a single Gradio interface
iface_combined = gr.Interface(
[translate, generate_text],
inputs=[
gr.inputs.Textbox(lines=5, label="Enter text to translate / generate:", default="Enter text to translate here."),
gr.inputs.Dropdown(choices=language_options, label="Target Language"),
],
outputs=[
gr.outputs.Textbox(label="Translated Text / Generated Text"),
],
title="Translation and Text Generation",
description="Choose a target language to translate English text or leave it as 'None' for text generation.",
examples=[["Translate this text to French.", "French (European)"]]
)
iface_combined.launch()