Files changed (1) hide show
  1. app.py +33 -5
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from transformers import MarianMTModel, MarianTokenizer
3
 
4
  def translate(text, target_language):
5
  language_codes = {
@@ -27,6 +27,19 @@ def translate(text, target_language):
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",
@@ -34,12 +47,26 @@ language_options = [
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()
@@ -47,3 +74,4 @@ iface.launch()
47
 
48
 
49
 
 
 
1
  import gradio as gr
2
+ from transformers import MarianMTModel, MarianTokenizer, pipeline
3
 
4
  def translate(text, target_language):
5
  language_codes = {
 
27
  translation = tokenizer.decode(outputs[0], skip_special_tokens=True)
28
  return translation
29
 
30
+ def classify_text(text, labels):
31
+ classifier = pipeline("zero-shot-classification")
32
+ result = classifier(text, labels.split(','))
33
+ scores = result["scores"]
34
+ predictions = result["labels"]
35
+ sorted_predictions = [pred for _, pred in sorted(zip(scores, predictions), reverse=True)]
36
+ return sorted_predictions
37
+
38
+ def generate_text(prompt, max_length):
39
+ text_gen = pipeline("text-generation", model="gpt2")
40
+ generated_text = text_gen(prompt, max_length=max_length, do_sample=True)[0]["generated_text"]
41
+ return generated_text
42
+
43
  language_options = [
44
  "Spanish", "French (European)", "French (Canadian)", "Italian", "Ukrainian",
45
  "Portuguese (Brazilian)", "Portuguese (European)", "Russian", "Chinese",
 
47
  ]
48
 
49
  iface = gr.Interface(
50
+ [translate, classify_text, generate_text],
51
  inputs=[
52
+ [
53
+ gr.inputs.Textbox(lines=5, label="Enter text to translate:"),
54
+ gr.inputs.Dropdown(choices=language_options, label="Target Language"),
55
+ ],
56
+ [
57
+ gr.inputs.Textbox(lines=5, label="Enter text to classify:"),
58
+ gr.inputs.Textbox(lines=2, label="Enter comma-separated labels:"),
59
+ ],
60
+ [
61
+ gr.inputs.Textbox(lines=5, label="Enter a prompt for text generation:"),
62
+ gr.inputs.Slider(minimum=10, maximum=150, step=1, default=50, label="Max Length"),
63
+ ],
64
+ ],
65
+ outputs=[
66
+ gr.outputs.Textbox(label="Translated Text"),
67
+ gr.outputs.Textbox(label="Classification"),
68
+ gr.outputs.Textbox(label="Generated Text"),
69
  ],
 
70
  )
71
 
72
  iface.launch()
 
74
 
75
 
76
 
77
+