Jesuscarr commited on
Commit
d24de68
1 Parent(s): d86acb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -32
app.py CHANGED
@@ -1,48 +1,50 @@
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
 
 
1
  import gradio as gr
2
+ from transformers import MarianMTModel, MarianTokenizer, GPT2LMHeadModel, GPT2Tokenizer
3
 
4
  def translate(text, target_language):
5
+ # ... (keep the existing code for translation here)
6
+
7
+ def generate_text(prompt):
8
+ model_name = 'gpt2'
9
+ tokenizer = GPT2Tokenizer.from_pretrained(model_name)
10
+ model = GPT2LMHeadModel.from_pretrained(model_name)
11
+ inputs = tokenizer.encode(prompt, return_tensors='pt')
12
+ outputs = model.generate(inputs, max_length=100, num_return_sequences=1)
13
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
14
+ return generated_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  language_options = [
17
+ # ... (keep the existing language options here)
 
 
18
  ]
19
 
20
+ iface_translation = gr.Interface(
21
  fn=translate,
22
+ # ... (keep the existing translation inputs and outputs here)
23
+ )
24
+
25
+ iface_generation = gr.Interface(
26
+ fn=generate_text,
27
+ inputs=gr.inputs.Textbox(lines=5, label="Enter a prompt for text generation:"),
28
+ outputs=gr.outputs.Textbox(label="Generated Text"),
29
+ )
30
+
31
+ # Combine the two interfaces into a single Gradio interface
32
+ iface_combined = gr.Interface(
33
+ [translate, generate_text],
34
  inputs=[
35
+ gr.inputs.Textbox(lines=5, label="Enter text to translate / generate:", default="Enter text to translate here."),
36
  gr.inputs.Dropdown(choices=language_options, label="Target Language"),
37
  ],
38
+ outputs=[
39
+ gr.outputs.Textbox(label="Translated Text / Generated Text"),
40
+ ],
41
+ title="Translation and Text Generation",
42
+ description="Choose a target language to translate English text or leave it as 'None' for text generation.",
43
+ examples=[["Translate this text to French.", "French (European)"]]
44
  )
45
 
46
+ iface_combined.launch()
47
+
48
 
49
 
50