import os import subprocess import math def install(package): subprocess.check_call(["pip", "install", package]) # Install required libraries install("numpy") install("pandas") install("torch") install("transformers") install("gradio") import numpy as np import pandas as pd import torch from transformers import AutoModel, AutoTokenizer import gradio as gr # Updated formulas in clear mathematical notation formulas = { "Wiener Index (W)": "W = Σ₍u,v₎ d(u,v)\nwhere d(u,v) is the shortest path distance between vertices u and v.", "First Zagreb Index (M₁)": "M₁ = Σ₍v ∈ V(G)₎ deg(v)", "Second Zagreb Index (M₂)": "M₂ = Σ₍uv ∈ E(G)₎ [deg(u) × deg(v)]", "Randić Index (R)": "R = Σ₍uv ∈ E(G)₎ 1/√(deg(u) × deg(v))", "Atom-Bond Connectivity Index (ABC)": "ABC = Σ₍uv ∈ E(G)₎ √((deg(u) + deg(v) - 2)/(deg(u) × deg(v)))", "Augmented Zagreb Index (AZI)": "AZI = Σ₍uv ∈ E(G)₎ [ (deg(u) × deg(v))/(deg(u) + deg(v) - 2) ]³", "Geometric-Arithmetic Index (GA)": "GA = Σ₍uv ∈ E(G)₎ [2×√(deg(u)×deg(v))/(deg(u)+deg(v))]", "Sum-Connectivity Index (SCI)": "SCI = Σ₍uv ∈ E(G)₎ 1/√(deg(u) + deg(v))", "Harmonic Index (Harm)": "Harm = Σ₍uv ∈ E(G)₎ [2/(deg(u)+deg(v))]", "Gutman Index (Gut)": "Gut = Σ₍u,v, u 0] result = sum(values) steps = [ f"1. **Shortest-path distances:** {distances}", f"2. **Reciprocals:** " + " + ".join(f"1/{d}" for d in distances if d > 0) + f" = {result:.4f}" ] output = "### **Harary Index (H)**\n\n" output += "**Formula:**\n" + formulas["Harary Index (H)"] + "\n\n" output += "**Required Input:** List of shortest-path distances between each vertex pair (comma-separated), e.g., 1,1,1\n\n" output += "**Step-by-step solution:**\n" + "\n".join(steps) + "\n\n" output += f"**Answer:** H = {result:.4f}" return output else: return "Index not recognized." except Exception as e: return f"Error in processing input: {e}" def update_input_placeholder(index): placeholders = { "Wiener Index (W)": "Enter distances (e.g., 1,1,1)", "First Zagreb Index (M₁)": "Enter vertex degrees (e.g., 2,3,4)", "Second Zagreb Index (M₂)": "Enter edge degree pairs (e.g., 2-3,3-2)", "Randić Index (R)": "Enter edge degree pairs (e.g., 2-3,3-2)", "Atom-Bond Connectivity Index (ABC)": "Enter edge degree pairs (e.g., 2-3,3-2)", "Augmented Zagreb Index (AZI)": "Enter edge degree pairs (e.g., 2-3,3-2)", "Geometric-Arithmetic Index (GA)": "Enter edge degree pairs (e.g., 2-3,3-2)", "Sum-Connectivity Index (SCI)": "Enter edge degree pairs (e.g., 2-3,3-2)", "Harmonic Index (Harm)": "Enter edge degree pairs (e.g., 2-3,3-2)", "Gutman Index (Gut)": "Enter degrees;distances (e.g., 2,2,2;1,1,1)", "Eccentric Connectivity Index (ECI)": "Enter deg-ecc pairs (e.g., 2-1,3-1,2-1)", "Total Eccentricity (TE)": "Enter eccentricities (e.g., 1,1,2)", "Harary Index (H)": "Enter distances (e.g., 1,1,1)" } return placeholders.get(index, "Enter input as required"), formulas.get(index, "") # Updated list of indices for the dropdown index_options = [ "Wiener Index (W)", "First Zagreb Index (M₁)", "Second Zagreb Index (M₂)", "Randić Index (R)", "Atom-Bond Connectivity Index (ABC)", "Augmented Zagreb Index (AZI)", "Geometric-Arithmetic Index (GA)", "Sum-Connectivity Index (SCI)", "Harmonic Index (Harm)", "Gutman Index (Gut)", "Eccentric Connectivity Index (ECI)", "Total Eccentricity (TE)", "Harary Index (H)" ] with gr.Blocks() as demo: gr.Markdown("## Topological Index Calculator\n\nSelect an index, review its formula, enter the required values as described, and click **Solve** to see a detailed, step-by-step solution.") index_dropdown = gr.Dropdown(index_options, label="Select an Index") formula_box = gr.Textbox(label="Formula", interactive=False) input_box = gr.Textbox(label="Enter Values") solve_button = gr.Button("Solve") output_box = gr.Textbox(label="Step-by-Step Solution", interactive=False, lines=20) index_dropdown.change(fn=lambda idx: update_input_placeholder(idx), inputs=[index_dropdown], outputs=[input_box, formula_box]) solve_button.click(fn=solve_index, inputs=[index_dropdown, input_box], outputs=[output_box]) demo.launch()