shohabbosdev's picture
Update app.py
a8fe89e verified
import sympy as sp
import matplotlib.pyplot as plt
import gradio as gr
import time
def generate_math_formula_image(formula_str, progress=gr.Progress()):
try:
if formula_str is None:
return gr.Info("Kiritish maydoni bo'sh bo'lmasligi zarur %s" %formula_str), None
else:
progress(0, desc="Formula yaratish jarayoni boshlandi. Bu biroz vaqt olishi mumkin. Iltimos ozgina kuting!!!")
time.sleep(1)
# Matematik formulani yaratish
formula = sp.sympify(formula_str)
# Formulani LaTeX formatida ifodalash
latex_formula = sp.latex(formula)
# Formulani rasmga aylantirish
fig = plt.figure(figsize=(6, 2))
plt.text(0.5, 0.5, r'$%s$' % latex_formula, fontsize=20, horizontalalignment='center', verticalalignment='center')
plt.axis('off')
# Rasmni saqlab olish
image_file = 'formula.png'
plt.savefig(image_file, dpi=300, bbox_inches='tight', pad_inches=0.1)
plt.close(fig)
for i in progress.tqdm(range(100)):
time.sleep(0.01)
return latex_formula, image_file
except Exception as e:
return str(e), None
# Gradio interfaysini yaratish
with gr.Blocks(theme="gradio/seafoam") as demo:
gr.Markdown("""# Istalgan Matematik Formulalar
Barchasi juda oson shunchaki matn kiriting va uning matematik ifodasini latex formatda va rasm holatda yuklab oling
""")
with gr.Accordion(label="Dastur haqida qisqacha", open=False):
gr.Markdown("""# Bundan foydalanish juda oson!!!
Buning uchun siz python dasturini hamda <code>gradio</code>, <code>matplotlib</code>, <code>sympy</code> kabi kutubxonalar o'rnatasiz<br>
Hurmat bilan <a href="https://t.me/shohabbosde">Shoh Abbos Ulug'murodov</a>
""")
with gr.Row():
formula_input = gr.Textbox(label="Matematik formula kiritish qismi", placeholder="Formulani kiriting...", autofocus=True)
with gr.Row():
latex_output = gr.Textbox(label="LaTeX formatdagi formulasi", info="LaTeX formatdagi formula shu qismda ko'rinadi", show_copy_button=True)
image_output = gr.Image(label="Formula tasviri", elem_id="formula_image", show_share_button=True)
formula_input.change(fn=generate_math_formula_image, inputs=formula_input, outputs=[latex_output, image_output])
if __name__ == "__main__":
demo.launch()