|
|
import gradio as gr |
|
|
from transformers import pipeline |
|
|
import time |
|
|
|
|
|
|
|
|
print("Loading model...") |
|
|
|
|
|
classifier = pipeline( |
|
|
"sentiment-analysis", |
|
|
model="./istrenirani_model", |
|
|
device=-1 |
|
|
) |
|
|
print("Model loaded successfully!") |
|
|
|
|
|
|
|
|
def prevedi_labelu(label, score): |
|
|
"""Translate model labels to Serbian""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if label == 'LABEL_0': |
|
|
return "🔴 Negativan", score, "#ff4444" |
|
|
elif label == 'LABEL_1': |
|
|
return "⚪ Neutralan", score, "#888888" |
|
|
elif label == 'LABEL_2': |
|
|
return "🟢 Pozitivan", score, "#44ff44" |
|
|
return "❓ Nepoznat", score, "#888888" |
|
|
|
|
|
|
|
|
def analyze_sentiment(text): |
|
|
"""Analyze sentiment of Serbian text""" |
|
|
if not text or not text.strip(): |
|
|
return "⚠️ Molim vas unesite tekst!" |
|
|
|
|
|
try: |
|
|
|
|
|
result = classifier(text)[0] |
|
|
|
|
|
|
|
|
sentiment, confidence, color = prevedi_labelu(result['label'], result['score']) |
|
|
|
|
|
|
|
|
sentiment_html = f""" |
|
|
<div style='text-align: center; padding: 40px; border-radius: 15px; background-color: {color}22; border: 3px solid {color};'> |
|
|
<h1 style='color: {color}; margin: 0; font-size: 56px;'>{sentiment}</h1> |
|
|
</div> |
|
|
""" |
|
|
|
|
|
return sentiment_html |
|
|
|
|
|
except Exception as e: |
|
|
return f"❌ Greška: {str(e)}" |
|
|
|
|
|
|
|
|
|
|
|
custom_css = """ |
|
|
#sentiment-output { |
|
|
font-size: 24px; |
|
|
font-weight: bold; |
|
|
} |
|
|
.gradio-container { |
|
|
max-width: 800px; |
|
|
margin: auto; |
|
|
} |
|
|
""" |
|
|
|
|
|
|
|
|
with gr.Blocks(css=custom_css, title="Serbian Sentiment Analyzer", theme=gr.themes.Soft()) as demo: |
|
|
|
|
|
|
|
|
gr.Markdown( |
|
|
""" |
|
|
# 🎭 Analiza Sentimenta - Srpski Jezik |
|
|
|
|
|
Unesite tekst na srpskom jeziku da biste analizirali sentiment. |
|
|
|
|
|
Model prepoznaje: **Pozitivan** 🟢, **Neutralan** ⚪, **Negativan** 🔴 |
|
|
|
|
|
--- |
|
|
""" |
|
|
) |
|
|
|
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
text_input = gr.Textbox( |
|
|
label="Unesite tekst za analizu", |
|
|
placeholder="Npr: Ovaj film je bio fenomenalan!", |
|
|
lines=5 |
|
|
) |
|
|
|
|
|
analyze_btn = gr.Button("🔍 Analiziraj Sentiment", variant="primary", size="lg") |
|
|
|
|
|
with gr.Row(): |
|
|
sentiment_output = gr.HTML(label="Rezultat", elem_id="sentiment-output") |
|
|
|
|
|
|
|
|
analyze_btn.click( |
|
|
fn=analyze_sentiment, |
|
|
inputs=text_input, |
|
|
outputs=sentiment_output |
|
|
) |
|
|
|
|
|
|
|
|
gr.Markdown( |
|
|
""" |
|
|
--- |
|
|
|
|
|
### ℹ️ O Modelu |
|
|
|
|
|
- **Baziran na**: [dejanseo/BERTic-sentiment](https://huggingface.co/dejanseo/BERTic-sentiment) |
|
|
- **Istreniran na**: 4500 srpskih rečenica (1500 po kategoriji) |
|
|
- **Klase**: Pozitivan, Neutralan, Negativan |
|
|
|
|
|
🤖 Napravljeno sa ❤️ koristeći Transformers i Gradio (Iako mi je Streamlit bolji) |
|
|
|
|
|
--- |
|
|
|
|
|
### 🔌 Korišćenje API-ja |
|
|
|
|
|
Ovaj Space podržava API koji možete pozivati programski... |
|
|
|
|
|
Primer (JavaScript): |
|
|
|
|
|
```javascript |
|
|
const response = await fetch("https://huggingface.co/spaces/codepanov/serbian-sentiment-analyzer/api/predict", { |
|
|
method: "POST", |
|
|
headers: { "Content-Type": "application/json" }, |
|
|
body: JSON.stringify({ data: ["Vaš tekst ovde"] }) |
|
|
}); |
|
|
const result = await response.json(); |
|
|
``` |
|
|
""" |
|
|
) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |
|
|
|