File size: 3,647 Bytes
3a46037
d2ee098
 
3a46037
d2ee098
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a46037
e2cf7d8
d2ee098
f3ce746
 
d2ee098
3a46037
d2ee098
 
 
 
 
e2cf7d8
d2ee098
 
 
3a46037
d2ee098
 
 
3a46037
d2ee098
 
 
 
3a46037
e2cf7d8
 
 
 
 
 
 
d2ee098
3a46037
d2ee098
3a46037
d2ee098
 
 
 
e2cf7d8
d2ee098
3a46037
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import gradio as gr
from fuzzywuzzy import process
from transformers import pipeline

# Our dictionary of 20 dental terms
dental_terms = {
    "cavity": "A cavity is a hole in a tooth caused by decay.",
    "gingivitis": "Gingivitis is the inflammation of the gums, often caused by plaque buildup.",
    "implant": "A dental implant is a surgical component that interfaces with the jawbone to support a dental prosthesis.",
    "orthodontics": "Orthodontics is a branch of dentistry that corrects teeth and jaw alignment issues.",
    "plaque": "Plaque is a sticky, colorless film of bacteria that forms on teeth.",
    "enamel": "Enamel is the hard, outer surface layer of your teeth that protects against decay.",
    "braces": "Braces are orthodontic devices used to straighten teeth and correct bite issues.",
    "root canal": "A root canal is a treatment to repair and save a badly damaged or infected tooth.",
    "crown": "A crown is a dental cap placed over a tooth to restore its shape, size, and strength.",
    "veneers": "Veneers are thin shells placed over the front of teeth to improve appearance.",
    "halitosis": "Halitosis is chronic bad breath caused by bacteria or other factors.",
    "periodontitis": "Periodontitis is a serious gum infection that damages gums and can destroy the jawbone.",
    "denture": "Dentures are removable appliances that replace missing teeth and surrounding tissues.",
    "bridge": "A dental bridge is a fixed prosthetic device that replaces missing teeth.",
    "tartar": "Tartar is hardened plaque that forms on teeth and can only be removed by a dentist.",
    "x-ray": "A dental x-ray is an imaging technique used to view the inside of teeth and surrounding tissues.",
    "flossing": "Flossing is the process of cleaning between your teeth with dental floss.",
    "sealant": "A sealant is a protective coating applied to teeth to prevent decay.",
    "bitewing": "A bitewing is a type of dental x-ray that shows the upper and lower back teeth.",
    "occlusion": "Occlusion refers to the alignment and contact between teeth when the jaws close."
}

# Set up a gpt2-large pipeline
generation_pipeline = pipeline(
    "text-generation",
    model="gpt2-large"
)

def chatbot_response(message, history):
    """
    Hybrid response logic:
    1) Check if user input matches a known dental term (exactly or via fuzzy matching).
    2) If found or close match, return the definition from our dictionary.
    3) Otherwise, use GPT-2 to generate an open-ended response.
    """
    # Normalize user input to lowercase for simpler matching
    user_input_lower = message.lower()

    # 1) Exact match check
    if user_input_lower in dental_terms:
        return dental_terms[user_input_lower]

    # 2) Fuzzy match check
    closest_match, score = process.extractOne(user_input_lower, dental_terms.keys())
    if score >= 80:
        return f"Did you mean '{closest_match}'? {dental_terms[closest_match]}"

    # 3) If no match or fuzzy match is too low, use GPT-2 for generation
    try:
        result = generation_pipeline(message, max_length=100, num_return_sequences=1)
        generated_text = result[0]["generated_text"]
    except Exception as e:
        generated_text = f"Error generating response: {str(e)}"

    return generated_text

# Gradio chat interface
demo = gr.ChatInterface(
    fn=chatbot_response,
    title="Hybrid Dental Terminology Chatbot",
    description=(
        "Enter a dental term to get its definition (20 known terms). "
        "If the term isn't recognized, GPT-2 will respond with a generated message."
    )
)

if __name__ == "__main__":
    demo.launch()