Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import sympy
|
| 3 |
+
from graphviz import Digraph
|
| 4 |
+
|
| 5 |
+
# Asal çarpanları bulan fonksiyon
|
| 6 |
+
def asal_carpanlar(sayi):
|
| 7 |
+
return sympy.factorint(sayi)
|
| 8 |
+
|
| 9 |
+
# Çarpan ağacını oluşturmak için rekürsif fonksiyon
|
| 10 |
+
def carpan_agaci(sayi):
|
| 11 |
+
factors = sympy.factorint(sayi)
|
| 12 |
+
if len(factors) == 1 and list(factors.values())[0] == 1:
|
| 13 |
+
return str(sayi)
|
| 14 |
+
else:
|
| 15 |
+
factor = list(factors.keys())[0]
|
| 16 |
+
return [sayi, carpan_agaci(factor), carpan_agaci(sayi // factor)]
|
| 17 |
+
|
| 18 |
+
# Ağacı grafiksel olarak oluşturan fonksiyon
|
| 19 |
+
def ciz_agac(agac, graph=None):
|
| 20 |
+
if graph is None:
|
| 21 |
+
graph = Digraph()
|
| 22 |
+
if isinstance(agac, list):
|
| 23 |
+
parent = str(agac[0])
|
| 24 |
+
left, right = agac[1], agac[2]
|
| 25 |
+
for child in [left, right]:
|
| 26 |
+
child_label = str(child[0]) if isinstance(child, list) else str(child)
|
| 27 |
+
graph.edge(parent, child_label)
|
| 28 |
+
if isinstance(child, list):
|
| 29 |
+
ciz_agac(child, graph)
|
| 30 |
+
return graph
|
| 31 |
+
|
| 32 |
+
# Gradio arayüzü fonksiyonu
|
| 33 |
+
def asal_carpan_gorseli(sayi):
|
| 34 |
+
agac = carpan_agaci(int(sayi))
|
| 35 |
+
graph = ciz_agac(agac)
|
| 36 |
+
graph.render("asal_carpan_agaci", format="png")
|
| 37 |
+
return "asal_carpan_agaci.png"
|
| 38 |
+
|
| 39 |
+
# Gradio arayüz tasarımı
|
| 40 |
+
with gr.Blocks() as demo:
|
| 41 |
+
gr.Markdown("### 🔢 Asal Çarpan Ağacı Hesaplama")
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
sayi = gr.Number(label="Bir sayı girin", minimum=2, value=48)
|
| 45 |
+
|
| 46 |
+
btn = gr.Button("Asal Çarpan Ağacını Oluştur 🌳")
|
| 47 |
+
|
| 48 |
+
output_img = gr.Image(label="Asal Çarpan Ağacı Görseli", type="filepath")
|
| 49 |
+
|
| 50 |
+
btn.click(asal_carpan_gorseli, inputs=sayi, outputs=output_img)
|
| 51 |
+
|
| 52 |
+
demo.launch()
|