File size: 13,862 Bytes
c051efa 2142c80 f3ff1c7 2142c80 fce7101 f4fa7fb 46aa785 e182566 b4553e5 e182566 b4553e5 7fcabe0 7acd937 8b7994d 2142c80 97be484 f3ff1c7 10b378e 2343f3f 75d80d2 2343f3f 7a7b7b1 e182566 f3ff1c7 2142c80 f3ff1c7 97be484 2343f3f 1d85bf4 3432366 1d85bf4 66d3081 0b9cf76 0db0cec b07a923 2343f3f 7fcabe0 e781c31 9721f1e e182566 0b9cf76 3432366 dd6c899 81d729c 2343f3f e182566 97be484 81d729c 7fcabe0 97be484 3432366 b4553e5 0b9cf76 97be484 7fcabe0 97be484 43296bd d77b3f4 43296bd 5ede5a3 43296bd 5ede5a3 7fcabe0 5ede5a3 b4553e5 5ede5a3 d77b3f4 b4553e5 e182566 d77b3f4 5ede5a3 e182566 d77b3f4 3432366 e182566 7fcabe0 852d7e0 7fbd340 852d7e0 7fcabe0 60b2cc4 852d7e0 60b2cc4 852d7e0 f3ff1c7 7a7b7b1 bf43636 8df7630 aa19c35 bf43636 aa19c35 bf3ffe4 b7de124 bf3ffe4 8df7630 bf3ffe4 8df7630 97be484 0b9cf76 852d7e0 75d80d2 852d7e0 75d80d2 852d7e0 75d80d2 852d7e0 0b9cf76 852d7e0 75d80d2 bd1a474 0db0cec 75d80d2 97be484 0db0cec e8299c3 97be484 75d80d2 e8299c3 d544b01 97be484 75d80d2 8a96746 97be484 d544b01 0b9cf76 75d80d2 3432366 7baf15a 75d80d2 b4553e5 3432366 2343f3f 75d80d2 d77b3f4 0b9cf76 e8299c3 75d80d2 0db0cec 852d7e0 60b2cc4 75d80d2 e9d7e5b 5ede5a3 852d7e0 2343f3f 97be484 2343f3f 0b9cf76 97be484 0b9cf76 97be484 0b9cf76 f8b9c50 0b9cf76 bf43636 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
import gradio as gr
import os
import json
import requests
import xml.etree.ElementTree as ET
from huggingface_hub import HfApi, create_repo
create_repo("BF", token=HF_TOKEN, repo_type="space", space_sdk="gradio", exist_ok=True)
# Dosya yolu: Kalıcı depolama için öncelik, yoksa geçici dizin
LOG_FILE = '/persistent-storage/chat_logs.txt'
persistent_dir = '/persistent-storage'
if not os.path.exists(persistent_dir):
try:
os.makedirs(persistent_dir, exist_ok=True)
print(f"Kalıcı depolama dizini oluşturuldu: {persistent_dir}")
except Exception as e:
print(f"Kalıcı depolama dizini oluşturulamadı: {e}. Geçici dizine geri dönülüyor.")
LOG_FILE = 'chat_logs.txt'
print(f"Dosya yolu: {os.path.abspath(LOG_FILE)}")
# API ayarları
API_URL = "https://api.openai.com/v1/chat/completions"
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
if not OPENAI_API_KEY:
print("Hata: OPENAI_API_KEY çevre değişkeni ayarlanmamış!")
url = 'https://www.trekbisiklet.com.tr/output/8582384479'
response = requests.get(url)
root = ET.fromstring(response.content)
products = []
for item in root.findall('item'):
if item.find('isOptionOfAProduct').text == '1':
if item.find('stockAmount').text > '0':
name_words = item.find('rootlabel').text.lower().split()
name = name_words[0]
full_name = ' '.join(name_words)
stockAmount = "stokta"
price = item.find('priceWithTax').text
item_info = (stockAmount, price)
products.append((name, item_info, full_name))
def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=None, history=None):
if chatbot is None:
chatbot = []
if history is None:
history = []
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {OPENAI_API_KEY}"
}
print(f"System message: {system_msg}")
multi_turn_message = [
{"role": "system", "content": "Bir önceki sohbeti unut. Vereceğin ürün bilgisi, bu bilginin içinde yan yana yazmıyorsa veya arada başka bilgiler yazıyor ise, o bilgiyi vermeyeceksin çünkü o bilgi yanlıştır. vereceğin bilgiyi bu bilgilerin içinden alıyorsan her kelimenin yan yana yazmazı şartı ile o bilgiyi verebilirsin. Madone SLR bisikletler soruluyorsa (GEN 7) ibaresini kendin ekleyerek, aramayı GEN 7'li yap.Sana verilen bilgilerin içinde bir ürün adı veya bisiklet modelinin rengi yoksa, ürün ile ilgili bilgi vermeyeceksin ve sorulan modelden farklı boy ve renkler stoklarda varsa, bu bilgileri vereceksin. Alternatif renk veya boyu yok ise, başka bir model adını öğrenirsen stokları tekrar kontrol edebileceğini söyleyeceksin. Sana bir model adı rakamı ile verilmiş ve bu ürün bu bilgiler içinde yok ise, o ürün stoklarımızda yoktur diye bilgi vereceksin ve model adı rakamsız girilmiş ise nodel adının rakamı ile girilmesini rica edeceksin, örnek olarak 'Madone SL 7' gibi 7 rakamının da yazılmasını rica edeceksin. Madone, Emonda, Domane ve Checpont modelleri birer yol bisikleti modelidir, bu modellerin renklerinden önce yazan ve 47, 49, 50, 52, 54, 56, 58, 60, 62, 64 rakamları, o bisikletlerin boylarıdır. Bu bilgi içindeki renkler ise o ürünlerin renkleridir. Sana bir ürün var mı diye sorulduğunda, sadece bilgi içinde olan ürünleri söyleyebilirsin. Stoklarımızda yok ise o ürün ile ilgili bilgi vermeyeceksin. En büyük veya en küçük boy sorulduğunda, bilgi içinde renki bilgisi olan modellerin bilgisini vereceksin. Gerçek zamanlı stok bilgilerine erişme yeteneğin var. En aşağıdaki ürünlerin adına, rengine, boyuna ve fiyatına tam erişimin var ve bunları bilmiyorum demeyeceksin. Üyelere özel fiyatları ve indirimleri görmek için kullanıcıların siteye üye olmaları gerekmektedir. Sen bir AI Trek marka bisiklet uzmanı, bilir kişisi ve asistanısın.Trek ve Electra bisikletler konusunda uzmanım.İzmir'de yeni bir mağazamız açılıyor. Mağazamız Nisan 2025 ayında açılmış olacak. Yeri is Alsancak'da. İstanbul'da üç Trek mağazamız var: Caddebostan, Ortaköy ve Sarıyer. Ortaköy maidği 10.00-19.00 saatleri arasında açık. ve Toyota Plaza ve Carrefour'un yanindadir,tam adresi Dereboyu Cad No:84 Ortaköy Beşiktaş ve telefon numarası 0212 2271015. Caddebostan mağazası, Prof. Dr. Hulusi Behçet 18 Caddebostan, Kadıköy adresinde, Göztepe Parkı karşısındadır, telefon numarası 0216 6292432, 10.00-19.00 saatleri arasında açık. Tüm mağazalar Pazar günü kapalıdır. Caddebostan mağazamızda haftanın her günü Bike fit yapılmaktadır ve ücreti 3500 TL ve süresi 60-90 dakika. Bike fit yaptırmak isteyenler, Bike fit sayfamızda sağ tarafta bulunan RANDEVU AL butonu ile randevu oluştumaları gerekmektedir. Sarıyer mağazamızın adresi şöyledir: Mareşal Fevzi Çakmak Cad. No 54 Kemer-Bahçeköy Mahallsi Sarıyer, hafta içleri ve cumartesi günleri 10.00 ile 19.00 saatleri arasında hizmet vermektedir. Bu mağazamız elektrikli bisikletlerin daha çok sergilendiği ve tüm çeşiti bir arada görebileceğiniz mağazamızdır. Maslaktan, Belgrad ormanına gelirken sol tarafta kalmaktadır ve telefon numarası 0542 137 1080.."},
]
messages = multi_turn_message.copy()
input_words = [str(word).lower() for word in inputs.split()]
for product_info in products:
if product_info[0] in input_words:
new_msg = f"{product_info[2]} {product_info[1][0]} ve fiyatı EURO {product_info[1][1]}"
print(new_msg)
messages.append({"role": "system", "content": new_msg})
for data in chatbot:
if data["role"] == "user":
messages.append({"role": "user", "content": data["content"]})
elif data["role"] == "assistant":
messages.append({"role": "assistant", "content": data["content"]})
messages.append({"role": "user", "content": inputs})
payload = {
"model": "gpt-4o",
"messages": messages,
"temperature": 0.7,
"top_p": 0.9,
"n": 1,
"stream": True,
"presence_penalty": 0,
"frequency_penalty": 0,
}
chat_counter += 1
history.append(inputs)
print(f"Logging: Payload is - {payload}")
# Kullanıcı mesajını dosyaya yaz
try:
with open(LOG_FILE, 'a', encoding='utf-8') as f:
f.write(f"User: {inputs}\n")
print(f"Kullanıcı mesajı dosyaya yazıldı: {inputs}")
except Exception as e:
print(f"Dosya yazma hatası (Kullanıcı): {e}")
# Chatbot'a kullanıcı mesajını ekle
chatbot.append({"role": "user", "content": inputs})
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
print(f"Logging: Response code - {response.status_code}")
if response.status_code != 200:
print(f"API hatası: {response.text}")
return chatbot, history, chat_counter
partial_words = ""
counter = 0
for chunk in response.iter_lines():
counter += 1
if not chunk:
continue
chunk_str = chunk.decode('utf-8')
print(f"Chunk {counter}: {chunk_str}")
if chunk_str.startswith("data: ") and chunk_str != "data: [DONE]":
try:
chunk_data = json.loads(chunk_str[6:])
delta = chunk_data['choices'][0]['delta']
if 'content' in delta and delta['content']:
content = delta['content']
partial_words += content
print(f"İçerik eklendi: {content}")
print(f"Güncel partial_words: {partial_words}")
except json.JSONDecodeError as e:
print(f"JSON parse hatası: {e} - Chunk: {chunk_str}")
elif chunk_str == "data: [DONE]":
print("Akış tamamlandı: [DONE] alındı")
if partial_words:
history.append(partial_words)
chatbot.append({"role": "assistant", "content": partial_words})
try:
with open(LOG_FILE, 'a', encoding='utf-8') as f:
f.write(f"Bot: {partial_words}\n")
print(f"Bot yanıtı dosyaya yazıldı: {partial_words}")
except Exception as e:
print(f"Dosya yazma hatası (Bot): {e}")
# Chatbot içeriğini güncelle ve yield ile gönder
chat = chatbot.copy()
if partial_words and chat and chat[-1]["role"] == "user":
chat.append({"role": "assistant", "content": partial_words})
elif partial_words and chat and chat[-1]["role"] == "assistant":
chat[-1] = {"role": "assistant", "content": partial_words}
yield chat, history, chat_counter
print(f"Son chatbot durumu: {chatbot}")
return chatbot, history, chat_counter
def save_chat(chatbot):
file_path = os.path.abspath(LOG_FILE)
try:
with open(LOG_FILE, 'a', encoding='utf-8') as f:
f.write("\n--- Kayıt Edilen Sohbet ---\n")
for msg in chatbot:
f.write(f"{msg['role'].capitalize()}: {msg['content']}\n")
print(f"Sohbet dosyaya kaydedildi: {file_path}")
return f"Sohbet başarıyla kaydedildi!\nDosya: {file_path}"
except Exception as e:
print(f"Kayıt hatası: {e}")
return f"Kayıt hatası: {e}\nDosya: {file_path}"
def reset_textbox():
return gr.update(value='')
from huggingface_hub import HfApi, create_repo
# Repository oluşturma (eğer mevcut değilse)
HF_TOKEN = os.getenv("HF_TOKEN")
create_repo("BF", token=HF_TOKEN, repo_type="space", exist_ok=True)
def upload_logs_to_hf(repo_id: str, hf_token: str, local_log_file: str = "chat_logs.txt"):
"""
Log dosyasını Hugging Face Hub repository'sine yükler.
Args:
repo_id (str): Repository kimliği (örn. "SamiKoen/BF").
hf_token (str): Hugging Face API token'ınız.
local_log_file (str): Yüklenecek log dosyasının yolu.
"""
api = HfApi(token=hf_token)
try:
api.upload_file(
path_or_fileobj=local_log_file,
path_in_repo=local_log_file,
repo_id=repo_id,
repo_type="space",
commit_message="Log dosyası güncellendi"
)
print(f"Log dosyası başarıyla yüklendi: {local_log_file}")
except Exception as e:
print(f"Log dosyası yüklenirken hata oluştu: {e}")
def save_chat_and_upload(chatbot):
# Sohbeti kaydetme işlemini burada yapın
save_status = save_chat(chatbot)
# Log dosyasını Hugging Face Hub'a yükleyin
HF_REPO_ID = "SamiKoen/BF" # Kendi repo kimliğinizi girin.
HF_TOKEN = os.getenv("HF_TOKEN") # Token'ı ortam değişkeni olarak alabilirsiniz.
upload_logs_to_hf(HF_REPO_ID, HF_TOKEN)
return save_status
# Gradio arayüzü
demo_css = """
#send_button, #save_button {
background-color: #0b93f6;
border: none;
color: white;
font-size: 16px;
border-radius: 10%;
width: 100px !important;
height: 37px !important;
display: inline-flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: background-color 0.3s;
margin: 5px;
}
#send_button:hover, #save_button:hover {
background-color: #0077c0;
}
.fixed_button_container {
padding: 0px;
margin: 0px 0 0 0px;
}
#custom_row {
width: 150% !important;
flex-wrap: nowrap !important;
}
@media only screen and (max-width: 1000px) {
.custom_row {
flex-wrap: nowrap !important;
}
}
#chatbot {
height: 100vh;
overflow-y: auto;
}
"""
theme = gr.themes.Base(
neutral_hue="blue",
text_size="sm",
spacing_size="sm",
)
with gr.Blocks(css=demo_css, theme=theme) as demo:
if not os.path.exists(LOG_FILE):
with open(LOG_FILE, 'w', encoding='utf-8') as f:
f.write("--- Yeni Sohbet ---\n")
with gr.Column(elem_id="col_container"):
with gr.Accordion("", open=False, visible=False):
system_msg = gr.Textbox(value="")
new_msg = gr.Textbox(value="")
accordion_msg = gr.HTML(value="", visible=False)
chatbot = gr.Chatbot(label='Trek Asistanı', elem_id="chatbot", type="messages")
with gr.Row(elem_id="custom_row"):
inputs = gr.Textbox(
placeholder="Buraya yazın",
show_label=False,
container=False,
)
with gr.Column(elem_classes="fixed_button_container"):
send_button = gr.Button(value="Gönder", elem_id="send_button")
save_button = gr.Button(value="Kayıt Et", elem_id="save_button")
state = gr.State([])
save_status = gr.Textbox(label="Kayıt Durumu", interactive=False)
with gr.Accordion("", open=False, visible=False):
top_p = gr.Slider(minimum=0, maximum=1.0, value=0.5, step=0.05, interactive=False, visible=False)
temperature = gr.Slider(minimum=0, maximum=5.0, value=0.1, step=0.1, interactive=False, visible=False)
chat_counter = gr.Number(value=0, visible=False, precision=0)
inputs.submit(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
inputs.submit(reset_textbox, [], [inputs])
send_button.click(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
send_button.click(reset_textbox, [], [inputs])
save_button.click(save_chat_and_upload, [chatbot], [save_status])
demo.queue(max_size=10).launch(debug=True)
|