Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,115 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
}
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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/bootstrap@5.1.0/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()
|