Update app.py
Browse files
app.py
CHANGED
@@ -79,7 +79,7 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
|
|
79 |
history.append(inputs)
|
80 |
print(f"Logging: Payload is - {payload}")
|
81 |
|
82 |
-
# Kullanıcı mesajını anında yazma
|
83 |
try:
|
84 |
with open(LOG_FILE, 'a', encoding='utf-8') as f:
|
85 |
f.write(f"User: {inputs}\n")
|
@@ -91,40 +91,59 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
|
|
91 |
print(f"Logging: Response code - {response.status_code}")
|
92 |
if response.status_code != 200:
|
93 |
print(f"API hatası: {response.text}")
|
|
|
94 |
|
95 |
token_counter = 0
|
96 |
partial_words = ""
|
97 |
counter = 0
|
98 |
for chunk in response.iter_lines():
|
99 |
-
|
100 |
-
|
101 |
continue
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
110 |
else:
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
def save_chat(chatbot):
|
|
|
125 |
file_path = os.path.abspath(LOG_FILE)
|
126 |
-
if not chatbot:
|
127 |
-
return f"Hata: Kaydedilecek sohbet yok!\nDosya: {file_path}"
|
128 |
try:
|
129 |
with open(LOG_FILE, 'a', encoding='utf-8') as f:
|
130 |
f.write("\n--- Kayıt Edilen Sohbet ---\n")
|
@@ -140,8 +159,6 @@ def save_chat(chatbot):
|
|
140 |
def reset_textbox():
|
141 |
return gr.update(value='')
|
142 |
|
143 |
-
|
144 |
-
|
145 |
# Gradio arayüzü
|
146 |
demo_css = """
|
147 |
#send_button, #save_button {
|
@@ -210,22 +227,17 @@ with gr.Blocks(css=demo_css, theme=theme) as demo:
|
|
210 |
save_button = gr.Button(value="Kayıt Et", elem_id="save_button")
|
211 |
|
212 |
state = gr.State([])
|
213 |
-
save_status = gr.Textbox(label="Kayıt Durumu", interactive=False)
|
214 |
|
215 |
with gr.Accordion("", open=False, visible=False):
|
216 |
top_p = gr.Slider(minimum=0, maximum=1.0, value=0.5, step=0.05, interactive=False, visible=False)
|
217 |
temperature = gr.Slider(minimum=0, maximum=5.0, value=0.1, step=0.1, interactive=False, visible=False)
|
218 |
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
219 |
|
220 |
-
# ENTER tuşuyla gönderme
|
221 |
inputs.submit(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
|
222 |
inputs.submit(reset_textbox, [], [inputs])
|
223 |
-
|
224 |
-
# Gönder butonu
|
225 |
send_button.click(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
|
226 |
send_button.click(reset_textbox, [], [inputs])
|
227 |
-
|
228 |
-
# Kayıt Et butonu
|
229 |
save_button.click(save_chat, [chatbot], [save_status])
|
230 |
|
231 |
demo.queue(max_size=10).launch(debug=True)
|
|
|
79 |
history.append(inputs)
|
80 |
print(f"Logging: Payload is - {payload}")
|
81 |
|
82 |
+
# Kullanıcı mesajını anında yazma
|
83 |
try:
|
84 |
with open(LOG_FILE, 'a', encoding='utf-8') as f:
|
85 |
f.write(f"User: {inputs}\n")
|
|
|
91 |
print(f"Logging: Response code - {response.status_code}")
|
92 |
if response.status_code != 200:
|
93 |
print(f"API hatası: {response.text}")
|
94 |
+
yield chatbot, history, chat_counter, response # Hata durumunda erken çıkış
|
95 |
|
96 |
token_counter = 0
|
97 |
partial_words = ""
|
98 |
counter = 0
|
99 |
for chunk in response.iter_lines():
|
100 |
+
counter += 1
|
101 |
+
if not chunk:
|
102 |
continue
|
103 |
+
chunk_str = chunk.decode('utf-8')
|
104 |
+
print(f"Chunk {counter}: {chunk_str}")
|
105 |
+
|
106 |
+
# Veri akışını kontrol et
|
107 |
+
if chunk_str.startswith("data: ") and chunk_str != "data: [DONE]":
|
108 |
+
try:
|
109 |
+
chunk_data = json.loads(chunk_str[6:]) # "data: " önekini çıkar
|
110 |
+
delta = chunk_data['choices'][0]['delta']
|
111 |
+
if 'content' in delta:
|
112 |
+
content = delta['content']
|
113 |
+
partial_words += content
|
114 |
+
print(f"İçerik eklendi: {content}")
|
115 |
+
print(f"Güncel partial_words: {partial_words}")
|
116 |
else:
|
117 |
+
print("Bu chunk içerik içermiyor (delta boş veya başka bilgi taşıyor)")
|
118 |
+
except json.JSONDecodeError as e:
|
119 |
+
print(f"JSON parse hatası: {e} - Chunk: {chunk_str}")
|
120 |
+
elif chunk_str == "data: [DONE]":
|
121 |
+
print("Akış tamamlandı: [DONE] alındı")
|
122 |
+
|
123 |
+
# Chatbot güncellemesi
|
124 |
+
if partial_words:
|
125 |
+
if token_counter == 0:
|
126 |
+
history.append(" " + partial_words)
|
127 |
+
else:
|
128 |
+
history[-1] = partial_words
|
129 |
+
chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)]
|
130 |
+
token_counter += 1
|
131 |
+
yield chat, history, chat_counter, response
|
132 |
+
|
133 |
+
# Bot yanıtını dosyaya yazma
|
134 |
+
if partial_words:
|
135 |
+
try:
|
136 |
+
with open(LOG_FILE, 'a', encoding='utf-8') as f:
|
137 |
+
f.write(f"Bot: {partial_words}\n")
|
138 |
+
print(f"Bot yanıtı dosyaya yazıldı: {partial_words}")
|
139 |
+
except Exception as e:
|
140 |
+
print(f"Dosya yazma hatası (Bot): {e}")
|
141 |
+
else:
|
142 |
+
print("Uyarı: partial_words boş, bot yanıtı yazılamadı!")
|
143 |
|
144 |
def save_chat(chatbot):
|
145 |
+
""" Sohbeti dosyaya kaydetme fonksiyonu """
|
146 |
file_path = os.path.abspath(LOG_FILE)
|
|
|
|
|
147 |
try:
|
148 |
with open(LOG_FILE, 'a', encoding='utf-8') as f:
|
149 |
f.write("\n--- Kayıt Edilen Sohbet ---\n")
|
|
|
159 |
def reset_textbox():
|
160 |
return gr.update(value='')
|
161 |
|
|
|
|
|
162 |
# Gradio arayüzü
|
163 |
demo_css = """
|
164 |
#send_button, #save_button {
|
|
|
227 |
save_button = gr.Button(value="Kayıt Et", elem_id="save_button")
|
228 |
|
229 |
state = gr.State([])
|
230 |
+
save_status = gr.Textbox(label="Kayıt Durumu", interactive=False)
|
231 |
|
232 |
with gr.Accordion("", open=False, visible=False):
|
233 |
top_p = gr.Slider(minimum=0, maximum=1.0, value=0.5, step=0.05, interactive=False, visible=False)
|
234 |
temperature = gr.Slider(minimum=0, maximum=5.0, value=0.1, step=0.1, interactive=False, visible=False)
|
235 |
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
236 |
|
|
|
237 |
inputs.submit(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
|
238 |
inputs.submit(reset_textbox, [], [inputs])
|
|
|
|
|
239 |
send_button.click(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
|
240 |
send_button.click(reset_textbox, [], [inputs])
|
|
|
|
|
241 |
save_button.click(save_chat, [chatbot], [save_status])
|
242 |
|
243 |
demo.queue(max_size=10).launch(debug=True)
|