Update app.py
Browse files
app.py
CHANGED
@@ -4,8 +4,19 @@ import json
|
|
4 |
import requests
|
5 |
import xml.etree.ElementTree as ET
|
6 |
|
7 |
-
#
|
8 |
-
LOG_FILE = 'chat_logs.txt'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
print(f"Dosya yolu: {os.path.abspath(LOG_FILE)}")
|
10 |
|
11 |
# API ayarları
|
@@ -53,9 +64,10 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
|
|
53 |
print(new_msg)
|
54 |
messages.append({"role": "system", "content": new_msg})
|
55 |
|
|
|
56 |
for data in chatbot:
|
57 |
-
messages.append({"role": "user", "content": data["content"]})
|
58 |
-
messages.append({"role": "assistant", "content": data["content"]})
|
59 |
|
60 |
messages.append({"role": "user", "content": inputs})
|
61 |
|
@@ -82,6 +94,9 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
|
|
82 |
except Exception as e:
|
83 |
print(f"Dosya yazma hatası (Kullanıcı): {e}")
|
84 |
|
|
|
|
|
|
|
85 |
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
86 |
print(f"Logging: Response code - {response.status_code}")
|
87 |
if response.status_code != 200:
|
@@ -102,19 +117,18 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
|
|
102 |
try:
|
103 |
chunk_data = json.loads(chunk_str[6:])
|
104 |
delta = chunk_data['choices'][0]['delta']
|
105 |
-
if 'content' in delta:
|
106 |
content = delta['content']
|
107 |
partial_words += content
|
108 |
print(f"İçerik eklendi: {content}")
|
109 |
print(f"Güncel partial_words: {partial_words}")
|
110 |
-
else:
|
111 |
-
print("Bu chunk içerik içermiyor")
|
112 |
except json.JSONDecodeError as e:
|
113 |
print(f"JSON parse hatası: {e} - Chunk: {chunk_str}")
|
114 |
elif chunk_str == "data: [DONE]":
|
115 |
print("Akış tamamlandı: [DONE] alındı")
|
116 |
if partial_words:
|
117 |
-
history.append(partial_words)
|
|
|
118 |
try:
|
119 |
with open(LOG_FILE, 'a', encoding='utf-8') as f:
|
120 |
f.write(f"Bot: {partial_words}\n")
|
@@ -123,14 +137,13 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
|
|
123 |
print(f"Dosya yazma hatası (Bot): {e}")
|
124 |
|
125 |
# Chatbot için geçici güncelleme
|
126 |
-
chat =
|
127 |
-
[{"role": "assistant", "content": history[i+1]} for i in range(1, len(history)-1, 2)]
|
128 |
if partial_words and len(history) % 2 != 0: # Bot yanıtı tamamlanmadıysa geçici ekle
|
129 |
-
chat
|
130 |
|
131 |
yield chat, history, chat_counter
|
132 |
|
133 |
-
return
|
134 |
|
135 |
def save_chat(chatbot):
|
136 |
file_path = os.path.abspath(LOG_FILE)
|
@@ -194,8 +207,9 @@ theme = gr.themes.Base(
|
|
194 |
)
|
195 |
|
196 |
with gr.Blocks(css=demo_css, theme=theme) as demo:
|
197 |
-
|
198 |
-
|
|
|
199 |
|
200 |
with gr.Column(elem_id="col_container"):
|
201 |
with gr.Accordion("", open=False, visible=False):
|
|
|
4 |
import requests
|
5 |
import xml.etree.ElementTree as ET
|
6 |
|
7 |
+
# Kalıcı dosya yolu: Space'in persistent storage dizinine kaydetmek için
|
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)
|
15 |
+
print(f"Kalıcı depolama dizini oluşturuldu: {persistent_dir}")
|
16 |
+
except Exception as e:
|
17 |
+
print(f"Kalıcı depolama dizini oluşturulamadı: {e}. Geçici dizine geri dönülüyor.")
|
18 |
+
LOG_FILE = 'chat_logs.txt'
|
19 |
+
|
20 |
print(f"Dosya yolu: {os.path.abspath(LOG_FILE)}")
|
21 |
|
22 |
# API ayarları
|
|
|
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 |
+
messages.append({"role": "user", "content": data["content"] if data["role"] == "user" else ""})
|
70 |
+
messages.append({"role": "assistant", "content": data["content"] if data["role"] == "assistant" else ""})
|
71 |
|
72 |
messages.append({"role": "user", "content": inputs})
|
73 |
|
|
|
94 |
except Exception as e:
|
95 |
print(f"Dosya yazma hatası (Kullanıcı): {e}")
|
96 |
|
97 |
+
# Chatbot'a kullanıcı mesajını ekle
|
98 |
+
chatbot.append({"role": "user", "content": inputs})
|
99 |
+
|
100 |
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
101 |
print(f"Logging: Response code - {response.status_code}")
|
102 |
if response.status_code != 200:
|
|
|
117 |
try:
|
118 |
chunk_data = json.loads(chunk_str[6:])
|
119 |
delta = chunk_data['choices'][0]['delta']
|
120 |
+
if 'content' in delta and delta['content']:
|
121 |
content = delta['content']
|
122 |
partial_words += content
|
123 |
print(f"İçerik eklendi: {content}")
|
124 |
print(f"Güncel partial_words: {partial_words}")
|
|
|
|
|
125 |
except json.JSONDecodeError as e:
|
126 |
print(f"JSON parse hatası: {e} - Chunk: {chunk_str}")
|
127 |
elif chunk_str == "data: [DONE]":
|
128 |
print("Akış tamamlandı: [DONE] alındı")
|
129 |
if partial_words:
|
130 |
+
history.append(partial_words)
|
131 |
+
chatbot.append({"role": "assistant", "content": partial_words}) # Bot yanıtını kalıcı olarak ekle
|
132 |
try:
|
133 |
with open(LOG_FILE, 'a', encoding='utf-8') as f:
|
134 |
f.write(f"Bot: {partial_words}\n")
|
|
|
137 |
print(f"Dosya yazma hatası (Bot): {e}")
|
138 |
|
139 |
# Chatbot için geçici güncelleme
|
140 |
+
chat = chatbot.copy() # Mevcut chatbot içeriğini koru
|
|
|
141 |
if partial_words and len(history) % 2 != 0: # Bot yanıtı tamamlanmadıysa geçici ekle
|
142 |
+
chat[-1] = {"role": "assistant", "content": partial_words} if chat[-1]["role"] == "assistant" else {"role": "assistant", "content": partial_words}
|
143 |
|
144 |
yield chat, history, chat_counter
|
145 |
|
146 |
+
return chatbot, history, chat_counter # Güncellenmiş chatbot'u döndür
|
147 |
|
148 |
def save_chat(chatbot):
|
149 |
file_path = os.path.abspath(LOG_FILE)
|
|
|
207 |
)
|
208 |
|
209 |
with gr.Blocks(css=demo_css, theme=theme) as demo:
|
210 |
+
if not os.path.exists(LOG_FILE):
|
211 |
+
with open(LOG_FILE, 'w', encoding='utf-8') as f:
|
212 |
+
f.write("--- Yeni Sohbet ---\n")
|
213 |
|
214 |
with gr.Column(elem_id="col_container"):
|
215 |
with gr.Accordion("", open=False, visible=False):
|