File size: 7,451 Bytes
6401b38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#writefile app.py
import streamlit as st
import smtplib
from email.mime.text import MIMEText
from transformers import AutoTokenizer, AutoModelForTokenClassification, AutoModelForSequenceClassification, pipeline
import re
from pydantic import BaseModel, Field
from PIL import Image

ner_model_name = "Gorengoz/bert-based-Turkish-NER-wikiann"
sentiment_model_name = "Gorengoz/bert-turkish-sentiment-analysis-cased"

# Initialize tokenizers and models for NER
ner_tokenizer = AutoTokenizer.from_pretrained(ner_model_name)
ner_model = AutoModelForTokenClassification.from_pretrained(ner_model_name)

# Initialize tokenizers and models for Sentiment Analysis
sentiment_tokenizer = AutoTokenizer.from_pretrained(sentiment_model_name)
sentiment_model = AutoModelForSequenceClassification.from_pretrained(sentiment_model_name)

# Create pipelines for NER and Sentiment Analysis
ner_pipeline = pipeline("token-classification", model=ner_model, tokenizer=ner_tokenizer)
sentiment_pipeline = pipeline("text-classification", model=sentiment_model, tokenizer=sentiment_tokenizer)


label_mapping = {
    "LABEL_0": "olumlu",
    "LABEL_1": "nötr",
    "LABEL_2": "olumsuz"
}

class SentimentResult(BaseModel):
    entity: str
    sentiment: str

class AnalysisResponse(BaseModel):
    entity_list: list
    sentiment_list: list
# Her entity için cümleleri parçalama
def get_entity_sentences(entity: str, sentences: list) -> list:
    return [sent for sent in sentences if entity in sent]

def predict(review): 
    # NER modelini kullanarak entity'leri bulma
    entities = ner_pipeline(review, aggregation_strategy="simple")
    
    unique_entities = set([entity['word'] for entity in entities])
    
    # Cümleleri hem '.' hem de ',' ile bölme
    sentences = re.split(r'[.,]', review)
    sentences = [sentence.strip() for sentence in sentences if sentence.strip()]
    
    # Sentiment modelimizi kullanma
    entity_sentiments = {}
    for entity in unique_entities:
        entity_sentences = get_entity_sentences(entity, sentences)
        sentiments = [sentiment_pipeline(sentence) for sentence in entity_sentences]

        flattened_sentiments = [item for sublist in sentiments for item in sublist]
        
        # Entitilerin sentimentini bulma
        if flattened_sentiments:
            sentiment_scores = [sent['label'] for sent in flattened_sentiments]
            mapped_sentiments = [label_mapping.get(label, 'unknown') for label in sentiment_scores]
            sentiment = max(set(mapped_sentiments), key=mapped_sentiments.count)  # Bir marka için farklı sentimentler bulunduğunda en baskın olanı kabul etme.
        else:
            sentiment = 'nötr'  
            
        entity_sentiments[entity] = sentiment

        entity_list=list(entity_sentiments.keys())
        sentiment_list=list(entity_sentiments.values())

    return entity_list, sentiment_list


def send_email(to_address, subject, body):
    # Configure email details
    from_address = '[email protected]'
    smtp_server = 'smtp.gmail.com'
    smtp_port = 587
    smtp_user = '[email protected]'
    smtp_password = 'jtbb gfbd rfix thoe'  

    # Create email message
    msg = MIMEText(body, 'plain', 'utf-8')
    msg['Subject'] = subject
    msg['From'] = from_address
    msg['To'] = to_address

    # Send email
    with smtplib.SMTP(smtp_server, smtp_port) as server:
        server.starttls()
        server.login(smtp_user, smtp_password)
        server.sendmail(from_address, to_address, msg.as_string())

