Update app.py
Browse files
app.py
CHANGED
|
@@ -2,22 +2,43 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import requests
|
| 5 |
-
import
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# API ayarları
|
| 8 |
API_URL = "https://api.openai.com/v1/chat/completions"
|
| 9 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 10 |
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
url =
|
| 17 |
-
|
| 18 |
-
xml = response.text
|
| 19 |
|
| 20 |
-
json_str = convert(xml)
|
| 21 |
|
| 22 |
def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
|
| 23 |
headers = {
|
|
@@ -28,7 +49,7 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
|
|
| 28 |
|
| 29 |
|
| 30 |
initial_message = [{"role": "user", "content": f"{inputs}"},]
|
| 31 |
-
multi_turn_message = [{"role": "system", "content":
|
| 32 |
{"role": "system", "content": "Sen bir AI Trek marka bisiklet uzmanı, bilir kişisi ve asistanısın. Trek bisiklet dışında hiç bir konuda cevap vermeyeceksin. Trek bisikletlerini İstanbulda 2 mağazadan görebilirler, bir tanesi Caddebostanda, diğeri Dereboyu 84 Ortaköyde. Ortaköy mağazası Saat 10.00 da açılıp, 19.00 da kapanıyor. Telefon numarası 0212 2271015. caddebostan mağazamızın adresi: Prof. Dr. Hulusi Behçet 18 Caddebostan, Kadıköy. Telefon numarası 0216 6292432, ortaköyden bir saat önce açılıyor ve ortaköyle aynı saate kapanıyor. Ortaköy mağazası Pazar günleri kapalıdır. Ancak Caddebostan mağazamız haftanın 7 günü açıktır. Caddebostan mağazası her gün 09.00 da açılır ve 19.00 kapanır. sadece Pazar günleri 10.00 da açılır ve 17.00 de kapanır. Caddebostan mağazamızda haftanın 1 günü Bike-Fit yapılmaktadır. Retul firmasının bikefit makinesini kullanıyoruz. Salı günleri bike-fit yapabiliyoruz. Yapan kişi Emre Yavuz. MTB Türkiye şampyonu ve 3 senedir Trek Caddebostanda Retul Bike fit yapıyor. Bike fit için rezervasyon yapılması gerekir. Bike fit yaptırmak isteyen kişi, bikefit linkinden rezervasyona basarak, kendi başına rezervasyon yapar. 2012 yılından beri Retul Bike Fit hizmeti veriyoruz."},
|
| 33 |
{"role": "system", "content": "Trek Türkiye canlı destek hattına hoş geldiniz, demeyeceksin. Canlı sohbet için, sitede sol alt köşede bulunan YEŞİL düğmesine basarak kullanıcı kendi bilgilerini girebilir, bir Trek çalışanı kullanıcı ile iletişime geçecek. Trek Türkiye için web adresimiz: https://www.alatin.com.tr "},
|
| 34 |
{"role": "system", "content": "Bike fit ücreti 1400 TL. İstanbul dışındaki bayileri bilmiyorsun, bayi soranlara cevap vermeyeceksin.. Tüm bayi detayları için https://www.alatin.com.tr/sayfa/bayilerimiz/ linkinine baksınlar. Bikefit işlem süresi yaklaşık 60 - 90 dakikadır."},
|
|
@@ -65,57 +86,4 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
|
|
| 65 |
history.append(inputs)
|
| 66 |
print(f"Logging : payload is - {payload}")
|
| 67 |
|
| 68 |
-
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
| 69 |
-
|
| 70 |
-
print(f"Logging : response code - {response}")
|
| 71 |
-
token_counter = 0
|
| 72 |
-
partial_words = ""
|
| 73 |
-
|
| 74 |
-
counter=0
|
| 75 |
-
for chunk in response.iter_lines():
|
| 76 |
-
|
| 77 |
-
if counter == 0:
|
| 78 |
-
counter+=1
|
| 79 |
-
continue
|
| 80 |
-
|
| 81 |
-
if chunk.decode() :
|
| 82 |
-
chunk = chunk.decode()
|
| 83 |
-
|
| 84 |
-
if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
|
| 85 |
-
partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"]
|
| 86 |
-
if token_counter == 0:
|
| 87 |
-
history.append(" " + partial_words)
|
| 88 |
-
else:
|
| 89 |
-
history[-1] = partial_words
|
| 90 |
-
chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
|
| 91 |
-
token_counter+=1
|
| 92 |
-
yield chat, history, chat_counter, response # resembles {chatbot: chat, state: history}
|
| 93 |
-
def reset_textbox():
|
| 94 |
-
return gr.update(value='')
|
| 95 |
-
def set_visible_false():
|
| 96 |
-
return gr.update(visible=False)
|
| 97 |
-
def set_visible_true():
|
| 98 |
-
return gr.update(visible=False)
|
| 99 |
-
theme_addon_msg = ""
|
| 100 |
-
system_msg_info = ""
|
| 101 |
-
theme = gr.themes.Soft(primary_hue="zinc", secondary_hue="green", neutral_hue="blue",
|
| 102 |
-
text_size=gr.themes.sizes.text_md)
|
| 103 |
-
|
| 104 |
-
with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;} #chatbot {height: 450px; overflow: auto;}""",
|
| 105 |
-
theme=theme) as demo:
|
| 106 |
-
with gr.Column(elem_id = "col_container"):
|
| 107 |
-
with gr.Accordion("", open=False, visible=False):
|
| 108 |
-
system_msg = gr.Textbox(value="")
|
| 109 |
-
accordion_msg = gr.HTML(value="", visible=False)
|
| 110 |
-
chatbot = gr.Chatbot(label='Trek Asistanı', elem_id="chatbot")
|
| 111 |
-
inputs = gr.Textbox(placeholder= "Buraya yazın, yanıtlayalım.", show_label= False)
|
| 112 |
-
state = gr.State([])
|
| 113 |
-
with gr.Accordion("", open=False, visible=False):
|
| 114 |
-
top_p = gr.Slider( minimum=-0, maximum=1.0, value=1.0, step=0.05, interactive=False, visible=False)
|
| 115 |
-
temperature = gr.Slider( minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=False, visible=False)
|
| 116 |
-
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
| 117 |
-
|
| 118 |
-
inputs.submit( predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter],) #openai_api_key
|
| 119 |
-
inputs.submit(reset_textbox, [], [inputs])
|
| 120 |
-
|
| 121 |
-
demo.queue(max_size=20, concurrency_count=20).launch(debug=True)
|
|
|
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import requests
|
| 5 |
+
import xml.etree.ElementTree as ET
|
| 6 |
+
from collections import defaultdict
|
| 7 |
+
|
| 8 |
|
| 9 |
# API ayarları
|
| 10 |
API_URL = "https://api.openai.com/v1/chat/completions"
|
| 11 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 12 |
|
| 13 |
+
def etree_to_dict(t):
|
| 14 |
+
d = {t.tag: {} if t.attrib else None}
|
| 15 |
+
children = list(t)
|
| 16 |
+
if children:
|
| 17 |
+
dd = defaultdict(list)
|
| 18 |
+
for dc in map(etree_to_dict, children):
|
| 19 |
+
for k, v in dc.items():
|
| 20 |
+
dd[k].append(v)
|
| 21 |
+
d = {t.tag: {k:v[0] if len(v) == 1 else v for k, v in dd.items()}}
|
| 22 |
+
if t.attrib:
|
| 23 |
+
d[t.tag].update(('@' + k, v) for k, v in t.attrib.items())
|
| 24 |
+
if t.text:
|
| 25 |
+
text = t.text.strip()
|
| 26 |
+
if children or t.attrib:
|
| 27 |
+
if text:
|
| 28 |
+
d[t.tag]['#text'] = text
|
| 29 |
+
else:
|
| 30 |
+
d[t.tag] = text
|
| 31 |
+
return d
|
| 32 |
+
|
| 33 |
+
def xml_to_json(url):
|
| 34 |
+
response = requests.get(url)
|
| 35 |
+
xml_string = response.text
|
| 36 |
+
tree = ET.ElementTree(ET.fromstring(xml_string))
|
| 37 |
+
return json.dumps(etree_to_dict(tree.getroot()))
|
| 38 |
|
| 39 |
+
url = "https://www.alatin.com.tr/index.php?do=catalog/output&pCode=4249932787"
|
| 40 |
+
print(xml_to_json(url))
|
|
|
|
| 41 |
|
|
|
|
| 42 |
|
| 43 |
def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
|
| 44 |
headers = {
|
|
|
|
| 49 |
|
| 50 |
|
| 51 |
initial_message = [{"role": "user", "content": f"{inputs}"},]
|
| 52 |
+
multi_turn_message = [{"role": "system", "content": xml_to_json(url)},
|
| 53 |
{"role": "system", "content": "Sen bir AI Trek marka bisiklet uzmanı, bilir kişisi ve asistanısın. Trek bisiklet dışında hiç bir konuda cevap vermeyeceksin. Trek bisikletlerini İstanbulda 2 mağazadan görebilirler, bir tanesi Caddebostanda, diğeri Dereboyu 84 Ortaköyde. Ortaköy mağazası Saat 10.00 da açılıp, 19.00 da kapanıyor. Telefon numarası 0212 2271015. caddebostan mağazamızın adresi: Prof. Dr. Hulusi Behçet 18 Caddebostan, Kadıköy. Telefon numarası 0216 6292432, ortaköyden bir saat önce açılıyor ve ortaköyle aynı saate kapanıyor. Ortaköy mağazası Pazar günleri kapalıdır. Ancak Caddebostan mağazamız haftanın 7 günü açıktır. Caddebostan mağazası her gün 09.00 da açılır ve 19.00 kapanır. sadece Pazar günleri 10.00 da açılır ve 17.00 de kapanır. Caddebostan mağazamızda haftanın 1 günü Bike-Fit yapılmaktadır. Retul firmasının bikefit makinesini kullanıyoruz. Salı günleri bike-fit yapabiliyoruz. Yapan kişi Emre Yavuz. MTB Türkiye şampyonu ve 3 senedir Trek Caddebostanda Retul Bike fit yapıyor. Bike fit için rezervasyon yapılması gerekir. Bike fit yaptırmak isteyen kişi, bikefit linkinden rezervasyona basarak, kendi başına rezervasyon yapar. 2012 yılından beri Retul Bike Fit hizmeti veriyoruz."},
|
| 54 |
{"role": "system", "content": "Trek Türkiye canlı destek hattına hoş geldiniz, demeyeceksin. Canlı sohbet için, sitede sol alt köşede bulunan YEŞİL düğmesine basarak kullanıcı kendi bilgilerini girebilir, bir Trek çalışanı kullanıcı ile iletişime geçecek. Trek Türkiye için web adresimiz: https://www.alatin.com.tr "},
|
| 55 |
{"role": "system", "content": "Bike fit ücreti 1400 TL. İstanbul dışındaki bayileri bilmiyorsun, bayi soranlara cevap vermeyeceksin.. Tüm bayi detayları için https://www.alatin.com.tr/sayfa/bayilerimiz/ linkinine baksınlar. Bikefit işlem süresi yaklaşık 60 - 90 dakikadır."},
|
|
|
|
| 86 |
history.append(inputs)
|
| 87 |
print(f"Logging : payload is - {payload}")
|
| 88 |
|
| 89 |
+
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|