-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuygulama.py
63 lines (48 loc) · 1.79 KB
/
uygulama.py
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
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import pipeline, AutoTokenizer, AutoModelForTokenClassification
import nltk
nltk.download('punkt')
app = FastAPI()
# Türkçe Varlık Tespiti (NER) için gerekli model ve tokenizer'ı yükle
tokenizer = AutoTokenizer.from_pretrained("savasy/bert-base-turkish-ner-cased")
model = AutoModelForTokenClassification.from_pretrained("savasy/bert-base-turkish-ner-cased")
ner = pipeline("ner", model=model, tokenizer=tokenizer, grouped_entities=True)
# Sentiment Analysis için pipeline'ı başlat
sentiment_analyzer = pipeline("sentiment-analysis")
class TextRequest(BaseModel):
text: str # Bu modelin anahtarı "text" olarak güncellenmeli
def classify_sentiment(text):
result = sentiment_analyzer(text)
label = result[0]['label']
# Türkçe duygu etiketlerini belirleyin
if label.lower() == 'positive':
label = 'olumlu'
elif label.lower() == 'negative':
label = 'olumsuz'
else:
label = 'nötr'
return label
def extract_entities(text):
entities = ner(text)
entity_list = [entity['word'] for entity in entities]
return entity_list
@app.post("/process_review/")
async def process_review(request: TextRequest):
text = request.text # "text" anahtarını kullanarak metni alın
# Varlıkları çıkar
entities = extract_entities(text)
# Her varlık için duygu analizi yap
results = []
for entity in entities:
entity_sentiment = classify_sentiment(entity)
results.append({"entity": entity, "sentiment": entity_sentiment})
# Çıktıyı hazırla
output = {
"entity_list": entities,
"results": results
}
return output
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=9524)