Spaces:
Runtime error
app.py
Browse filesimport 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()
@@ -1,49 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from transformers import MarianMTModel, MarianTokenizer
|
3 |
-
|
4 |
-
def translate(text, target_language):
|
5 |
-
language_codes = {
|
6 |
-
"Spanish": "es",
|
7 |
-
"French (European)": "fr",
|
8 |
-
"French (Canadian)": "fr",
|
9 |
-
"Italian": "it",
|
10 |
-
"Ukrainian": "uk",
|
11 |
-
"Portuguese (Brazilian)": "pt_BR",
|
12 |
-
"Portuguese (European)": "pt",
|
13 |
-
"Russian": "ru",
|
14 |
-
"Chinese": "zh",
|
15 |
-
"Dutch": "nl",
|
16 |
-
"German": "de",
|
17 |
-
"Arabic": "ar",
|
18 |
-
"Hebrew": "he",
|
19 |
-
"Greek": "el"
|
20 |
-
}
|
21 |
-
target_language_code = language_codes[target_language]
|
22 |
-
model_name = f'helsinki-nlp/opus-mt-en-{target_language_code}'
|
23 |
-
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
24 |
-
model = MarianMTModel.from_pretrained(model_name)
|
25 |
-
inputs = tokenizer(text, return_tensors="pt")
|
26 |
-
outputs = model.generate(**inputs)
|
27 |
-
translation = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
28 |
-
return translation
|
29 |
-
|
30 |
-
language_options = [
|
31 |
-
"Spanish", "French (European)", "French (Canadian)", "Italian", "Ukrainian",
|
32 |
-
"Portuguese (Brazilian)", "Portuguese (European)", "Russian", "Chinese",
|
33 |
-
"Dutch", "German", "Arabic", "Hebrew", "Greek"
|
34 |
-
]
|
35 |
-
|
36 |
-
iface = gr.Interface(
|
37 |
-
fn=translate,
|
38 |
-
inputs=[
|
39 |
-
gr.inputs.Textbox(lines=5, label="Enter text to translate:"),
|
40 |
-
gr.inputs.Dropdown(choices=language_options, label="Target Language"),
|
41 |
-
],
|
42 |
-
outputs=gr.outputs.Textbox(label="Translated Text"),
|
43 |
-
)
|
44 |
-
|
45 |
-
iface.launch()
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|