Update app.py
Browse files
app.py
CHANGED
@@ -45,14 +45,54 @@ for item in root.findall('item'):
|
|
45 |
hfapi = os.getenv("hfapi")
|
46 |
if not hfapi:
|
47 |
raise ValueError("hfapi ortam değişkeni ayarlanmamış!")
|
48 |
-
#
|
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
|
54 |
-
|
55 |
-
ve sonrasında HF Hub'a yüklenir.
|
56 |
"""
|
57 |
if chatbot is None:
|
58 |
chatbot = []
|
@@ -66,7 +106,7 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=None,
|
|
66 |
print(f"System message: {system_msg}")
|
67 |
|
68 |
multi_turn_message = [
|
69 |
-
{"role": "system", "content": "Bir önceki sohbeti unut. Vereceğin ürün bilgisi
|
70 |
]
|
71 |
|
72 |
messages = multi_turn_message.copy()
|
@@ -150,51 +190,13 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=None,
|
|
150 |
yield chat, history, chat_counter
|
151 |
|
152 |
print(f"Son chatbot durumu: {chatbot}")
|
153 |
-
#
|
154 |
-
upload_logs_to_hf("
|
155 |
return chatbot, history, chat_counter
|
156 |
|
157 |
-
|
158 |
-
file_path = os.path.abspath(LOG_FILE)
|
159 |
-
try:
|
160 |
-
with open(LOG_FILE, 'a', encoding='utf-8') as f:
|
161 |
-
f.write("\n--- Kayıt Edilen Sohbet ---\n")
|
162 |
-
for msg in chatbot:
|
163 |
-
f.write(f"{msg['role'].capitalize()}: {msg['content']}\n")
|
164 |
-
print(f"Sohbet dosyaya kaydedildi: {file_path}")
|
165 |
-
return f"Sohbet başarıyla kaydedildi!\nDosya: {file_path}"
|
166 |
-
except Exception as e:
|
167 |
-
print(f"Kayıt hatası: {e}")
|
168 |
-
return f"Kayıt hatası: {e}\nDosya: {file_path}"
|
169 |
-
|
170 |
-
def reset_textbox():
|
171 |
-
return gr.update(value='')
|
172 |
-
|
173 |
-
def upload_logs_to_hf(repo_id: str, hf_token: str, local_log_file: str = "chat_logs.txt"):
|
174 |
-
"""
|
175 |
-
Log dosyasını Hugging Face Hub repository'sine yükler.
|
176 |
-
|
177 |
-
Args:
|
178 |
-
repo_id (str): Repository kimliği (örn. "SamiKoen/BF").
|
179 |
-
hf_token (str): Hugging Face API token'ınız.
|
180 |
-
local_log_file (str): Yüklenecek log dosyasının yolu.
|
181 |
-
"""
|
182 |
-
api = HfApi(token=hf_token)
|
183 |
-
try:
|
184 |
-
api.upload_file(
|
185 |
-
path_or_fileobj=local_log_file,
|
186 |
-
path_in_repo=local_log_file,
|
187 |
-
repo_id=repo_id,
|
188 |
-
repo_type="space",
|
189 |
-
commit_message="Log dosyası güncellendi"
|
190 |
-
)
|
191 |
-
print(f"Log dosyası başarıyla yüklendi: {local_log_file}")
|
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
|
198 |
background-color: #0b93f6;
|
199 |
border: none;
|
200 |
color: white;
|
@@ -209,9 +211,6 @@ demo_css = """
|
|
209 |
transition: background-color 0.3s;
|
210 |
margin: 5px;
|
211 |
}
|
212 |
-
#send_button:hover, #save_button:hover {
|
213 |
-
background-color: #0077c0;
|
214 |
-
}
|
215 |
.fixed_button_container {
|
216 |
padding: 0px;
|
217 |
margin: 0px 0 0 0px;
|
@@ -256,8 +255,8 @@ with gr.Blocks(css=demo_css, theme=theme) as demo:
|
|
256 |
show_label=False,
|
257 |
container=False,
|
258 |
)
|
259 |
-
|
260 |
-
|
261 |
|
262 |
state = gr.State([])
|
263 |
|
@@ -266,8 +265,10 @@ with gr.Blocks(css=demo_css, theme=theme) as demo:
|
|
266 |
temperature = gr.Slider(minimum=0, maximum=5.0, value=0.1, step=0.1, interactive=False, visible=False)
|
267 |
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
268 |
|
|
|
269 |
inputs.submit(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
|
270 |
inputs.submit(reset_textbox, [], [inputs])
|
271 |
send_button.click(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
|
|
|
272 |
|
273 |
demo.queue(max_size=10).launch(debug=True)
|
|
|
45 |
hfapi = os.getenv("hfapi")
|
46 |
if not hfapi:
|
47 |
raise ValueError("hfapi ortam değişkeni ayarlanmamış!")
|
48 |
+
# Kod deposu (Space) için repo oluşturuluyor.
|
49 |
create_repo("BF", token=hfapi, repo_type="space", space_sdk="gradio", exist_ok=True)
|
50 |
+
# Loglar için ayrı bir repo (commitler log dosyasını güncelleyecek ancak bu, Space’i yeniden başlatmayacak)
|
51 |
+
create_repo("BF-logs", token=hfapi, repo_type="model", exist_ok=True)
|
52 |
+
|
53 |
+
def upload_logs_to_hf(repo_id: str, hf_token: str, local_log_file: str = "chat_logs.txt", repo_type: str = "model"):
|
54 |
+
"""
|
55 |
+
Log dosyasını Hugging Face Hub üzerindeki ayrı bir repository'ye yükler.
|
56 |
+
|
57 |
+
Args:
|
58 |
+
repo_id (str): Log repository kimliği (örn. "BF-logs" -> Hub'da "KullaniciAdiniz/BF-logs").
|
59 |
+
hf_token (str): Hugging Face API token'ınız.
|
60 |
+
local_log_file (str): Yüklenecek log dosyasının yolu.
|
61 |
+
repo_type (str): Repository tipi ("model" kullanıyoruz).
|
62 |
+
"""
|
63 |
+
api = HfApi(token=hf_token)
|
64 |
+
try:
|
65 |
+
api.upload_file(
|
66 |
+
path_or_fileobj=local_log_file,
|
67 |
+
path_in_repo=local_log_file,
|
68 |
+
repo_id=repo_id,
|
69 |
+
repo_type=repo_type,
|
70 |
+
commit_message="Log dosyası güncellendi"
|
71 |
+
)
|
72 |
+
print(f"Log dosyası başarıyla yüklendi: {local_log_file}")
|
73 |
+
except Exception as e:
|
74 |
+
print(f"Log dosyası yüklenirken hata oluştu: {e}")
|
75 |
+
|
76 |
+
def save_chat(chatbot):
|
77 |
+
file_path = os.path.abspath(LOG_FILE)
|
78 |
+
try:
|
79 |
+
with open(LOG_FILE, 'a', encoding='utf-8') as f:
|
80 |
+
f.write("\n--- Kayıt Edilen Sohbet ---\n")
|
81 |
+
for msg in chatbot:
|
82 |
+
f.write(f"{msg['role'].capitalize()}: {msg['content']}\n")
|
83 |
+
print(f"Sohbet dosyaya kaydedildi: {file_path}")
|
84 |
+
return f"Sohbet başarıyla kaydedildi!\nDosya: {file_path}"
|
85 |
+
except Exception as e:
|
86 |
+
print(f"Kayıt hatası: {e}")
|
87 |
+
return f"Kayıt hatası: {e}\nDosya: {file_path}"
|
88 |
+
|
89 |
+
def reset_textbox():
|
90 |
+
return gr.update(value='')
|
91 |
|
92 |
def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=None, history=None):
|
93 |
"""
|
94 |
+
Her Enter'a basıldığında çalışır. Kullanıcı mesajı log dosyasına yazılır,
|
95 |
+
bot yanıtı tamamlandığında log dosyasına eklenir ve ardından loglar HF Hub'daki log repo'suna yüklenir.
|
|
|
96 |
"""
|
97 |
if chatbot is None:
|
98 |
chatbot = []
|
|
|
106 |
print(f"System message: {system_msg}")
|
107 |
|
108 |
multi_turn_message = [
|
109 |
+
{"role": "system", "content": "Bir önceki sohbeti unut. Vereceğin ürün bilgisi ... (uzun metin)"}
|
110 |
]
|
111 |
|
112 |
messages = multi_turn_message.copy()
|
|
|
190 |
yield chat, history, chat_counter
|
191 |
|
192 |
print(f"Son chatbot durumu: {chatbot}")
|
193 |
+
# Tüm streaming tamamlandıktan sonra log dosyası ayrı log reposuna yüklenir.
|
194 |
+
upload_logs_to_hf("BF-logs", hfapi, local_log_file=LOG_FILE, repo_type="model")
|
195 |
return chatbot, history, chat_counter
|
196 |
|
197 |
+
# Gradio arayüzü CSS ve tema ayarları
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
demo_css = """
|
199 |
+
#send_button {
|
200 |
background-color: #0b93f6;
|
201 |
border: none;
|
202 |
color: white;
|
|
|
211 |
transition: background-color 0.3s;
|
212 |
margin: 5px;
|
213 |
}
|
|
|
|
|
|
|
214 |
.fixed_button_container {
|
215 |
padding: 0px;
|
216 |
margin: 0px 0 0 0px;
|
|
|
255 |
show_label=False,
|
256 |
container=False,
|
257 |
)
|
258 |
+
# Yalnızca "Gönder" butonu bulunuyor.
|
259 |
+
send_button = gr.Button(value="Gönder", elem_id="send_button")
|
260 |
|
261 |
state = gr.State([])
|
262 |
|
|
|
265 |
temperature = gr.Slider(minimum=0, maximum=5.0, value=0.1, step=0.1, interactive=False, visible=False)
|
266 |
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
267 |
|
268 |
+
# Hem Enter hem de Gönder butonuna basıldığında predict çalışıyor.
|
269 |
inputs.submit(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
|
270 |
inputs.submit(reset_textbox, [], [inputs])
|
271 |
send_button.click(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
|
272 |
+
send_button.click(reset_textbox, [], [inputs])
|
273 |
|
274 |
demo.queue(max_size=10).launch(debug=True)
|