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

Update app.py

Browse files

Added extra futures

Files changed (1) hide show
  1. app.py +47 -14
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import gradio as gr
2
- from transformers import MarianMTModel, MarianTokenizer
 
3
 
 
4
  def translate(text, target_language):
5
  language_codes = {
6
  "Spanish": "es",
@@ -18,32 +20,63 @@ def translate(text, target_language):
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
 
 
1
  import gradio as gr
2
+ from transformers import MarianMTModel, MarianTokenizer, GPT2LMHeadModel, GPT2Tokenizer, AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
 
5
+ # Translation
6
  def translate(text, target_language):
7
  language_codes = {
8
  "Spanish": "es",
 
20
  "Hebrew": "he",
21
  "Greek": "el"
22
  }
23
+
24
+
25
+ # Text Generation
26
+ def generate_text(prompt):
27
+ text_gen = pipeline("text-generation", model="gpt2")
28
+ generated_text = text_gen(prompt, max_length=max_length, do_sample=True)[0]["generated_text"]
29
+ return generated_text
30
+
31
+
32
+
33
+
34
+ # Text Classification
35
+ def classify_text(text):
36
+ classifier = pipeline("zero-shot-classification")
37
+ result = classifier(text, labels.split(','))
38
+ scores = result["scores"]
39
+ predictions = result["labels"]
40
+ sorted_predictions = [pred for _, pred in sorted(zip(scores, predictions), reverse=True)]
41
+ return sorted_predictions
42
+
43
+
44
+
45
+ # Sentiment Analysis
46
+ def sentiment_analysis(text):
47
+ model_name = "distilbert-base-uncased-finetuned-sst-2-english"
48
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
49
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
50
  inputs = tokenizer(text, return_tensors="pt")
51
+ outputs = model(**inputs)
52
+ sentiment_scores = torch.softmax(outputs.logits, dim=1)
53
+ sentiment = "positive" if sentiment_scores[0, 1] > sentiment_scores[0, 0] else "negative"
54
+ return sentiment
55
 
56
+ language_options = [
57
  "Spanish", "French (European)", "French (Canadian)", "Italian", "Ukrainian",
58
  "Portuguese (Brazilian)", "Portuguese (European)", "Russian", "Chinese",
59
  "Dutch", "German", "Arabic", "Hebrew", "Greek"
60
  ]
61
 
62
+ iface = gr.Interface(
63
+ [translate, generate_text, classify_text, sentiment_analysis],
64
  inputs=[
65
  gr.inputs.Textbox(lines=5, label="Enter text to translate:"),
66
  gr.inputs.Dropdown(choices=language_options, label="Target Language"),
67
+ gr.inputs.Textbox(lines=5, label="Enter text for text generation:"),
68
+ gr.inputs.Textbox(lines=5, label="Enter text for text classification:"),
69
+ gr.inputs.Textbox(lines=5, label="Enter text for sentiment analysis:"),
70
+ ],
71
+ outputs=[
72
+ gr.outputs.Textbox(label="Translated Text"),
73
+ gr.outputs.Textbox(label="Generated Text"),
74
+ gr.outputs.Textbox(label="Classification Result"),
75
+ gr.outputs.Textbox(label="Sentiment Result"),
76
  ],
 
77
  )
78
 
79
+ iface.launch()
 
80
 
81
 
82