Update app.py
Browse files
app.py
CHANGED
@@ -41,12 +41,10 @@ for item in root.findall('item'):
|
|
41 |
item_info = (stockAmount, price)
|
42 |
products.append((name, item_info, full_name))
|
43 |
|
44 |
-
# HF_TOKEN yerine "hfapi" ortam değişkenini alıyoruz
|
45 |
hfapi = os.getenv("hfapi")
|
46 |
if not hfapi:
|
47 |
raise ValueError("hfapi ortam değişkeni ayarlanmamış!")
|
48 |
-
|
49 |
-
# Repository oluşturma (repo adı "BF" kullanıcı adınızla uyumlu olmalı; space_sdk eklenmiş)
|
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):
|
@@ -101,7 +99,6 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=None,
|
|
101 |
except Exception as e:
|
102 |
print(f"Dosya yazma hatası (Kullanıcı): {e}")
|
103 |
|
104 |
-
# Chatbot'a kullanıcı mesajını ekle
|
105 |
chatbot.append({"role": "user", "content": inputs})
|
106 |
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
107 |
print(f"Logging: Response code - {response.status_code}")
|
@@ -186,6 +183,7 @@ def upload_logs_to_hf(repo_id: str, hf_token: str, local_log_file: str = "chat_l
|
|
186 |
except Exception as e:
|
187 |
print(f"Log dosyası yüklenirken hata oluştu: {e}")
|
188 |
|
|
|
189 |
def save_chat_and_upload(chatbot):
|
190 |
save_status = save_chat(chatbot)
|
191 |
HF_REPO_ID = "SamiKoen/BF" # Kendi repo kimliğinizi girin.
|
@@ -195,7 +193,7 @@ def save_chat_and_upload(chatbot):
|
|
195 |
|
196 |
# Gradio arayüzü
|
197 |
demo_css = """
|
198 |
-
#send_button
|
199 |
background-color: #0b93f6;
|
200 |
border: none;
|
201 |
color: white;
|
@@ -210,9 +208,6 @@ demo_css = """
|
|
210 |
transition: background-color 0.3s;
|
211 |
margin: 5px;
|
212 |
}
|
213 |
-
#send_button:hover, #save_button:hover {
|
214 |
-
background-color: #0077c0;
|
215 |
-
}
|
216 |
.fixed_button_container {
|
217 |
padding: 0px;
|
218 |
margin: 0px 0 0 0px;
|
@@ -257,22 +252,34 @@ with gr.Blocks(css=demo_css, theme=theme) as demo:
|
|
257 |
show_label=False,
|
258 |
container=False,
|
259 |
)
|
260 |
-
|
261 |
-
|
262 |
-
save_button = gr.Button(value="Kayıt Et", elem_id="save_button")
|
263 |
|
264 |
state = gr.State([])
|
265 |
-
save_status = gr.Textbox(label="Kayıt Durumu", interactive=False)
|
266 |
|
267 |
with gr.Accordion("", open=False, visible=False):
|
268 |
top_p = gr.Slider(minimum=0, maximum=1.0, value=0.5, step=0.05, interactive=False, visible=False)
|
269 |
temperature = gr.Slider(minimum=0, maximum=5.0, value=0.1, step=0.1, interactive=False, visible=False)
|
270 |
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
271 |
|
|
|
272 |
inputs.submit(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
|
273 |
inputs.submit(reset_textbox, [], [inputs])
|
274 |
send_button.click(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
|
275 |
send_button.click(reset_textbox, [], [inputs])
|
276 |
-
|
277 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
278 |
demo.queue(max_size=10).launch(debug=True)
|
|
|
41 |
item_info = (stockAmount, price)
|
42 |
products.append((name, item_info, full_name))
|
43 |
|
44 |
+
# HF_TOKEN yerine "hfapi" ortam değişkenini alıyoruz ve repo oluşturuyoruz
|
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):
|
|
|
99 |
except Exception as e:
|
100 |
print(f"Dosya yazma hatası (Kullanıcı): {e}")
|
101 |
|
|
|
102 |
chatbot.append({"role": "user", "content": inputs})
|
103 |
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
104 |
print(f"Logging: Response code - {response.status_code}")
|
|
|
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.
|
|
|
193 |
|
194 |
# Gradio arayüzü
|
195 |
demo_css = """
|
196 |
+
#send_button {
|
197 |
background-color: #0b93f6;
|
198 |
border: none;
|
199 |
color: white;
|
|
|
208 |
transition: background-color 0.3s;
|
209 |
margin: 5px;
|
210 |
}
|
|
|
|
|
|
|
211 |
.fixed_button_container {
|
212 |
padding: 0px;
|
213 |
margin: 0px 0 0 0px;
|
|
|
252 |
show_label=False,
|
253 |
container=False,
|
254 |
)
|
255 |
+
# Kayıt Et butonu kaldırıldı, sadece gönder butonu kullanılıyor.
|
256 |
+
send_button = gr.Button(value="Gönder", elem_id="send_button")
|
|
|
257 |
|
258 |
state = gr.State([])
|
|
|
259 |
|
260 |
with gr.Accordion("", open=False, visible=False):
|
261 |
top_p = gr.Slider(minimum=0, maximum=1.0, value=0.5, step=0.05, interactive=False, visible=False)
|
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 |
+
# Her Enter (veya Gönder butonuna tıklandığında) predict çalışacak
|
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)
|