SamiKoen commited on
Commit
6e8f2dc
·
verified ·
1 Parent(s): 1db74c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -7
app.py CHANGED
@@ -3,8 +3,14 @@ import os
3
  import json
4
  import requests
5
  import xml.etree.ElementTree as ET
 
 
 
6
  from huggingface_hub import HfApi, create_repo
7
 
 
 
 
8
  # Dosya yolu: Kalıcı depolama için öncelik, yoksa geçici dizin
9
  LOG_FILE = '/persistent-storage/chat_logs.txt'
10
  persistent_dir = '/persistent-storage'
@@ -50,6 +56,7 @@ if not hfapi:
50
  create_repo("BF", token=hfapi, repo_type="space", space_sdk="gradio", exist_ok=True)
51
 
52
  def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=None, history=None):
 
53
  if chatbot is None:
54
  chatbot = []
55
  if history is None:
@@ -144,8 +151,12 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=None,
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
 
151
  def save_chat(chatbot):
@@ -167,11 +178,6 @@ def reset_textbox():
167
  def upload_logs_to_hf(repo_id: str, hf_token: str, local_log_file: str = "chat_logs.txt"):
168
  """
169
  Log dosyasını Hugging Face Hub repository'sine yükler.
170
-
171
- Args:
172
- repo_id (str): Repository kimliği (örn. "SamiKoen/BF").
173
- hf_token (str): Hugging Face API token'ınız.
174
- local_log_file (str): Yüklenecek log dosyasının yolu.
175
  """
176
  api = HfApi(token=hf_token)
177
  try:
@@ -193,6 +199,28 @@ def save_chat_and_upload(chatbot):
193
  upload_logs_to_hf(HF_REPO_ID, hfapi)
194
  return save_status
195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
  # Gradio arayüzü
197
  demo_css = """
198
  #send_button, #save_button {
@@ -272,5 +300,4 @@ with gr.Blocks(css=demo_css, theme=theme) as demo:
272
  send_button.click(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
273
  send_button.click(reset_textbox, [], [inputs])
274
 
275
-
276
- demo.queue(max_size=10).launch(debug=True)
 
3
  import json
4
  import requests
5
  import xml.etree.ElementTree as ET
6
+ import schedule
7
+ import time
8
+ import threading
9
  from huggingface_hub import HfApi, create_repo
10
 
11
+ # Global değişken: sohbet geçmişi (otomatik kayıt için kullanılacak)
12
+ global_chat_history = []
13
+
14
  # Dosya yolu: Kalıcı depolama için öncelik, yoksa geçici dizin
15
  LOG_FILE = '/persistent-storage/chat_logs.txt'
16
  persistent_dir = '/persistent-storage'
 
56
  create_repo("BF", token=hfapi, repo_type="space", space_sdk="gradio", exist_ok=True)
57
 
58
  def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=None, history=None):
59
+ global global_chat_history
60
  if chatbot is None:
61
  chatbot = []
62
  if history is None:
 
151
  chat.append({"role": "assistant", "content": partial_words})
152
  elif partial_words and chat and chat[-1]["role"] == "assistant":
153
  chat[-1] = {"role": "assistant", "content": partial_words}
154
+ # Global sohbet geçmişini güncelle
155
+ global_chat_history = chatbot.copy()
156
  yield chat, history, chat_counter
157
  print(f"Son chatbot durumu: {chatbot}")
158
+ # Son durumda da güncelleme yap
159
+ global_chat_history = chatbot.copy()
160
  return chatbot, history, chat_counter
161
 
162
  def save_chat(chatbot):
 
178
  def upload_logs_to_hf(repo_id: str, hf_token: str, local_log_file: str = "chat_logs.txt"):
179
  """
180
  Log dosyasını Hugging Face Hub repository'sine yükler.
 
 
 
 
 
181
  """
182
  api = HfApi(token=hf_token)
183
  try:
 
199
  upload_logs_to_hf(HF_REPO_ID, hfapi)
200
  return save_status
201
 
202
+ # Otomatik kayıt fonksiyonu (schedule ile çağrılacak)
203
+ def scheduled_save():
204
+ global global_chat_history
205
+ if global_chat_history:
206
+ print("Otomatik kayıt tetiklendi.")
207
+ save_chat_and_upload(global_chat_history)
208
+
209
+ # Scheduler'ı arka planda çalıştıracak fonksiyon
210
+ def run_scheduler():
211
+ while True:
212
+ schedule.run_pending()
213
+ time.sleep(1)
214
+
215
+ # Belirli saatlerde otomatik kayıt (örn. 12:00, 18:00 ve 23:00)
216
+ schedule.every().day.at("12:00").do(scheduled_save)
217
+ schedule.every().day.at("18:00").do(scheduled_save)
218
+ schedule.every().day.at("23:00").do(scheduled_save)
219
+
220
+ # Scheduler'ı daemon thread ile başlatıyoruz
221
+ scheduler_thread = threading.Thread(target=run_scheduler, daemon=True)
222
+ scheduler_thread.start()
223
+
224
  # Gradio arayüzü
225
  demo_css = """
226
  #send_button, #save_button {
 
300
  send_button.click(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
301
  send_button.click(reset_textbox, [], [inputs])
302
 
303
+ demo.queue(max_size=10).launch(debug=True)