Update app.py
Browse files
app.py
CHANGED
@@ -9,14 +9,7 @@ import time
|
|
9 |
import threading
|
10 |
|
11 |
# Log dosyası adı ve yolu
|
12 |
-
LOG_FILE = 'chat_logs.txt'
|
13 |
-
persistent_dir = '/persistent-storage'
|
14 |
-
|
15 |
-
if os.path.exists(persistent_dir):
|
16 |
-
LOG_FILE = os.path.join(persistent_dir, LOG_FILE)
|
17 |
-
else:
|
18 |
-
LOG_FILE = 'chat_logs.txt'
|
19 |
-
|
20 |
print(f"Dosya yolu: {os.path.abspath(LOG_FILE)}")
|
21 |
|
22 |
# API ayarları
|
@@ -46,10 +39,8 @@ hfapi = os.getenv("hfapi")
|
|
46 |
if not hfapi:
|
47 |
raise ValueError("hfapi ortam değişkeni ayarlanmamış!")
|
48 |
|
49 |
-
# Repository oluşturma
|
50 |
create_repo("BF", token=hfapi, repo_type="space", space_sdk="gradio", exist_ok=True)
|
51 |
|
52 |
-
# Sohbeti yerel dosyaya kaydetme
|
53 |
def save_chat(chatbot):
|
54 |
file_path = os.path.abspath(LOG_FILE)
|
55 |
try:
|
@@ -63,7 +54,6 @@ def save_chat(chatbot):
|
|
63 |
print(f"Kayıt hatası: {e}")
|
64 |
return False
|
65 |
|
66 |
-
# Hugging Face’e log dosyasını yükleme
|
67 |
def upload_logs_to_hf(repo_id: str, hf_token: str, local_log_file: str = LOG_FILE):
|
68 |
api = HfApi(token=hf_token)
|
69 |
try:
|
@@ -80,25 +70,26 @@ def upload_logs_to_hf(repo_id: str, hf_token: str, local_log_file: str = LOG_FIL
|
|
80 |
print(f"HF yükleme hatası: {e}")
|
81 |
return False
|
82 |
|
83 |
-
# Zamanlayıcıyı arka planda çalıştırma
|
84 |
def run_scheduler(chatbot_ref):
|
85 |
def scheduled_save_and_upload():
|
86 |
if chatbot_ref:
|
|
|
87 |
save_success = save_chat(chatbot_ref)
|
88 |
if save_success:
|
89 |
-
HF_REPO_ID = "SamiKoen/BF"
|
90 |
upload_logs_to_hf(HF_REPO_ID, hfapi)
|
91 |
print(f"Zamanlanmış işlem tamamlandı: {time.strftime('%H:%M:%S')}")
|
92 |
|
93 |
-
schedule.every().day.at("
|
94 |
-
schedule.every().day.at("
|
95 |
schedule.every().day.at("21:00").do(scheduled_save_and_upload)
|
|
|
96 |
|
97 |
while True:
|
98 |
schedule.run_pending()
|
|
|
99 |
time.sleep(60)
|
100 |
|
101 |
-
# Chatbot tahmin fonksiyonu
|
102 |
def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=None, history=None):
|
103 |
if chatbot is None:
|
104 |
chatbot = []
|
@@ -184,11 +175,9 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=None,
|
|
184 |
|
185 |
return chatbot, history, chat_counter
|
186 |
|
187 |
-
# Textbox’ı sıfırlama
|
188 |
def reset_textbox():
|
189 |
return gr.update(value='')
|
190 |
|
191 |
-
# Gradio CSS
|
192 |
demo_css = """
|
193 |
#send_button {
|
194 |
background-color: #0b93f6;
|
@@ -233,7 +222,6 @@ theme = gr.themes.Base(
|
|
233 |
spacing_size="sm",
|
234 |
)
|
235 |
|
236 |
-
# Gradio arayüzü
|
237 |
with gr.Blocks(css=demo_css, theme=theme) as demo:
|
238 |
if not os.path.exists(LOG_FILE):
|
239 |
with open(LOG_FILE, 'w', encoding='utf-8') as f:
|
@@ -263,7 +251,6 @@ with gr.Blocks(css=demo_css, theme=theme) as demo:
|
|
263 |
temperature = gr.Slider(minimum=0, maximum=5.0, value=0.1, step=0.1, interactive=False, visible=False)
|
264 |
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
265 |
|
266 |
-
# Chatbot referansını güncelleme
|
267 |
chatbot_ref = []
|
268 |
def update_chatbot_ref(chat):
|
269 |
chatbot_ref[:] = chat
|
@@ -272,8 +259,7 @@ with gr.Blocks(css=demo_css, theme=theme) as demo:
|
|
272 |
inputs.submit(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter]).then(update_chatbot_ref, chatbot, chatbot).then(reset_textbox, [], [inputs])
|
273 |
send_button.click(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter]).then(update_chatbot_ref, chatbot, chatbot).then(reset_textbox, [], [inputs])
|
274 |
|
275 |
-
# Zamanlayıcıyı başlat
|
276 |
scheduler_thread = threading.Thread(target=run_scheduler, args=(chatbot_ref,), daemon=True)
|
277 |
scheduler_thread.start()
|
278 |
|
279 |
-
demo.
|
|
|
9 |
import threading
|
10 |
|
11 |
# Log dosyası adı ve yolu
|
12 |
+
LOG_FILE = '/data/chat_logs.txt' if os.path.exists('/data') else 'chat_logs.txt'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
print(f"Dosya yolu: {os.path.abspath(LOG_FILE)}")
|
14 |
|
15 |
# API ayarları
|
|
|
39 |
if not hfapi:
|
40 |
raise ValueError("hfapi ortam değişkeni ayarlanmamış!")
|
41 |
|
|
|
42 |
create_repo("BF", token=hfapi, repo_type="space", space_sdk="gradio", exist_ok=True)
|
43 |
|
|
|
44 |
def save_chat(chatbot):
|
45 |
file_path = os.path.abspath(LOG_FILE)
|
46 |
try:
|
|
|
54 |
print(f"Kayıt hatası: {e}")
|
55 |
return False
|
56 |
|
|
|
57 |
def upload_logs_to_hf(repo_id: str, hf_token: str, local_log_file: str = LOG_FILE):
|
58 |
api = HfApi(token=hf_token)
|
59 |
try:
|
|
|
70 |
print(f"HF yükleme hatası: {e}")
|
71 |
return False
|
72 |
|
|
|
73 |
def run_scheduler(chatbot_ref):
|
74 |
def scheduled_save_and_upload():
|
75 |
if chatbot_ref:
|
76 |
+
print(f"Zamanlayıcı tetiklendi: {time.strftime('%Y-%m-DD %H:%M:%S')}")
|
77 |
save_success = save_chat(chatbot_ref)
|
78 |
if save_success:
|
79 |
+
HF_REPO_ID = "SamiKoen/BF"
|
80 |
upload_logs_to_hf(HF_REPO_ID, hfapi)
|
81 |
print(f"Zamanlanmış işlem tamamlandı: {time.strftime('%H:%M:%S')}")
|
82 |
|
83 |
+
schedule.every().day.at("09:00").do(scheduled_save_and_upload)
|
84 |
+
schedule.every().day.at("15:00").do(scheduled_save_and_upload)
|
85 |
schedule.every().day.at("21:00").do(scheduled_save_and_upload)
|
86 |
+
print("Zamanlayıcı başlatıldı")
|
87 |
|
88 |
while True:
|
89 |
schedule.run_pending()
|
90 |
+
print(f"Zamanlayıcı çalışıyor, bekliyor: {time.strftime('%H:%M:%S')}")
|
91 |
time.sleep(60)
|
92 |
|
|
|
93 |
def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=None, history=None):
|
94 |
if chatbot is None:
|
95 |
chatbot = []
|
|
|
175 |
|
176 |
return chatbot, history, chat_counter
|
177 |
|
|
|
178 |
def reset_textbox():
|
179 |
return gr.update(value='')
|
180 |
|
|
|
181 |
demo_css = """
|
182 |
#send_button {
|
183 |
background-color: #0b93f6;
|
|
|
222 |
spacing_size="sm",
|
223 |
)
|
224 |
|
|
|
225 |
with gr.Blocks(css=demo_css, theme=theme) as demo:
|
226 |
if not os.path.exists(LOG_FILE):
|
227 |
with open(LOG_FILE, 'w', encoding='utf-8') as f:
|
|
|
251 |
temperature = gr.Slider(minimum=0, maximum=5.0, value=0.1, step=0.1, interactive=False, visible=False)
|
252 |
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
253 |
|
|
|
254 |
chatbot_ref = []
|
255 |
def update_chatbot_ref(chat):
|
256 |
chatbot_ref[:] = chat
|
|
|
259 |
inputs.submit(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter]).then(update_chatbot_ref, chatbot, chatbot).then(reset_textbox, [], [inputs])
|
260 |
send_button.click(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter]).then(update_chatbot_ref, chatbot, chatbot).then(reset_textbox, [], [inputs])
|
261 |
|
|
|
262 |
scheduler_thread = threading.Thread(target=run_scheduler, args=(chatbot_ref,), daemon=True)
|
263 |
scheduler_thread.start()
|
264 |
|
265 |
+
demo.launch(debug=True) # HF Spaces için share=True kaldırıldı
|