VIATEUR-AI commited on
Commit
feffa2d
·
verified ·
1 Parent(s): d26951a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -34
app.py CHANGED
@@ -1,44 +1,70 @@
1
  import gradio as gr
2
 
3
- # Fungura stock nshya (nta na kimwe gihari mu ntangiriro)
4
- stock = {}
5
-
6
- def add_product(product_name, quantity):
7
- if product_name.strip() == "":
8
- return "Andika izina ry'igicuruzwa."
9
- if quantity < 0:
10
- return "Ingano y'ububiko igomba kuba itari hasi ya zeru."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # Ongeraho cyangwa uvugurure ububiko
13
- stock[product_name] = stock.get(product_name, 0) + quantity
14
- return f"Ibicuruzwa '{product_name}' byongeweho/ byavuguruwe. Ububiko ubu: {stock[product_name]}"
15
-
16
- def check_stock(threshold):
17
- if not stock:
18
- return "Nta bicuruzwa biri mu bubiko."
19
- low_stock_items = [f"{item} (Ububiko: {qty})" for item, qty in stock.items() if qty < threshold]
20
- if low_stock_items:
21
- return "Ibi bicuruzwa biri mu bubiko buke, hasabwa kongera:\n" + "\n".join(low_stock_items)
22
- else:
23
- return "Ububiko bwose burasa neza! Nta kibazo cyo kongera ububiko."
24
 
25
  with gr.Blocks() as demo:
26
  gr.Markdown("# AI - Gucunga Ububiko ku Isoko")
27
 
28
- with gr.Row():
29
- product_name = gr.Textbox(label="Izina ry'igicuruzwa")
30
- quantity = gr.Number(label="Ingano y'ububiko (positive integer)", value=0, precision=0)
31
- add_btn = gr.Button("Ongeraho / Vugurura ububiko")
32
-
33
- output_add = gr.Textbox(label="Ibisubizo byo kongera ububiko")
34
-
35
- add_btn.click(add_product, inputs=[product_name, quantity], outputs=output_add)
36
-
37
- threshold = gr.Slider(0, 50, value=10, step=1, label="Hitamo umupaka w'ububiko uri hasi cyane")
38
- check_btn = gr.Button("Genura ububiko buke")
39
- output_check = gr.Textbox(label="Ibisubizo byo kugenzura ububiko")
40
-
41
- check_btn.click(check_stock, inputs=threshold, outputs=output_check)
42
 
43
  if __name__ == "__main__":
44
  demo.launch()
 
1
  import gradio as gr
2
 
3
+ # Ububiko bw'intangiriro
4
+ stock = {
5
+ "Potatoes": 100,
6
+ "Tomatoes": 50,
7
+ "Carrots": 30,
8
+ "Ibicuruzwa_A": 50,
9
+ "Ibicuruzwa_B": 8,
10
+ "Ibicuruzwa_C": 0,
11
+ "Ibicuruzwa_D": 15,
12
+ }
13
+
14
+ def add_products_batch(batch_input):
15
+ """
16
+ Buri murongo wanditse muri format: izina, quantity
17
+ Urugero:
18
+ Potatoes, 100
19
+ Tomatoes, 50
20
+ """
21
+ if not batch_input.strip():
22
+ return "Andika ibicuruzwa n'ububiko bya byo, buri kimwe ku murongo, mu format: izina, quantity.", gr.Dropdown.update(choices=list(stock.keys()))
23
+
24
+ lines = batch_input.strip().split("\n")
25
+ messages = []
26
+ for line in lines:
27
+ parts = line.split(",")
28
+ if len(parts) != 2:
29
+ messages.append(f"Umurongo '{line}' ntusobanutse neza. Format: izina, quantity")
30
+ continue
31
+ product_name = parts[0].strip()
32
+ try:
33
+ quantity = int(parts[1].strip())
34
+ if quantity < 0:
35
+ messages.append(f"Ingano y'ububiko igomba kuba itari hasi ya zeru: '{line}'")
36
+ continue
37
+ except ValueError:
38
+ messages.append(f"Ingano y'ububiko si integer: '{line}'")
39
+ continue
40
+
41
+ stock[product_name] = stock.get(product_name, 0) + quantity
42
+ messages.append(f"'{product_name}' yongewemo/ yavuguruwe: {stock[product_name]}")
43
 
44
+ return "\n".join(messages), gr.Dropdown.update(choices=list(stock.keys()))
45
+
46
+ def get_stock_value(selected_product):
47
+ if selected_product not in stock:
48
+ return "Icyo gicuruzwa ntikirimo mu bubiko."
49
+ return f"Ububiko bwa '{selected_product}' ni: {stock[selected_product]}"
 
 
 
 
 
 
50
 
51
  with gr.Blocks() as demo:
52
  gr.Markdown("# AI - Gucunga Ububiko ku Isoko")
53
 
54
+ with gr.Column():
55
+ gr.Markdown("### Ongera ibicuruzwa byinshi icyarimwe (buri kimwe ku murongo: izina, quantity)")
56
+ batch_input = gr.Textarea(label="Andika ibicuruzwa na quantity (buri murongo: izina, quantity)", lines=5)
57
+ add_btn = gr.Button("Ongeramo ibicuruzwa byinshi")
58
+ output_add = gr.Textbox(label="Ibisubizo byo kongera ububiko")
59
+
60
+ add_btn.click(add_products_batch, inputs=batch_input, outputs=[output_add, batch_input])
61
+
62
+ gr.Markdown("### Reba ububiko bw'igicuruzwa runaka")
63
+ product_dropdown = gr.Dropdown(choices=list(stock.keys()), label="Hitamo igicuruzwa")
64
+ check_btn = gr.Button("Reba ububiko")
65
+ output_check = gr.Textbox(label="Agaciro k'ububiko")
66
+
67
+ check_btn.click(get_stock_value, inputs=product_dropdown, outputs=output_check)
68
 
69
  if __name__ == "__main__":
70
  demo.launch()