Team Ai
Apppublic

MUsman12321/Knowledge-based-system

sourceHugging Facemitupdated 7mo agoView on Hugging Face
0likes
app.py184 linesDownload Raw Back to root
1import gradio as gr2 3# ------------------------------4# KNOWLEDGE BASE (25 Production Rules)5# ------------------------------6 7rules = [8 9    # ===== HARDWARE =====10    {"problem": "Power Supply Failure",11     "conditions": ["no_power", "black_screen"],12     "solution": "Check power cable and replace power supply if necessary."},13 14    {"problem": "RAM Failure",15     "conditions": ["beeping_sound", "random_restart"],16     "solution": "Reseat or replace RAM modules."},17 18    {"problem": "Hard Drive Failure",19     "conditions": ["slow_boot", "disk_error"],20     "solution": "Run disk diagnostics or replace hard drive."},21 22    {"problem": "System Overheating",23     "conditions": ["system_shutdown", "fan_noise"],24     "solution": "Clean cooling fans and check thermal paste."},25 26    {"problem": "GPU/Display Failure",27     "conditions": ["black_screen", "system_crash"],28     "solution": "Check graphics card and update drivers."},29 30    # ===== NETWORK =====31    {"problem": "Internet Connection Failure",32     "conditions": ["no_connection", "wifi_problem"],33     "solution": "Restart router and check cables."},34 35    {"problem": "DNS Configuration Error",36     "conditions": ["dns_error", "website_not_loading"],37     "solution": "Change DNS to a public DNS server."},38 39    {"problem": "Network Cable Fault",40     "conditions": ["no_connection", "network_cable_unplugged"],41     "solution": "Reconnect or replace network cable."},42 43    {"problem": "Router Configuration Issue",44     "conditions": ["wifi_problem", "slow_internet"],45     "solution": "Reset router settings and update firmware."},46 47    {"problem": "Firewall Blocking Access",48     "conditions": ["website_not_loading", "firewall_warning"],49     "solution": "Check firewall settings and allow application."},50 51    # ===== SOFTWARE =====52    {"problem": "Software Installation Error",53     "conditions": ["cannot_install", "permission_denied"],54     "solution": "Run installer as administrator."},55 56    {"problem": "Operating System Corruption",57     "conditions": ["system_crash", "missing_files"],58     "solution": "Repair OS using recovery mode."},59 60    {"problem": "Low Disk Space",61     "conditions": ["cannot_install", "low_storage"],62     "solution": "Free up disk space."},63 64    {"problem": "Driver Corruption",65     "conditions": ["device_not_detected", "system_crash"],66     "solution": "Reinstall device drivers."},67 68    {"problem": "Update Failure",69     "conditions": ["update_failed", "random_restart"],70     "solution": "Rollback recent update."},71 72    # ===== PERIPHERALS =====73    {"problem": "Printer Offline",74     "conditions": ["printer_offline", "cannot_print"],75     "solution": "Reconnect printer and restart spooler service."},76 77    {"problem": "Paper Jam",78     "conditions": ["printer_error", "paper_jam"],79     "solution": "Remove jammed paper and reset printer."},80 81    {"problem": "Keyboard Failure",82     "conditions": ["keyboard_not_working", "device_not_detected"],83     "solution": "Reconnect keyboard or replace batteries."},84 85    {"problem": "Mouse Failure",86     "conditions": ["mouse_not_working", "device_not_detected"],87     "solution": "Try another USB port or replace mouse."},88 89    {"problem": "Audio Driver Issue",90     "conditions": ["no_sound", "audio_error"],91     "solution": "Update or reinstall audio drivers."},92 93    # ===== SECURITY =====94    {"problem": "Virus Infection",95     "conditions": ["popup_ads", "unknown_programs"],96     "solution": "Run full antivirus scan."},97 98    {"problem": "Account Lockout",99     "conditions": ["account_locked", "wrong_password"],100     "solution": "Reset password through administrator."},101 102    {"problem": "Phishing Attack",103     "conditions": ["suspicious_email", "credential_request"],104     "solution": "Do not click links and report to IT security."},105 106    {"problem": "Ransomware Infection",107     "conditions": ["files_encrypted", "ransom_message"],108     "solution": "Disconnect from network and contact IT immediately."},109 110    {"problem": "Unauthorized Access Attempt",111     "conditions": ["multiple_login_attempts", "security_alert"],112     "solution": "Enable two-factor authentication and change passwords."}113]114 115# ------------------------------116# SYMPTOM LIST (Working Memory Input)117# ------------------------------118 119symptoms = sorted({cond for rule in rules for cond in rule["conditions"]})120 121# ------------------------------122# INFERENCE ENGINE (Forward Chaining)123# ------------------------------124 125def infer(selected_symptoms):126 127    if not selected_symptoms:128        return "⚠️ Please select at least one symptom."129 130    best_match = None131    highest_score = 0132 133    for rule in rules:134        matched = [cond for cond in rule["conditions"] if cond in selected_symptoms]135        score = len(matched) / len(rule["conditions"])136 137        if score > highest_score:138            highest_score = score139            best_match = (rule["problem"], rule["solution"], matched, score)140 141    if best_match:142        problem, solution, matched, score = best_match143        confidence = round(score * 100, 2)144 145        return f"""146##  Diagnosis Result147 148**Problem Identified:** {problem}149 150**Confidence Level:** {confidence}%151 152**Matched Symptoms:** {matched}153 154###  Recommended Solution:155{solution}156"""157    else:158        return "No clear diagnosis found. Please contact IT support."159 160# ------------------------------161# GRADIO FRONTEND162# ------------------------------163 164with gr.Blocks(theme=gr.themes.Soft()) as demo:165 166    gr.Markdown("# Smart IT Helpdesk Expert System")167    gr.Markdown("Rule-Based Knowledge System using Forward Chaining (25 Production Rules)")168 169    symptom_selector = gr.CheckboxGroup(170        choices=symptoms,171        label="Select Observed Symptoms"172    )173 174    diagnose_btn = gr.Button(" Diagnose Issue", variant="primary")175    result_output = gr.Markdown()176 177    diagnose_btn.click(178        infer,179        inputs=symptom_selector,180        outputs=result_output181    )182 183if __name__ == "__main__":184    demo.launch()