SamiKoen commited on
Commit
a7eb3c0
·
1 Parent(s): 162fac8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -45
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
- # Hugging Face pipeline'ını yükle
112
- nlp = pipeline('text-generation', model='EleutherAI/gpt-neo-2.7B')
113
-
114
- # API url'si
115
- url = 'https://bizimhesap.com/api/product/getproductsasxml?apikey=6F4BAF303FA240608A39653824B6C495'
116
-
117
- # Verileri al
118
- response = requests.get(url)
119
- xml_data = response.content
120
-
121
- # XML verilerini ayrıştır
122
- root = ET.fromstring(xml_data)
123
- products = root.findall('urunler/urun')
124
-
125
- # Chatbot'a girdi al
126
- input_text = input("Merhaba, ne yapabilirim? ")
 
 
 
 
 
 
 
 
 
 
 
 
 
127
 
128
- # Girdiye göre yanıt üret
129
- if 'stok' in input_text and ('durum' in input_text or 'seviye' in input_text):
130
- # Eğer kullanıcı stok durumunu sorduysa
131
- for product in products:
132
- if product.find('urun_ad').text.lower() in input_text.lower():
133
- stok = int(product.find('stok').text)
134
- if stok > 0:
135
- response_text = f"{product.find('urun_ad').text} ürününden {stok} adet mevcut."
136
- else:
137
- response_text = f"{product.find('urun_ad').text} ürünü stoklarda yok."
138
- break
139
- else:
140
- response_text = "Aradığınız ürün stoklarda yok."
141
- elif 'fiyat' in input_text:
142
- # Eğer kullanıcı fiyat bilgisi istiyorsa
143
- for product in products:
144
- if product.find('urun_ad').text.lower() in input_text.lower():
145
- price = float(product.find('satis_fiyat').text)
146
- response_text = f"{product.find('urun_ad').text} ürününün fiyatı {price:.2f} TL."
147
- break
148
  else:
149
- response_text = "Aradığınız ürün fiyatı hakkında bilgi verilemedi."
150
- else:
151
- # Girdiye göre otomatik bir yanıt üret
152
- generated_text = nlp(input_text, max_length=50, do_sample=True, temperature=0.7)
153
- response_text = generated_text[0]['generated_text'].strip()
154
-
155
- # Yanıtı yazdır
156
- print(response_text)
 
 
 
 
 
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()