-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
126 lines (111 loc) · 4.43 KB
/
app.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
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
import os
import re
import base64
import requests
from openai import OpenAI
from dotenv import load_dotenv
from flask import Flask, render_template, request, jsonify
load_dotenv()
client = OpenAI(api_key=os.environ['OPENAI_API_KEY'])
app = Flask(__name__)
def get_blacklist():
url = "https://api.github.com/repos/ooguz/turkce-kufur-karaliste/contents/karaliste.txt"
response = requests.get(url)
if response.status_code == 200:
content = base64.b64decode(response.json()['content']).decode('utf-8')
blacklist = content.split('\n')
return [word.strip() for word in blacklist if word.strip()]
else:
raise Exception("GitHub'dan veri çekilemedi.")
def check_content(text):
blacklist = get_blacklist()
pattern = r'\b(' + '|'.join(re.escape(word) for word in blacklist) + r')\b'
if re.search(pattern, text, re.IGNORECASE):
return "Uygunsuz"
else:
return "Uygun"
def analyze_sentiment(text):
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "Verilen metni analiz et ve şu kategorilerden birine yerleştir: Uygun, Uygunsuz, Saldırgan, Kötümser, Kinayeli. Eğer 'Uygun' değilse, neden olduğuna dair kısa, tek cümlelik bir açıklama yap."},
{"role": "user", "content": text}
]
)
return response.choices[0].message.content
def analyze_image(image_path):
with open(image_path, "rb") as image_file:
base64_image = base64.b64encode(image_file.read()).decode('utf-8')
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Bu görüntüyü analiz et ve uygunluğunu değerlendir. Şu kategorilerden birine yerleştir: Uygun, Uygunsuz, Saldırgan, Kötümser, Kinayeli. Eğer 'Uygun' değilse, neden olduğuna dair kısa, tek cümlelik bir açıklama yap."},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
max_tokens=300
)
return response.choices[0].message.content
def analyze_text(text):
blacklist_result = check_content(text)
if blacklist_result == "Uygun":
sentiment_analysis = analyze_sentiment(text)
sentiment_parts = sentiment_analysis.split('.')
sentiment_category = sentiment_parts[0].strip()
if sentiment_category == "Uygun":
return "Metin onaylandı.", sentiment_category, ""
else:
feedback = sentiment_parts[1].strip() if len(sentiment_parts) > 1 else ""
return f"Metin onaylanmadı.", sentiment_category, feedback
else:
return "Metin kara liste kontrolünden geçemedi.", "Uygunsuz", "Kara listedeki kelime kullanımı"
def analyze_image_content(image_path):
image_analysis = analyze_image(image_path)
image_parts = image_analysis.split('.')
image_category = image_parts[0].strip()
if image_category == "Uygun":
return "Görsel onaylandı.", image_category, ""
else:
feedback = image_parts[1].strip() if len(image_parts) > 1 else ""
return f"Görsel onaylanmadı.", image_category, feedback
@app.route('/')
def home():
return render_template('index.html')
@app.route('/analyze_text', methods=['POST'])
def analyze_text_route():
text = request.form['text']
result, category, feedback = analyze_text(text)
return jsonify({
'result': result,
'category': category,
'feedback': feedback
})
@app.route('/analyze_image', methods=['POST'])
def analyze_image_route():
if 'image' not in request.files:
return jsonify({'error': 'No image uploaded'}), 400
image = request.files['image']
if image.filename == '':
return jsonify({'error': 'No image selected'}), 400
if image:
image_path = os.path.join('uploads', image.filename)
image.save(image_path)
result, category, feedback = analyze_image_content(image_path)
os.remove(image_path)
return jsonify({
'result': result,
'category': category,
'feedback': feedback
})
if __name__ == "__main__":
os.makedirs('uploads', exist_ok=True)
app.run(debug=True)