Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +73 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from sympy import symbols, Eq, lcm, linsolve
|
| 3 |
+
import re
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
def parse_formula(formula):
|
| 7 |
+
pattern = r'([A-Z][a-z]?)(\d*)'
|
| 8 |
+
elements = re.findall(pattern, formula)
|
| 9 |
+
counts = {}
|
| 10 |
+
for (elem, num) in elements:
|
| 11 |
+
num = int(num) if num else 1
|
| 12 |
+
counts[elem] = counts.get(elem, 0) + num
|
| 13 |
+
return counts
|
| 14 |
+
|
| 15 |
+
def balance_equation(reactants, products):
|
| 16 |
+
elements = set()
|
| 17 |
+
for compound in reactants + products:
|
| 18 |
+
elements.update(parse_formula(compound).keys())
|
| 19 |
+
elements = list(elements)
|
| 20 |
+
coeffs = symbols(' '.join([f'a{i}' for i in range(len(reactants)+len(products))]), integer=True)
|
| 21 |
+
equations = []
|
| 22 |
+
for element in elements:
|
| 23 |
+
lhs = sum(parse_formula(reactants[i]).get(element, 0) * coeffs[i] for i in range(len(reactants)))
|
| 24 |
+
rhs = sum(parse_formula(products[i]).get(element, 0) * coeffs[i+len(reactants)] for i in range(len(products)))
|
| 25 |
+
equations.append(Eq(lhs, rhs))
|
| 26 |
+
equations.append(Eq(coeffs[0], 1))
|
| 27 |
+
sol = linsolve(equations, coeffs)
|
| 28 |
+
if not sol:
|
| 29 |
+
return None
|
| 30 |
+
sol = list(sol)[0]
|
| 31 |
+
denoms = [term.as_numer_denom()[1] for term in sol]
|
| 32 |
+
multiple = lcm(denoms)
|
| 33 |
+
coeff_ints = [int(term * multiple) for term in sol]
|
| 34 |
+
return coeff_ints
|
| 35 |
+
|
| 36 |
+
def run_app(reactants_input, products_input):
|
| 37 |
+
reactants = [r.strip() for r in reactants_input.split(',')]
|
| 38 |
+
products = [p.strip() for p in products_input.split(',')]
|
| 39 |
+
all_compounds = reactants + products
|
| 40 |
+
|
| 41 |
+
data = []
|
| 42 |
+
for c in all_compounds:
|
| 43 |
+
parsed = parse_formula(c)
|
| 44 |
+
for elem, count in parsed.items():
|
| 45 |
+
side = 'Tham gia' if c in reactants else 'Sản phẩm'
|
| 46 |
+
data.append({'Chất': c, 'Nguyên tố': elem, 'Số lượng': count, 'Vế': side})
|
| 47 |
+
df = pd.DataFrame(data)
|
| 48 |
+
|
| 49 |
+
coeffs = balance_equation(reactants, products)
|
| 50 |
+
if coeffs:
|
| 51 |
+
lhs = ' + '.join(f"{coeffs[i]} {reactants[i]}" for i in range(len(reactants)))
|
| 52 |
+
rhs = ' + '.join(f"{coeffs[i+len(reactants)]} {products[i]}" for i in range(len(products)))
|
| 53 |
+
equation = f"✅ Phương trình đã cân bằng:\n\n{lhs} → {rhs}"
|
| 54 |
+
else:
|
| 55 |
+
equation = "❌ Không cân bằng được phương trình."
|
| 56 |
+
|
| 57 |
+
return df, equation
|
| 58 |
+
|
| 59 |
+
iface = gr.Interface(
|
| 60 |
+
fn=run_app,
|
| 61 |
+
inputs=[
|
| 62 |
+
gr.Textbox(label="Chất tham gia (cách nhau bằng dấu ,)", placeholder="Ví dụ: Fe, O2"),
|
| 63 |
+
gr.Textbox(label="Chất sản phẩm (cách nhau bằng dấu ,)", placeholder="Ví dụ: Fe2O3")
|
| 64 |
+
],
|
| 65 |
+
outputs=[
|
| 66 |
+
gr.Dataframe(label="Bảng nguyên tố & số nguyên tử", wrap=True),
|
| 67 |
+
gr.Textbox(label="Kết quả phương trình", lines=4)
|
| 68 |
+
],
|
| 69 |
+
title="⚗️ Ứng dụng cân bằng phương trình hóa học",
|
| 70 |
+
description="Nhập chất tham gia và chất sản phẩm, cách nhau bằng dấu phẩy. App sẽ hiển thị bảng nguyên tố & số nguyên tử + phương trình đã cân bằng."
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
sympy
|
| 3 |
+
pandas
|