Jesuscarr commited on
Commit
589d29d
1 Parent(s): d86acb9

Create main.py

Browse files

Added sentiment analysis

Files changed (1) hide show
  1. main.py +79 -0
main.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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",
9
+ "French (European)": "fr",
10
+ "French (Canadian)": "fr",
11
+ "Italian": "it",
12
+ "Ukrainian": "uk",
13
+ "Portuguese (Brazilian)": "pt_BR",
14
+ "Portuguese (European)": "pt",
15
+ "Russian": "ru",
16
+ "Chinese": "zh",
17
+ "Dutch": "nl",
18
+ "German": "de",
19
+ "Arabic": "ar",
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()