Update app.py
Browse files
app.py
CHANGED
@@ -4,153 +4,127 @@ import json
|
|
4 |
import requests
|
5 |
import xml.etree.ElementTree as ET
|
6 |
|
7 |
-
#
|
8 |
API_URL = "https://api.openai.com/v1/chat/completions"
|
9 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
url = 'https://www.alatin.com.tr/index.php?do=catalog/output&pCode=8582384479'
|
12 |
-
|
13 |
-
response = requests.get(url)
|
14 |
-
|
15 |
-
|
16 |
-
root = ET.fromstring(response.content)
|
17 |
-
|
18 |
-
products = []
|
19 |
-
|
20 |
-
for item in root.findall('item'):
|
21 |
-
if item.find('isOptionOfAProduct').text == '1':
|
22 |
-
if item.find('stockAmount').text > '0':
|
23 |
-
name_words = item.find('rootlabel').text.lower().split()
|
24 |
-
name = name_words[0]
|
25 |
-
full_name = ' '.join(name_words)
|
26 |
-
stockAmount = "stokta"
|
27 |
-
price = item.find('priceWithTax').text
|
28 |
-
item_info = (stockAmount, price)
|
29 |
-
# name: ilk kelime (marka), item_info: (stok adedi, fiyat)
|
30 |
-
products.append((name, item_info, full_name))
|
31 |
-
|
32 |
|
33 |
def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
|
34 |
headers = {
|
35 |
"Content-Type": "application/json",
|
36 |
"Authorization": f"Bearer {OPENAI_API_KEY}"
|
37 |
}
|
38 |
-
print(f"system message is ^^ {system_msg}")
|
39 |
-
initial_message = [{"role": "user", "content": f"{inputs}"},]
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
{"role": "system", "content": "Trek Türkiye, ASLA DURMA markasının yaratıcısı Fatih Topçu ve TREK RMK DYNAMIS bisiklet takımına sponsordur. Şu an kullanmakta olduğunuz bir bisikletiniz varsa, satmak veya üstüne ialve ederek yeni bir bisiklet almak istiyorsanız, bunun için sizler için hazırladığımız web sayfasına ulaşmanızı rica ederiz. Web adresi şöyledir: https://www.bikeexchangehub.com/ . Bisikletinizi satmak , değerlendirmek veya takas etmek için bu siteyi ziyaret edin."},
|
49 |
-
{"role": "system", "content": "cevap yazarken vereceğin bilginin tek bir bilgi içinde yan yana yazdığını iki kere kontrol edeceksin. Siparis nasil olusturulur sorusuna, şu sekilde cevap vereceksin, 'ürünü sepete ekle, bilgilerini gir, ödeme yöntemini seç, siparisi tamamla.'"},
|
50 |
-
]
|
51 |
-
messages = multi_turn_message
|
52 |
-
input_words = []
|
53 |
-
for input in inputs.split():
|
54 |
-
input_words.append(str(input).lower())
|
55 |
|
56 |
for product_info in products:
|
57 |
-
|
58 |
if product_info[0] in input_words:
|
59 |
new_msg = f"{product_info[2]} {product_info[1][0]} ve fiyatı EURO {product_info[1][1]}"
|
60 |
-
|
61 |
-
product_msg = {"role": "system", "content": new_msg}
|
62 |
-
messages.append(product_msg)
|
63 |
|
64 |
for data in chatbot:
|
65 |
-
user
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
assistant["role"] = "assistant"
|
70 |
-
assistant["content"] = data[1]
|
71 |
-
messages.append(user)
|
72 |
-
messages.append(assistant)
|
73 |
-
temp = {}
|
74 |
-
temp["role"] = "user"
|
75 |
-
temp["content"] = inputs
|
76 |
-
messages.append(temp)
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
-
|
83 |
|
84 |
history.append(inputs)
|
85 |
-
print(f"Logging : payload is - {payload}")
|
86 |
-
|
87 |
-
response = requests.post(API_URL, headers=headers,
|
88 |
-
json=payload, stream=True)
|
89 |
-
print(f"Logging : response code - {response}")
|
90 |
token_counter = 0
|
91 |
partial_words = ""
|
92 |
|
93 |
-
counter = 0
|
94 |
for chunk in response.iter_lines():
|
95 |
-
|
96 |
-
if counter == 0:
|
97 |
-
counter += 1
|
98 |
-
continue
|
99 |
-
|
100 |
if chunk.decode():
|
101 |
chunk = chunk.decode()
|
102 |
-
|
103 |
-
|
104 |
-
partial_words = partial_words + \
|
105 |
-
json.loads(chunk[6:])['choices'][0]["delta"]["content"]
|
106 |
if token_counter == 0:
|
107 |
history.append(" " + partial_words)
|
108 |
else:
|
109 |
history[-1] = partial_words
|
110 |
-
chat = [(history[i], history[i + 1]) for i in range(0,
|
111 |
-
len(history) - 1, 2)] # convert to tuples of list
|
112 |
token_counter += 1
|
113 |
-
|
114 |
-
yield chat, history, chat_counter, response
|
115 |
-
|
116 |
|
117 |
def reset_textbox():
|
118 |
return gr.update(value='')
|
119 |
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
chatbot = gr.Chatbot(label='Trek Asistanı', elem_id="chatbot")
|
142 |
-
inputs = gr.Textbox(
|
143 |
-
placeholder="Buraya yazın, yanıtlayalım.", show_label=False)
|
144 |
state = gr.State([])
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
151 |
-
|
152 |
-
inputs.submit(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [
|
153 |
-
chatbot, state, chat_counter],) # openai_api_key
|
154 |
inputs.submit(reset_textbox, [], [inputs])
|
155 |
|
156 |
-
demo.queue(max_size=10, concurrency_count=10).launch(debug=True)
|
|
|
4 |
import requests
|
5 |
import xml.etree.ElementTree as ET
|
6 |
|
7 |
+
# OpenAI API URL and Key
|
8 |
API_URL = "https://api.openai.com/v1/chat/completions"
|
9 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
10 |
|
11 |
+
# Function to get product data from XML URL
|
12 |
+
def get_product_data(url):
|
13 |
+
response = requests.get(url)
|
14 |
+
root = ET.fromstring(response.content)
|
15 |
+
products = []
|
16 |
+
|
17 |
+
for item in root.findall('item'):
|
18 |
+
if item.find('isOptionOfAProduct').text == '1':
|
19 |
+
if int(item.find('stockAmount').text) > 0:
|
20 |
+
name_words = item.find('rootlabel').text.lower().split()
|
21 |
+
name = name_words[0]
|
22 |
+
full_name = ' '.join(name_words)
|
23 |
+
stockAmount = "stokta"
|
24 |
+
price = item.find('priceWithTax').text
|
25 |
+
item_info = (stockAmount, price)
|
26 |
+
products.append((name, item_info, full_name))
|
27 |
+
|
28 |
+
return products
|
29 |
+
|
30 |
+
# Load product data
|
31 |
url = 'https://www.alatin.com.tr/index.php?do=catalog/output&pCode=8582384479'
|
32 |
+
products = get_product_data(url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
|
35 |
headers = {
|
36 |
"Content-Type": "application/json",
|
37 |
"Authorization": f"Bearer {OPENAI_API_KEY}"
|
38 |
}
|
|
|
|
|
39 |
|
40 |
+
initial_message = [{"role": "user", "content": f"{inputs}"},]
|
41 |
+
multi_turn_message = [
|
42 |
+
{"role": "system", "content": "Bir önceki sohbeti unut. Vereceğin ürün bilgisi... (devamı)"}
|
43 |
+
]
|
44 |
+
messages = multi_turn_message + initial_message
|
45 |
+
|
46 |
+
input_words = [word.lower() for word in inputs.split()]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
for product_info in products:
|
|
|
49 |
if product_info[0] in input_words:
|
50 |
new_msg = f"{product_info[2]} {product_info[1][0]} ve fiyatı EURO {product_info[1][1]}"
|
51 |
+
messages.append({"role": "system", "content": new_msg})
|
|
|
|
|
52 |
|
53 |
for data in chatbot:
|
54 |
+
messages.append({"role": "user", "content": data[0]})
|
55 |
+
messages.append({"role": "assistant", "content": data[1]})
|
56 |
+
|
57 |
+
messages.append({"role": "user", "content": inputs})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
+
payload = {
|
60 |
+
"model": "gpt-4o",
|
61 |
+
"messages": messages,
|
62 |
+
"temperature": temperature,
|
63 |
+
"top_p": top_p,
|
64 |
+
"n": 1,
|
65 |
+
"stream": True,
|
66 |
+
"presence_penalty": 0,
|
67 |
+
"frequency_penalty": 0,
|
68 |
+
}
|
69 |
|
70 |
+
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
71 |
|
72 |
history.append(inputs)
|
|
|
|
|
|
|
|
|
|
|
73 |
token_counter = 0
|
74 |
partial_words = ""
|
75 |
|
|
|
76 |
for chunk in response.iter_lines():
|
|
|
|
|
|
|
|
|
|
|
77 |
if chunk.decode():
|
78 |
chunk = chunk.decode()
|
79 |
+
if "content" in json.loads(chunk[6:])['choices'][0]['delta']:
|
80 |
+
partial_words += json.loads(chunk[6:])['choices'][0]["delta"]["content"]
|
|
|
|
|
81 |
if token_counter == 0:
|
82 |
history.append(" " + partial_words)
|
83 |
else:
|
84 |
history[-1] = partial_words
|
85 |
+
chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)]
|
|
|
86 |
token_counter += 1
|
87 |
+
yield chat, history, chat_counter
|
|
|
|
|
88 |
|
89 |
def reset_textbox():
|
90 |
return gr.update(value='')
|
91 |
|
92 |
+
css = """
|
93 |
+
#chatbot {
|
94 |
+
height: 450px;
|
95 |
+
overflow: auto;
|
96 |
+
display: flex;
|
97 |
+
flex-direction: column-reverse;
|
98 |
+
}
|
99 |
+
.message {
|
100 |
+
display: flex;
|
101 |
+
padding: 8px;
|
102 |
+
border-radius: 5px;
|
103 |
+
margin: 5px 0;
|
104 |
+
max-width: 60%;
|
105 |
+
}
|
106 |
+
.user {
|
107 |
+
background-color: #DCF8C6;
|
108 |
+
align-self: flex-end;
|
109 |
+
}
|
110 |
+
.assistant {
|
111 |
+
background-color: #ECECEC;
|
112 |
+
align-self: flex-start;
|
113 |
+
}
|
114 |
+
"""
|
115 |
+
|
116 |
+
theme = gr.themes.Soft(primary_hue="zinc", secondary_hue="green", neutral_hue="blue", text_size=gr.themes.sizes.text_sm)
|
117 |
+
|
118 |
+
with gr.Blocks(css=css, theme=theme) as demo:
|
119 |
+
with gr.Column():
|
120 |
chatbot = gr.Chatbot(label='Trek Asistanı', elem_id="chatbot")
|
121 |
+
inputs = gr.Textbox(placeholder="Buraya yazın, yanıtlayalım.", show_label=False)
|
|
|
122 |
state = gr.State([])
|
123 |
+
top_p = gr.Slider(minimum=0, maximum=1.0, value=0.5, step=0.05, interactive=False, visible=False)
|
124 |
+
temperature = gr.Slider(minimum=0, maximum=5.0, value=0.1, step=0.1, interactive=False, visible=False)
|
125 |
+
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
126 |
+
|
127 |
+
inputs.submit(predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
|
|
|
|
|
|
|
|
|
128 |
inputs.submit(reset_textbox, [], [inputs])
|
129 |
|
130 |
+
demo.queue(max_size=10, concurrency_count=10).launch(debug=True)
|