Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
-
#
|
|
|
|
| 5 |
sentiment_pipeline = pipeline(
|
| 6 |
"sentiment-analysis",
|
| 7 |
-
model="
|
| 8 |
-
top_k=None
|
| 9 |
)
|
| 10 |
|
| 11 |
def analyze_sentiment(text):
|
|
@@ -27,38 +28,29 @@ def analyze_sentiment(text):
|
|
| 27 |
|
| 28 |
try:
|
| 29 |
# Model'den tahmin al
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
-
# Label
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
'negative': 'negative',
|
| 44 |
-
'neg': 'negative',
|
| 45 |
-
'neutral': 'neutral',
|
| 46 |
-
'neu': 'neutral'
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
sentiment = sentiment_map.get(label, 'neutral')
|
| 50 |
-
|
| 51 |
-
# Negative için score'u normalize et
|
| 52 |
-
if sentiment == 'negative':
|
| 53 |
-
score = 1.0 - score
|
| 54 |
|
| 55 |
return {
|
| 56 |
"sentiment": sentiment,
|
| 57 |
"score": round(score, 4),
|
| 58 |
-
"
|
|
|
|
| 59 |
}
|
| 60 |
|
| 61 |
except Exception as e:
|
|
|
|
| 62 |
return {
|
| 63 |
"error": str(e),
|
| 64 |
"sentiment": "neutral",
|
|
@@ -69,7 +61,7 @@ def analyze_sentiment(text):
|
|
| 69 |
def gradio_interface(text):
|
| 70 |
result = analyze_sentiment(text)
|
| 71 |
|
| 72 |
-
if "error" in result:
|
| 73 |
return f"⚠️ Error: {result['error']}\n\nDefault: {result['sentiment']} ({result['score']})"
|
| 74 |
|
| 75 |
sentiment = result['sentiment']
|
|
@@ -85,35 +77,60 @@ def gradio_interface(text):
|
|
| 85 |
emoji = emoji_map.get(sentiment, '😐')
|
| 86 |
|
| 87 |
output = f"{emoji} **Sentiment:** {sentiment.upper()}\n"
|
| 88 |
-
output += f"📊 **Score:** {score}\n\n"
|
| 89 |
-
output += "**All Scores:**\n"
|
| 90 |
|
| 91 |
-
|
| 92 |
-
output += f"
|
|
|
|
|
|
|
| 93 |
|
| 94 |
return output
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
label="Input Text"
|
| 103 |
-
),
|
| 104 |
-
outputs=gr.Markdown(label="Sentiment Analysis Result"),
|
| 105 |
-
title="🤖 Sentiment Analysis API",
|
| 106 |
-
description="Real-time sentiment analysis using Hugging Face Transformers. Analyzes text and returns positive, negative, or neutral sentiment with confidence scores.",
|
| 107 |
-
examples=[
|
| 108 |
-
["This is amazing! I love it!"],
|
| 109 |
-
["I'm so sad and disappointed."],
|
| 110 |
-
["The weather is okay today."],
|
| 111 |
-
["Bu harika bir gün! Çok mutluyum."],
|
| 112 |
-
["Berbat bir deneyimdi, hiç beğenmedim."]
|
| 113 |
-
],
|
| 114 |
-
theme=gr.themes.Soft(),
|
| 115 |
-
api_name="analyze"
|
| 116 |
-
)
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
import numpy as np
|
| 4 |
|
| 5 |
+
# Daha hafif ve hızlı sentiment analysis model
|
| 6 |
+
# distilbert-base-uncased-finetuned-sst-2-english -> Hafif ve hızlı
|
| 7 |
sentiment_pipeline = pipeline(
|
| 8 |
"sentiment-analysis",
|
| 9 |
+
model="distilbert-base-uncased-finetuned-sst-2-english"
|
|
|
|
| 10 |
)
|
| 11 |
|
| 12 |
def analyze_sentiment(text):
|
|
|
|
| 28 |
|
| 29 |
try:
|
| 30 |
# Model'den tahmin al
|
| 31 |
+
result = sentiment_pipeline(text)[0]
|
| 32 |
|
| 33 |
+
label = result['label'].lower()
|
| 34 |
+
score = result['score']
|
| 35 |
|
| 36 |
+
# Label mapping
|
| 37 |
+
if 'pos' in label:
|
| 38 |
+
sentiment = 'positive'
|
| 39 |
+
elif 'neg' in label:
|
| 40 |
+
sentiment = 'negative'
|
| 41 |
+
score = 1.0 - score # Negative için score'u ters çevir
|
| 42 |
+
else:
|
| 43 |
+
sentiment = 'neutral'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
return {
|
| 46 |
"sentiment": sentiment,
|
| 47 |
"score": round(score, 4),
|
| 48 |
+
"raw_label": result['label'],
|
| 49 |
+
"raw_score": result['score']
|
| 50 |
}
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
+
print(f"Error in sentiment analysis: {str(e)}")
|
| 54 |
return {
|
| 55 |
"error": str(e),
|
| 56 |
"sentiment": "neutral",
|
|
|
|
| 61 |
def gradio_interface(text):
|
| 62 |
result = analyze_sentiment(text)
|
| 63 |
|
| 64 |
+
if "error" in result and result["error"] != "Please provide a text to analyze":
|
| 65 |
return f"⚠️ Error: {result['error']}\n\nDefault: {result['sentiment']} ({result['score']})"
|
| 66 |
|
| 67 |
sentiment = result['sentiment']
|
|
|
|
| 77 |
emoji = emoji_map.get(sentiment, '😐')
|
| 78 |
|
| 79 |
output = f"{emoji} **Sentiment:** {sentiment.upper()}\n"
|
| 80 |
+
output += f"📊 **Confidence Score:** {score}\n\n"
|
|
|
|
| 81 |
|
| 82 |
+
if "raw_label" in result:
|
| 83 |
+
output += f"**Raw Model Output:**\n"
|
| 84 |
+
output += f"- Label: {result['raw_label']}\n"
|
| 85 |
+
output += f"- Score: {result['raw_score']:.4f}\n"
|
| 86 |
|
| 87 |
return output
|
| 88 |
|
| 89 |
+
# API fonksiyonu (JSON response)
|
| 90 |
+
def api_analyze(text):
|
| 91 |
+
"""
|
| 92 |
+
API endpoint için JSON response döner
|
| 93 |
+
"""
|
| 94 |
+
return analyze_sentiment(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
+
# Gradio UI
|
| 97 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 98 |
+
gr.Markdown("# 🤖 Sentiment Analysis API")
|
| 99 |
+
gr.Markdown("Real-time sentiment analysis using Hugging Face Transformers.")
|
| 100 |
+
|
| 101 |
+
with gr.Tab("Web Interface"):
|
| 102 |
+
with gr.Row():
|
| 103 |
+
with gr.Column():
|
| 104 |
+
input_text = gr.Textbox(
|
| 105 |
+
lines=3,
|
| 106 |
+
placeholder="Enter text to analyze sentiment...",
|
| 107 |
+
label="Input Text"
|
| 108 |
+
)
|
| 109 |
+
analyze_btn = gr.Button("Analyze", variant="primary")
|
| 110 |
+
|
| 111 |
+
with gr.Column():
|
| 112 |
+
output_markdown = gr.Markdown(label="Result")
|
| 113 |
+
|
| 114 |
+
gr.Examples(
|
| 115 |
+
examples=[
|
| 116 |
+
["This is amazing! I love it!"],
|
| 117 |
+
["I'm so sad and disappointed."],
|
| 118 |
+
["The weather is okay today."],
|
| 119 |
+
["This product is terrible, worst purchase ever!"],
|
| 120 |
+
["Great service, highly recommend!"]
|
| 121 |
+
],
|
| 122 |
+
inputs=input_text
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
analyze_btn.click(
|
| 126 |
+
fn=gradio_interface,
|
| 127 |
+
inputs=input_text,
|
| 128 |
+
outputs=output_markdown
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
with gr.Tab("API Usage"):
|
| 132 |
+
gr.Markdown("""
|
| 133 |
+
## API Endpoint
|
| 134 |
+
|
| 135 |
+
Use this endpoint to analyze sentiment programmatically:
|
| 136 |
+
```python
|