Update app.py
Browse files
app.py
CHANGED
@@ -4,11 +4,10 @@ import json
|
|
4 |
import requests
|
5 |
import xml.etree.ElementTree as ET
|
6 |
|
7 |
-
#
|
8 |
LOG_FILE = '/persistent-storage/chat_logs.txt'
|
9 |
-
|
10 |
-
# persistent-storage dizinini kontrol et ve gerekirse oluştur
|
11 |
persistent_dir = '/persistent-storage'
|
|
|
12 |
if not os.path.exists(persistent_dir):
|
13 |
try:
|
14 |
os.makedirs(persistent_dir, exist_ok=True)
|
@@ -26,7 +25,6 @@ if not OPENAI_API_KEY:
|
|
26 |
print("Hata: OPENAI_API_KEY çevre değişkeni ayarlanmamış!")
|
27 |
|
28 |
url = 'https://www.trekbisiklet.com.tr/output/8582384479'
|
29 |
-
|
30 |
response = requests.get(url)
|
31 |
root = ET.fromstring(response.content)
|
32 |
|
@@ -43,7 +41,12 @@ for item in root.findall('item'):
|
|
43 |
item_info = (stockAmount, price)
|
44 |
products.append((name, item_info, full_name))
|
45 |
|
46 |
-
def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=
|
|
|
|
|
|
|
|
|
|
|
47 |
headers = {
|
48 |
"Content-Type": "application/json",
|
49 |
"Authorization": f"Bearer {OPENAI_API_KEY}"
|
@@ -64,10 +67,11 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
|
|
64 |
print(new_msg)
|
65 |
messages.append({"role": "system", "content": new_msg})
|
66 |
|
67 |
-
# Mevcut chatbot geçmişini mesajlara ekle
|
68 |
for data in chatbot:
|
69 |
-
|
70 |
-
|
|
|
|
|
71 |
|
72 |
messages.append({"role": "user", "content": inputs})
|
73 |
|
@@ -83,7 +87,7 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
|
|
83 |
}
|
84 |
|
85 |
chat_counter += 1
|
86 |
-
history.append(inputs)
|
87 |
print(f"Logging: Payload is - {payload}")
|
88 |
|
89 |
# Kullanıcı mesajını dosyaya yaz
|
@@ -128,7 +132,7 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
|
|
128 |
print("Akış tamamlandı: [DONE] alındı")
|
129 |
if partial_words:
|
130 |
history.append(partial_words)
|
131 |
-
chatbot.append({"role": "assistant", "content": partial_words})
|
132 |
try:
|
133 |
with open(LOG_FILE, 'a', encoding='utf-8') as f:
|
134 |
f.write(f"Bot: {partial_words}\n")
|
@@ -136,14 +140,17 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
|
|
136 |
except Exception as e:
|
137 |
print(f"Dosya yazma hatası (Bot): {e}")
|
138 |
|
139 |
-
# Chatbot
|
140 |
-
chat = chatbot.copy()
|
141 |
-
if partial_words and
|
142 |
-
chat
|
|
|
|
|
143 |
|
144 |
yield chat, history, chat_counter
|
145 |
|
146 |
-
|
|
|
147 |
|
148 |
def save_chat(chatbot):
|
149 |
file_path = os.path.abspath(LOG_FILE)
|
|
|
4 |
import requests
|
5 |
import xml.etree.ElementTree as ET
|
6 |
|
7 |
+
# Dosya yolu: Kalıcı depolama için öncelik, yoksa geçici dizin
|
8 |
LOG_FILE = '/persistent-storage/chat_logs.txt'
|
|
|
|
|
9 |
persistent_dir = '/persistent-storage'
|
10 |
+
|
11 |
if not os.path.exists(persistent_dir):
|
12 |
try:
|
13 |
os.makedirs(persistent_dir, exist_ok=True)
|
|
|
25 |
print("Hata: OPENAI_API_KEY çevre değişkeni ayarlanmamış!")
|
26 |
|
27 |
url = 'https://www.trekbisiklet.com.tr/output/8582384479'
|
|
|
28 |
response = requests.get(url)
|
29 |
root = ET.fromstring(response.content)
|
30 |
|
|
|
41 |
item_info = (stockAmount, price)
|
42 |
products.append((name, item_info, full_name))
|
43 |
|
44 |
+
def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=None, history=None):
|
45 |
+
if chatbot is None:
|
46 |
+
chatbot = []
|
47 |
+
if history is None:
|
48 |
+
history = []
|
49 |
+
|
50 |
headers = {
|
51 |
"Content-Type": "application/json",
|
52 |
"Authorization": f"Bearer {OPENAI_API_KEY}"
|
|
|
67 |
print(new_msg)
|
68 |
messages.append({"role": "system", "content": new_msg})
|
69 |
|
|
|
70 |
for data in chatbot:
|
71 |
+
if data["role"] == "user":
|
72 |
+
messages.append({"role": "user", "content": data["content"]})
|
73 |
+
elif data["role"] == "assistant":
|
74 |
+
messages.append({"role": "assistant", "content": data["content"]})
|
75 |
|
76 |
messages.append({"role": "user", "content": inputs})
|
77 |
|
|
|
87 |
}
|
88 |
|
89 |
chat_counter += 1
|
90 |
+
history.append(inputs)
|
91 |
print(f"Logging: Payload is - {payload}")
|
92 |
|
93 |
# Kullanıcı mesajını dosyaya yaz
|
|
|
132 |
print("Akış tamamlandı: [DONE] alındı")
|
133 |
if partial_words:
|
134 |
history.append(partial_words)
|
135 |
+
chatbot.append({"role": "assistant", "content": partial_words})
|
136 |
try:
|
137 |
with open(LOG_FILE, 'a', encoding='utf-8') as f:
|
138 |
f.write(f"Bot: {partial_words}\n")
|
|
|
140 |
except Exception as e:
|
141 |
print(f"Dosya yazma hatası (Bot): {e}")
|
142 |
|
143 |
+
# Chatbot içeriğini güncelle ve yield ile gönder
|
144 |
+
chat = chatbot.copy()
|
145 |
+
if partial_words and chat and chat[-1]["role"] == "user":
|
146 |
+
chat.append({"role": "assistant", "content": partial_words})
|
147 |
+
elif partial_words and chat and chat[-1]["role"] == "assistant":
|
148 |
+
chat[-1] = {"role": "assistant", "content": partial_words}
|
149 |
|
150 |
yield chat, history, chat_counter
|
151 |
|
152 |
+
print(f"Son chatbot durumu: {chatbot}")
|
153 |
+
return chatbot, history, chat_counter
|
154 |
|
155 |
def save_chat(chatbot):
|
156 |
file_path = os.path.abspath(LOG_FILE)
|