Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import pipeline | |
import os | |
HF_TOKEN = os.getenv('HF_TOKEN') | |
hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "crowdsourced-sentiment") | |
def sentiment_analysis_generate_text(text): | |
# Define the model | |
model_name = "gsar78/HellenicSentimentAI" | |
# Create the pipeline | |
nlp = pipeline("sentiment-analysis", model=model_name) | |
# Split the input text into individual sentences | |
sentences = text.split('|') | |
# Run the pipeline on each sentence and collect the results | |
results = nlp(sentences) | |
output = [] | |
for sentence, result in zip(sentences, results): | |
output.append(f"Text: {sentence.strip()}\nSentiment: {result['label']}, Score: {result['score']:.4f}\n") | |
# Join the results into a single string to return | |
return "\n".join(output) | |
def sentiment_analysis_generate_table(text): | |
# Define the model | |
model_name = "gsar78/HellenicSentimentAI" | |
# Create the pipeline | |
nlp = pipeline("sentiment-analysis", model=model_name) | |
# Split the input text into individual sentences | |
sentences = text.split('|') | |
# Generate the HTML table with enhanced colors and bold headers | |
html = """ | |
<html> | |
<head> | |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bootstrap.min.css"> | |
<style> | |
.label { | |
transition: .15s; | |
border-radius: 8px; | |
padding: 5px 10px; | |
font-size: 14px; | |
text-transform: uppercase; | |
} | |
.positive { | |
background-color: rgb(54, 176, 75); | |
color: white; | |
} | |
.negative { | |
background-color: rgb(237, 83, 80); | |
color: white; | |
} | |
.neutral { | |
background-color: rgb(255, 165, 0); | |
color: white; | |
} | |
th { | |
font-weight: bold; | |
color: rgb(106, 38, 198); | |
} | |
</style> | |
</head> | |
<body> | |
<table class="table table-striped"> | |
<thead> | |
<tr> | |
<th scope="col">Text</th> | |
<th scope="col">Score</th> | |
<th scope="col">Sentiment</th> | |
</tr> | |
</thead> | |
<tbody> | |
""" | |
for sentence in sentences: | |
result = nlp(sentence.strip())[0] | |
text = sentence.strip() | |
score = f"{result['score']:.4f}" | |
sentiment = result['label'] | |
# Determine the sentiment class | |
if sentiment.lower() == "positive": | |
sentiment_class = "positive" | |
elif sentiment.lower() == "negative": | |
sentiment_class = "negative" | |
else: | |
sentiment_class = "neutral" | |
# Generate table rows | |
html += f'<tr><td>{text}</td><td>{score}</td><td><span class="label {sentiment_class}">{sentiment}</span></td></tr>' | |
html += """ | |
</tbody> | |
</table> | |
</body> | |
</html> | |
""" | |
return html | |
if __name__ == "__main__": | |
iface = gr.Interface( | |
fn=sentiment_analysis_generate_table, | |
inputs=gr.Textbox(placeholder="Enter sentence here..."), | |
outputs=gr.HTML(), | |
title="Hellenic Sentiment AI", | |
description="A sentiment analysis model, primarily for the Greek language.<br>" | |
"Type in some text to see its sentiment classification: positive, neutral, or negative.<br>" | |
"Multiple sentences can be classified when separated by the | character.<br>" | |
"For Emotion & Sentiment Classification visit Version 2.0: <a href='https://gsar78-hellenicsentimentai-v2.hf.space' target='_blank'>Hellenic Sentiment AI v2</a><br>" | |
"Version 1.1 - Developed by GeoSar", | |
examples=[ | |
["Η πικάντικη γεύση αυτής της σούπας λαχανικών ήταν ακριβώς αυτό που χρειαζόμουν σήμερα. Είχε μια ωραία γαργαλιστική αίσθηση χωρίς να είναι πολύ καυτερή."], | |
["Η πίτσα ήταν καμένη και τα υλικά φθηνής ποιότητας. Σίγουρα δεν θα ξαναπαραγγείλω από εκεί."] | |
], | |
allow_flagging="manual", | |
flagging_options=["Incorrect", "Ambiguous"], | |
flagging_callback=hf_writer, | |
examples_per_page=2, | |
allow_duplication=False, | |
concurrency_limit="default" | |
) | |
iface.launch(share=True) |