Update app.py
Browse files
app.py
CHANGED
@@ -106,51 +106,65 @@ demo.queue(max_size=20, concurrency_count=20).launch(debug=True)
|
|
106 |
|
107 |
import requests
|
108 |
import xml.etree.ElementTree as ET
|
109 |
-
from transformers import pipeline
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
-
|
129 |
-
if
|
130 |
-
|
131 |
-
|
132 |
-
if
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
response_text = f"{product.find('urun_ad').text} ürününün fiyatı {price:.2f} TL."
|
147 |
-
break
|
148 |
else:
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
import requests
|
108 |
import xml.etree.ElementTree as ET
|
|
|
109 |
|
110 |
+
def get_product_info(product_name):
|
111 |
+
api_key = "6F4BAF303FA240608A39653824B6C495"
|
112 |
+
url = f"https://bizimhesap.com/api/product/getproductsasxml?apikey={api_key}"
|
113 |
+
response = requests.get(url)
|
114 |
+
root = ET.fromstring(response.content)
|
115 |
+
product_info = {}
|
116 |
+
for product in root.findall("./urunler/urun"):
|
117 |
+
name = product.find("./urun_ad").text.strip()
|
118 |
+
if product_name.lower() in name.lower():
|
119 |
+
product_info["name"] = name
|
120 |
+
product_info["code"] = product.find("./stok_kod").text.strip()
|
121 |
+
product_info["stock"] = int(product.find("./stok").text)
|
122 |
+
product_info["price"] = float(product.find("./satis_fiyat").text.replace(',', '.'))
|
123 |
+
product_info["currency"] = product.find("./para_birim").text.strip()
|
124 |
+
product_info["detail"] = product.find("./detay").text.strip()
|
125 |
+
product_info["category"] = product.find("./kat_yolu").text.strip()
|
126 |
+
product_info["image_url"] = product.find("./resim").text.strip()
|
127 |
+
variant = product.find("./varyant").text.strip()
|
128 |
+
if "renk" in variant.lower():
|
129 |
+
color_start = variant.find("(") + 1
|
130 |
+
color_end = variant.find(")", color_start)
|
131 |
+
product_info["color"] = variant[color_start:color_end]
|
132 |
+
if "kadro" in variant.lower():
|
133 |
+
frame_size_start = variant.find(":") + 1
|
134 |
+
product_info["frame_size"] = variant[frame_size_start:].strip()
|
135 |
+
return product_info
|
136 |
+
|
137 |
+
# Gradio UI
|
138 |
+
import gradio as gr
|
139 |
|
140 |
+
def answer_question(question):
|
141 |
+
if "stok" in question.lower():
|
142 |
+
product_name = question.lower().replace("stokta", "").replace("stok", "").replace("miktar", "").strip()
|
143 |
+
product_info = get_product_info(product_name)
|
144 |
+
if not product_info:
|
145 |
+
return f"{product_name.title()} ürünü maalesef stoklarımızda bulunmamaktadır."
|
146 |
+
else:
|
147 |
+
stock = product_info["stock"]
|
148 |
+
return f"{product_info['name'].title()} ürününden {stock} adet stoklarımızda mevcuttur."
|
149 |
+
elif "fiyat" in question.lower():
|
150 |
+
product_name = question.lower().replace("fiyatı", "").replace("fiyat", "").strip()
|
151 |
+
product_info = get_product_info(product_name)
|
152 |
+
if not product_info:
|
153 |
+
return f"{product_name.title()} ürünü maalesef stoklarımızda bulunmamaktadır."
|
154 |
+
else:
|
155 |
+
price = product_info["price"]
|
156 |
+
currency = product_info["currency"]
|
157 |
+
return f"{product_info['name'].title()} ürününün fiyatı {price} {currency}'dir."
|
|
|
|
|
158 |
else:
|
159 |
+
return "Maalesef anlayamadım. Lütfen sorunuzu tekrar sorun."
|
160 |
+
|
161 |
+
iface = gr.Interface(
|
162 |
+
fn=answer_question,
|
163 |
+
inputs="text",
|
164 |
+
outputs="text",
|
165 |
+
title="BizimHesap Chatbot",
|
166 |
+
description="Ürün stok ve fiyat sorguları için kullanabilirsiniz.",
|
167 |
+
theme="huggingface"
|
168 |
+
)
|
169 |
+
|
170 |
+
iface.launch()
|