File size: 1,699 Bytes
1398925
d24de68
1398925
6908aad
d24de68
 
 
 
 
 
 
 
 
 
6908aad
 
d24de68
07a4801
13081ab
d24de68
6908aad
d24de68
 
 
 
 
 
 
 
 
 
 
 
6908aad
d24de68
6908aad
 
d24de68
 
 
 
 
 
6908aad
af1aba9
d24de68
 
59e6f0a
6908aad
3bc002c
d86acb9
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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()