def send_customer_response(customer_email, sentiment):
    if sentiment == "olumlu":
        subject = "Geri Bildiriminiz İçin Teşekkürler"
        body = (f"Merhaba,\n\n"
                f"Geri bildiriminiz tarafımıza ulaştı ve olumlu değerlendirildi.\n\n"
                f"Memnuniyetiniz için teşekkür ederiz. Düşünceleriniz bizim için çok değerlidir ve bu tür olumlu yorumlar bizi motive eder. Size en iyi hizmeti sunmak için sürekli olarak çalışıyoruz.\n\n"
                f"Herhangi bir ek bilgiye ihtiyacınız olursa, lütfen bizimle iletişime geçin.\n\n"
                f"Saygılarımızla,\n")
    else:
        subject = "Şikayetiniz Alındı ve Üzerinde Çalışıyoruz"
        body = (f"Merhaba,\n\n"
                f"Şikayetiniz tarafımıza ulaştı ve değerlendirilmek üzere alındı.\n\n"
                f"Şikayetinizle ilgili olarak size geri dönüş yapacağız ve sorunun çözümü için gerekli adımları atacağız.\n\n"
                f"Herhangi bir ek bilgiye ihtiyacınız olursa, lütfen bizimle iletişime geçin.\n\n"
                f"İlginiz için teşekkür ederiz.\n\n"
                f"Saygılarımızla,\n")

    send_email(customer_email, subject, body)


def process_complaints(complaint):
        organizations, sentiment = predict(complaint)
        for org in organizations:
            # Construct email subject and body
            subject = f"{org} Hakkında Müşteri Geri Bildirimi"
            body = (f"Merhaba {org} Ekibi,\n\n"
                    f"Müşterilerimizden gelen aşağıdaki yorum tarafımıza iletilmiştir:\n\n"
                    f"Yorum: {complaint}\n\n"
                    f"Analiz: {sentiment[organizations.index(org)]}\n\n"
                    f"Geri bildirimleri ele alıp değerlendirdiğiniz için teşekkür ederiz. Bu konuda gerekli aksiyonları alacağınızdan eminiz. Herhangi bir ek bilgiye "
                    f"ihtiyacınız olursa lütfen bizimle iletişime geçin.\n\n"
                    f"İyi çalışmalar dileriz.\n\n"
                    f"Saygılarımızla,\n")
            # Use the fixed brand email address
            to_address = "[email protected]"
            send_email(to_address, subject, body)

        # Sending response to the customer
        send_customer_response(customer_email, sentiment)

# Streamlit application
col1, col2, col3 = st.columns(3)
# Merkezi konumlandırma için bir sütun düzeni kullanma
col1, col2, col3 = st.columns([1, 3, 2])  # Oranları isteğinize göre ayarlayabilirsiniz

with col1:
    st.write(" ")
with col2:
    image = Image.open('gg.png')
    st.image(image, width=400)
with col3:
    st.write(" ")

st.title('Müşteri Geri Bildirim Yönetimi')

# st.header('Yorum Bildir')
st.write("Müşteri/Tüketici Geri Bildirimi Yönetim Sistemi ile memnuniyet veya şikâyetlerinizi tarafımıza bildirebilirsiniz. ")
st.write("E-posta bilgilerinizi girerek 'Şikayet Gönder' butonuna tıklamanız dahilinde form verileri doğrulanır. ")
st.write("Geri bildirimizde belirttiğiniz her bir organizasyona memnuniyet veya şikâyetiniz iletildikten sonra; tarafınıza mail yoluyla bilgilendirme yapılır.")
with st.form(key='complaint_form'):
    complaint_text = st.text_area("Yorum Metni")
    customer_email = st.text_input("Müşteri E-Posta Adresi")
    submit_button = st.form_submit_button(label='Gönder')

if submit_button:
    if complaint_text and customer_email:
        process_complaints(complaint_text)
        st.success('Geri bildirim başarıyla işlendi ve e-postalar gönderildi.')
    else:
        st.error('Lütfen tüm alanları doldurduğunuzdan emin olun.')


# Trendyol sitesinden aldığım ürünü çok beğendim. Ancak kargolama berbattı @ArasKargo