SamiKoen commited on
Commit
e15e9df
·
verified ·
1 Parent(s): 9cb89a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -24
app.py CHANGED
@@ -62,7 +62,7 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=None,
62
  print(f"System message: {system_msg}")
63
 
64
  multi_turn_message = [
65
- {"role": "system", "content": "Bir önceki sohbeti unut. Vereceğin ürün bilgisi, bu bilginin içinde yan yana yazmıyorsa veya arada başka bilgiler yazıyor ise, o bilgiyi vermeyeceksin çünkü o bilgi yanlıştır. ... (uzun metin)"}
66
  ]
67
 
68
  messages = multi_turn_message.copy()
@@ -109,42 +109,37 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=None,
109
  print(f"API hatası: {response.text}")
110
  return chatbot, history, chat_counter
111
 
112
- partial_words = ""
113
- counter = 0
114
  for chunk in response.iter_lines():
115
- counter += 1
116
  if not chunk:
117
  continue
118
  chunk_str = chunk.decode('utf-8')
119
- print(f"Chunk {counter}: {chunk_str}")
120
  if chunk_str.startswith("data: ") and chunk_str != "data: [DONE]":
121
  try:
122
  chunk_data = json.loads(chunk_str[6:])
123
  delta = chunk_data['choices'][0]['delta']
124
  if 'content' in delta and delta['content']:
125
  content = delta['content']
126
- partial_words += content
127
- print(f"İçerik eklendi: {content}")
128
- print(f"Güncel partial_words: {partial_words}")
129
  except json.JSONDecodeError as e:
130
  print(f"JSON parse hatası: {e} - Chunk: {chunk_str}")
131
  elif chunk_str == "data: [DONE]":
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")
139
- print(f"Bot yanıtı dosyaya yazıldı: {partial_words}")
140
- except Exception as e:
141
- print(f"Dosya yazma hatası (Bot): {e}")
142
- chat = chatbot.copy()
143
- if partial_words and chat and chat[-1]["role"] == "user":
144
- chat.append({"role": "assistant", "content": partial_words})
145
- elif partial_words and chat and chat[-1]["role"] == "assistant":
146
- chat[-1] = {"role": "assistant", "content": partial_words}
147
- yield chat, history, chat_counter
148
  print(f"Son chatbot durumu: {chatbot}")
149
  return chatbot, history, chat_counter
150
 
@@ -275,4 +270,4 @@ with gr.Blocks(css=demo_css, theme=theme) as demo:
275
  send_button.click(reset_textbox, [], [inputs])
276
  save_button.click(save_chat_and_upload, [chatbot], [save_status])
277
 
278
- demo.queue(max_size=10).launch(debug=True)
 
62
  print(f"System message: {system_msg}")
63
 
64
  multi_turn_message = [
65
+ {"role": "system", "content": "Bir önceki sohbeti unut. Vereceğin ürün bilgisi, bu bilginin içinde yan yana yazılmıyorsa veya arada başka bilgiler yazıyor ise, o bilgiyi vermeyeceksin çünkü o bilgi yanlıştır. ... (uzun metin)"}
66
  ]
67
 
68
  messages = multi_turn_message.copy()
 
109
  print(f"API hatası: {response.text}")
110
  return chatbot, history, chat_counter
111
 
112
+ assistant_response = ""
 
113
  for chunk in response.iter_lines():
 
114
  if not chunk:
115
  continue
116
  chunk_str = chunk.decode('utf-8')
117
+ print(f"Chunk: {chunk_str}")
118
  if chunk_str.startswith("data: ") and chunk_str != "data: [DONE]":
119
  try:
120
  chunk_data = json.loads(chunk_str[6:])
121
  delta = chunk_data['choices'][0]['delta']
122
  if 'content' in delta and delta['content']:
123
  content = delta['content']
124
+ assistant_response += content
125
+ # Parça parça güncelleme yerine, yalnızca akış sonunda sonuç üretiliyor.
 
126
  except json.JSONDecodeError as e:
127
  print(f"JSON parse hatası: {e} - Chunk: {chunk_str}")
128
  elif chunk_str == "data: [DONE]":
129
  print("Akış tamamlandı: [DONE] alındı")
130
+ break
131
+
132
+ if assistant_response:
133
+ history.append(assistant_response)
134
+ chatbot.append({"role": "assistant", "content": assistant_response})
135
+ try:
136
+ with open(LOG_FILE, 'a', encoding='utf-8') as f:
137
+ f.write(f"Bot: {assistant_response}\n")
138
+ print(f"Bot yanıtı dosyaya yazıldı: {assistant_response}")
139
+ except Exception as e:
140
+ print(f"Dosya yazma hatası (Bot): {e}")
141
+
142
+ yield chatbot, history, chat_counter
 
 
143
  print(f"Son chatbot durumu: {chatbot}")
144
  return chatbot, history, chat_counter
145
 
 
270
  send_button.click(reset_textbox, [], [inputs])
271
  save_button.click(save_chat_and_upload, [chatbot], [save_status])
272
 
273
+ demo.queue(max_size=10).launch(debug=True)