gsar78 commited on
Commit
6050aa2
·
verified ·
1 Parent(s): 2ed3a46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -47
app.py CHANGED
@@ -1,50 +1,115 @@
1
  import gradio as gr
 
2
 
3
- def predict(text):
4
- # Tokenize the input text
5
- inputs = tokenizer(text, return_tensors="pt")
6
- # Get the model outputs
7
- outputs = model(**inputs)
8
- # Apply softmax to the logits to get probabilities
9
- scores = torch.nn.functional.softmax(outputs.logits, dim=1)
10
- # Get the predicted label
11
- predicted_label_idx = scores.argmax(dim=1).item()
12
- labels = ["negative", "neutral", "positive"]
13
- predicted_label = labels[predicted_label_idx]
14
- confidence_score = scores[0, predicted_label_idx].item()
15
- # Create a dictionary with the prediction and scores
16
- result = {
17
- "text": text,
18
- "label": predicted_label,
19
- "score": confidence_score,
20
- "scores": {
21
- "positive": scores[0, 2].item(),
22
- "neutral": scores[0, 1].item(),
23
- "negative": scores[0, 0].item()
24
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  }
26
- return result
27
-
28
- iface = gr.Interface(
29
- fn=predict,
30
- inputs="text",
31
- outputs=[
32
- gr.Textbox(label="Prediction"),
33
- gr.Label(label="Label Confidence")
34
- ],
35
- title="Hellenic Sentiment AI",
36
- description=None,
37
- article=None,
38
- theme="default",
39
- flagging_dir=None,
40
- css=None,
41
- analytics_script=None,
42
- allow_flagging="never",
43
- allow_screenshot=True,
44
- enable_queue=True,
45
- show_input=True,
46
- show_output=True,
47
- footer="Development by Geo Sar"
48
- )
49
-
50
- iface.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
 
4
+ def sentiment_analysis_generate_text(text):
5
+ # Define the model
6
+ model_name = "gsar78/HellenicSentimentAI"
7
+
8
+ # Create the pipeline
9
+ nlp = pipeline("sentiment-analysis", model=model_name)
10
+ # Split the input text into individual sentences
11
+ sentences = text.split('|')
12
+ # Run the pipeline on each sentence and collect the results
13
+ results = nlp(sentences)
14
+ output = []
15
+ for sentence, result in zip(sentences, results):
16
+ output.append(f"Text: {sentence.strip()}\nSentiment: {result['label']}, Score: {result['score']:.4f}\n")
17
+
18
+ # Join the results into a single string to return
19
+ return "\n".join(output)
20
+
21
+
22
+ def sentiment_analysis_generate_table(text):
23
+ # Define the model
24
+ model_name = "gsar78/HellenicSentimentAI"
25
+ # Create the pipeline
26
+ nlp = pipeline("sentiment-analysis", model=model_name)
27
+ # Split the input text into individual sentences
28
+ sentences = text.split('|')
29
+
30
+ # Generate the HTML table with enhanced colors and bold headers
31
+ html = """
32
+ <html>
33
+ <head>
34
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bootstrap.min.css">
35
+ <style>
36
+ .label {
37
+ transition: .15s;
38
+ border-radius: 8px;
39
+ padding: 5px 10px;
40
+ font-size: 14px;
41
+ text-transform: uppercase;
42
+ }
43
+ .positive {
44
+ background-color: rgb(54, 176, 75);
45
+ color: white;
46
+ }
47
+ .negative {
48
+ background-color: rgb(237, 83, 80);
49
+ color: white;
50
+ }
51
+ .neutral {
52
+ background-color: rgb(52, 152, 219);
53
+ color: white;
54
  }
55
+ th {
56
+ font-weight: bold;
57
+ color: rgb(106, 38, 198);
58
+ }
59
+ </style>
60
+ </head>
61
+ <body>
62
+ <table class="table table-striped">
63
+ <thead>
64
+ <tr>
65
+ <th scope="col">Text</th>
66
+ <th scope="col">Score</th>
67
+ <th scope="col">Sentiment</th>
68
+ </tr>
69
+ </thead>
70
+ <tbody>
71
+ """
72
+ for sentence in sentences:
73
+ result = nlp(sentence.strip())[0]
74
+ text = sentence.strip()
75
+ score = f"{result['score']:.4f}"
76
+ sentiment = result['label']
77
+
78
+ # Determine the sentiment class
79
+ if sentiment == "Positive":
80
+ sentiment_class = "positive"
81
+ elif sentiment == "Negative":
82
+ sentiment_class = "negative"
83
+ else:
84
+ sentiment_class = "neutral"
85
+
86
+ # Generate table rows
87
+ html += f'<tr><td>{text}</td><td>{score}</td><td><span class="label {sentiment_class}">{sentiment}</span></td></tr>'
88
+
89
+ html += """
90
+ </tbody>
91
+ </table>
92
+ </body>
93
+ </html>
94
+ """
95
+
96
+ return html
97
+
98
+
99
+ if __name__ == "__main__":
100
+ iface = gr.Interface(
101
+ sentiment_analysis_generate_table,
102
+ gr.Textbox(placeholder="Enter sentence here..."),
103
+ ["html"],
104
+ title="Hellenic Sentiment AI",
105
+ description="<p>A sentiment analysis model for primarily the Greek language</p>"
106
+ "<p>Enter some text to see whether the sentiment is positive, neutral or negative.</p>",
107
+ examples=[
108
+ ['Η πικάντικη γεύση αυτής της σούπας λαχανικών ήταν ακριβώς αυτό που χρειαζόμουν σήμερα. Είχε μια ωραία γαργαλιστική αίσθηση χωρίς να είναι πολύ καυτερή.'],
109
+ ['Η πίτσα ήταν καμένη και τα υλικά φθηνής ποιότητας. Σίγουρα δεν θα ξαναπαραγγείλω απ�� εκεί.']
110
+ ],
111
+ allow_flagging=False,
112
+ examples_per_page=2,
113
+ )
114
+
115
+ iface.launch()