Update app.py
Browse files
app.py
CHANGED
@@ -1,159 +1,23 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
import json
|
4 |
import requests
|
5 |
-
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
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 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
for input in inputs.split():
|
57 |
-
input_words.append(str(input).lower())
|
58 |
-
|
59 |
-
for product_info in products:
|
60 |
-
|
61 |
-
if product_info[0] in input_words:
|
62 |
-
print(product_info[0])
|
63 |
-
new_msg = f"{product_info[2]} {product_info[1][0]} ve fiyatı EURO {product_info[1][1]}"
|
64 |
-
product_msg = {"role": "system", "content": new_msg}
|
65 |
-
messages.append(product_msg)
|
66 |
-
for data in chatbot:
|
67 |
-
user = {}
|
68 |
-
user["role"] = "user"
|
69 |
-
user["content"] = data[0]
|
70 |
-
assistant = {}
|
71 |
-
assistant["role"] = "assistant"
|
72 |
-
assistant["content"] = data[1]
|
73 |
-
messages.append(user)
|
74 |
-
messages.append(assistant)
|
75 |
-
temp = {}
|
76 |
-
temp["role"] = "user"
|
77 |
-
temp["content"] = inputs
|
78 |
-
messages.append(temp)
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
payload = {"model": "gpt-4", "messages": messages, "temperature": 0.7,
|
83 |
-
"top_p": 1.0, "n": 1, "stream": True, "presence_penalty": 0, "frequency_penalty": 0, }
|
84 |
-
|
85 |
-
chat_counter += 1
|
86 |
-
|
87 |
-
history.append(inputs)
|
88 |
-
print(f"Logging : payload is - {payload}")
|
89 |
-
|
90 |
-
response = requests.post(API_URL, headers=headers,
|
91 |
-
json=payload, stream=True)
|
92 |
-
print(f"Logging : response code - {response}")
|
93 |
-
token_counter = 0
|
94 |
-
partial_words = ""
|
95 |
-
|
96 |
-
counter = 0
|
97 |
-
for chunk in response.iter_lines():
|
98 |
-
|
99 |
-
if counter == 0:
|
100 |
-
counter += 1
|
101 |
-
continue
|
102 |
-
|
103 |
-
if chunk.decode():
|
104 |
-
chunk = chunk.decode()
|
105 |
-
|
106 |
-
if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
|
107 |
-
partial_words = partial_words + \
|
108 |
-
json.loads(chunk[6:])['choices'][0]["delta"]["content"]
|
109 |
-
if token_counter == 0:
|
110 |
-
history.append(" " + partial_words)
|
111 |
-
else:
|
112 |
-
history[-1] = partial_words
|
113 |
-
chat = [(history[i], history[i + 1]) for i in range(0,
|
114 |
-
len(history) - 1, 2)] # convert to tuples of list
|
115 |
-
token_counter += 1
|
116 |
-
# resembles {chatbot: chat, state: history}
|
117 |
-
yield chat, history, chat_counter, response
|
118 |
-
|
119 |
-
|
120 |
-
def reset_textbox():
|
121 |
-
return gr.update(value='')
|
122 |
-
|
123 |
-
|
124 |
-
def set_visible_false():
|
125 |
-
return gr.update(visible=False)
|
126 |
-
|
127 |
-
|
128 |
-
def set_visible_true():
|
129 |
-
return gr.update(visible=False)
|
130 |
-
|
131 |
-
|
132 |
-
theme_addon_msg = ""
|
133 |
-
system_msg_info = ""
|
134 |
-
theme = gr.themes.Soft(primary_hue="zinc", secondary_hue="green", neutral_hue="blue",
|
135 |
-
text_size=gr.themes.sizes.text_md)
|
136 |
-
|
137 |
-
with gr.Blocks(css="""#col_container { margin-left: auto; margin-right: auto;} #chatbot {height: 450px; overflow: auto;}""",
|
138 |
-
theme=theme) as demo:
|
139 |
-
with gr.Column(elem_id="col_container"):
|
140 |
-
with gr.Accordion("", open=False, visible=False):
|
141 |
-
system_msg = gr.Textbox(value="")
|
142 |
-
new_msg = gr.Textbox(value="")
|
143 |
-
accordion_msg = gr.HTML(value="", visible=False)
|
144 |
-
chatbot = gr.Chatbot(label='Trek Asistanı', elem_id="chatbot")
|
145 |
-
inputs = gr.Textbox(
|
146 |
-
placeholder="Buraya yazın, yanıtlayalım.", show_label=False)
|
147 |
-
state = gr.State([])
|
148 |
-
with gr.Accordion("", open=False, visible=False):
|
149 |
-
top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0,
|
150 |
-
step=0.05, interactive=False, visible=False)
|
151 |
-
temperature = gr.Slider(
|
152 |
-
minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=False, visible=False)
|
153 |
-
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
154 |
-
|
155 |
-
inputs.submit(predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [
|
156 |
-
chatbot, state, chat_counter],) # openai_api_key
|
157 |
-
inputs.submit(reset_textbox, [], [inputs])
|
158 |
-
|
159 |
-
demo.queue(max_size=20, concurrency_count=20).launch(debug=True)
|
|
|
|
|
|
|
|
|
1 |
import requests
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
|
4 |
+
def get_alatin_titles():
|
5 |
+
# Web sitesinden içerik çekme
|
6 |
+
url = "https://www.alatin.com.tr/"
|
7 |
+
response = requests.get(url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
# Eğer başarılı bir cevap alındıysa
|
10 |
+
if response.status_code == 200:
|
11 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
12 |
+
|
13 |
+
# Başlıkları çekme (Bu seçici, site yapısına bağlı olarak değişebilir)
|
14 |
+
titles = soup.find_all('h2') # Örnek olarak h2 etiketlerini çekiyoruz
|
15 |
+
|
16 |
+
for title in titles:
|
17 |
+
print(title.text)
|
18 |
+
|
19 |
+
else:
|
20 |
+
print("Web sitesine erişimde bir sorun oluştu.")
|
21 |
+
|
22 |
+
# Fonksiyonu çağırma
|
23 |
+
get_alatin_titles()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|