Update app.py
Browse files
app.py
CHANGED
@@ -41,13 +41,19 @@ for item in root.findall('item'):
|
|
41 |
item_info = (stockAmount, price)
|
42 |
products.append((name, item_info, full_name))
|
43 |
|
44 |
-
#
|
45 |
hfapi = os.getenv("hfapi")
|
46 |
if not hfapi:
|
47 |
raise ValueError("hfapi ortam değişkeni ayarlanmamış!")
|
|
|
48 |
create_repo("BF", token=hfapi, repo_type="space", space_sdk="gradio", exist_ok=True)
|
49 |
|
50 |
def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=None, history=None):
|
|
|
|
|
|
|
|
|
|
|
51 |
if chatbot is None:
|
52 |
chatbot = []
|
53 |
if history is None:
|
@@ -91,7 +97,7 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=None,
|
|
91 |
history.append(inputs)
|
92 |
print(f"Logging: Payload is - {payload}")
|
93 |
|
94 |
-
# Kullanıcı mesajını
|
95 |
try:
|
96 |
with open(LOG_FILE, 'a', encoding='utf-8') as f:
|
97 |
f.write(f"User: {inputs}\n")
|
@@ -142,7 +148,10 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=None,
|
|
142 |
elif partial_words and chat and chat[-1]["role"] == "assistant":
|
143 |
chat[-1] = {"role": "assistant", "content": partial_words}
|
144 |
yield chat, history, chat_counter
|
|
|
145 |
print(f"Son chatbot durumu: {chatbot}")
|
|
|
|
|
146 |
return chatbot, history, chat_counter
|
147 |
|
148 |
def save_chat(chatbot):
|
@@ -183,14 +192,6 @@ def upload_logs_to_hf(repo_id: str, hf_token: str, local_log_file: str = "chat_l
|
|
183 |
except Exception as e:
|
184 |
print(f"Log dosyası yüklenirken hata oluştu: {e}")
|
185 |
|
186 |
-
# Kayıt Et butonunu kaldırıyoruz. Her Enter tuşuna basıldığında (predict fonksiyonu çalıştığında) otomatik log kaydı yapılıyor.
|
187 |
-
def save_chat_and_upload(chatbot):
|
188 |
-
save_status = save_chat(chatbot)
|
189 |
-
HF_REPO_ID = "SamiKoen/BF" # Kendi repo kimliğinizi girin.
|
190 |
-
hfapi = os.getenv("hfapi")
|
191 |
-
upload_logs_to_hf(HF_REPO_ID, hfapi)
|
192 |
-
return save_status
|
193 |
-
|
194 |
# Gradio arayüzü
|
195 |
demo_css = """
|
196 |
#send_button {
|
@@ -252,7 +253,7 @@ with gr.Blocks(css=demo_css, theme=theme) as demo:
|
|
252 |
show_label=False,
|
253 |
container=False,
|
254 |
)
|
255 |
-
#
|
256 |
send_button = gr.Button(value="Gönder", elem_id="send_button")
|
257 |
|
258 |
state = gr.State([])
|
@@ -262,24 +263,10 @@ with gr.Blocks(css=demo_css, theme=theme) as demo:
|
|
262 |
temperature = gr.Slider(minimum=0, maximum=5.0, value=0.1, step=0.1, interactive=False, visible=False)
|
263 |
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
264 |
|
265 |
-
#
|
266 |
inputs.submit(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
|
267 |
inputs.submit(reset_textbox, [], [inputs])
|
268 |
send_button.click(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
|
269 |
send_button.click(reset_textbox, [], [inputs])
|
270 |
-
|
271 |
-
# Her mesaj gönderildikten sonra log dosyası otomatik olarak Hugging Face Hub'a yüklensin.
|
272 |
-
# Bunun için predict fonksiyonunun sonunda save_chat_and_upload çağrısını da ekleyebiliriz.
|
273 |
-
# Aşağıdaki örnekte predict fonksiyonunun akışı tamamlandıktan sonra (yield'den sonra)
|
274 |
-
# save_chat_and_upload çağrısını manuel olarak tetikleyebilirsiniz.
|
275 |
-
#
|
276 |
-
# Örneğin, predict fonksiyonunun döndüğü son değer üzerinden:
|
277 |
-
# chatbot, state, chat_counter = predict(...), ardından save_chat_and_upload(chatbot)
|
278 |
-
#
|
279 |
-
# Eğer bu entegrasyonu otomatik hale getirmek isterseniz, predict fonksiyonunun
|
280 |
-
# sonunda log dosyasını yükleyecek kodu ekleyebilirsiniz.
|
281 |
-
#
|
282 |
-
# Burada, kullanıcı mesajının dosyaya yazılması ve bot yanıtı tamamlandığında
|
283 |
-
# otomatik olarak log dosyasına eklenmesi sağlanmış durumda.
|
284 |
-
|
285 |
demo.queue(max_size=10).launch(debug=True)
|
|
|
41 |
item_info = (stockAmount, price)
|
42 |
products.append((name, item_info, full_name))
|
43 |
|
44 |
+
# Ortam değişkeninde "hfapi" tanımlı; repo oluşturuluyor.
|
45 |
hfapi = os.getenv("hfapi")
|
46 |
if not hfapi:
|
47 |
raise ValueError("hfapi ortam değişkeni ayarlanmamış!")
|
48 |
+
# Repo adı örneğin "BF" ise; kullanıcı adınız repo id'sinde eklenir (örneğin "SamiKoen/BF")
|
49 |
create_repo("BF", token=hfapi, repo_type="space", space_sdk="gradio", exist_ok=True)
|
50 |
|
51 |
def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=None, history=None):
|
52 |
+
"""
|
53 |
+
Her Enter tuşuna basıldığında çalışır;
|
54 |
+
kullanıcı mesajı log dosyasına yazılır, bot yanıtı tamamlandığında log dosyası güncellenir
|
55 |
+
ve sonrasında HF Hub'a yüklenir.
|
56 |
+
"""
|
57 |
if chatbot is None:
|
58 |
chatbot = []
|
59 |
if history is None:
|
|
|
97 |
history.append(inputs)
|
98 |
print(f"Logging: Payload is - {payload}")
|
99 |
|
100 |
+
# Kullanıcı mesajını log dosyasına yaz
|
101 |
try:
|
102 |
with open(LOG_FILE, 'a', encoding='utf-8') as f:
|
103 |
f.write(f"User: {inputs}\n")
|
|
|
148 |
elif partial_words and chat and chat[-1]["role"] == "assistant":
|
149 |
chat[-1] = {"role": "assistant", "content": partial_words}
|
150 |
yield chat, history, chat_counter
|
151 |
+
|
152 |
print(f"Son chatbot durumu: {chatbot}")
|
153 |
+
# Her enter'dan sonra log dosyası otomatik olarak HF Hub'a yüklensin:
|
154 |
+
upload_logs_to_hf("SamiKoen/BF", hfapi)
|
155 |
return chatbot, history, chat_counter
|
156 |
|
157 |
def save_chat(chatbot):
|
|
|
192 |
except Exception as e:
|
193 |
print(f"Log dosyası yüklenirken hata oluştu: {e}")
|
194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
# Gradio arayüzü
|
196 |
demo_css = """
|
197 |
#send_button {
|
|
|
253 |
show_label=False,
|
254 |
container=False,
|
255 |
)
|
256 |
+
# Sadece "Gönder" butonu bulunuyor.
|
257 |
send_button = gr.Button(value="Gönder", elem_id="send_button")
|
258 |
|
259 |
state = gr.State([])
|
|
|
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 |
+
# Hem Enter hem de Gönder butonuna basıldığında predict çalışıyor.
|
267 |
inputs.submit(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
|
268 |
inputs.submit(reset_textbox, [], [inputs])
|
269 |
send_button.click(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
|
270 |
send_button.click(reset_textbox, [], [inputs])
|
271 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
272 |
demo.queue(max_size=10).launch(debug=True)
|