WSLINMSAI's picture
Update app.py
2be3aa4 verified
import gradio as gr
from fuzzywuzzy import process
from transformers import pipeline
# 1) 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."
}
# 2) Set up a Transformer-based text generation pipeline
generation_pipeline = pipeline("text-generation", model="gpt2")
def chatbot_response(message, history):
"""
A hybrid response function:
- Check if the user query matches a known dental term (direct or fuzzy).
- If not matched, use a transformer model to generate an open-ended response.
"""
print(f"User Input: {message}")
print(f"Chat History: {history}")
# Lowercase for simpler matching
input_lower = message.lower()
# 1) Check for exact match
if input_lower in dental_terms:
response = dental_terms[input_lower]
print(f"Exact Match Response: {response}")
return response
# 2) Fuzzy matching for approximate matches
closest_match, score = process.extractOne(input_lower, dental_terms.keys())
print(f"Closest Match: {closest_match}, Score: {score}")
if score >= 80:
# Suspect the user intended a known term
return f"Did you mean '{closest_match}'? {dental_terms[closest_match]}"
else:
# 3) If no good match, let transformer-based AI handle it
# Generate a short text response.
generated = generation_pipeline(
message,
max_length=100, # adjust as needed
num_return_sequences=1,
do_sample=True,
top_p=0.9,
top_k=50
)
ai_response = generated[0]["generated_text"]
print(f"Transformer-based response: {ai_response}")
return ai_response
# 3) Gradio ChatInterface
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, a transformer-based model will respond :) "
)
)
if __name__ == "__main__":
demo.launch()