Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,86 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
for
|
| 21 |
-
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from fuzzywuzzy import process
|
| 3 |
+
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# 1) Our 20 dental terms:
|
| 6 |
+
dental_terms = {
|
| 7 |
+
"cavity": "A cavity is a hole in a tooth caused by decay.",
|
| 8 |
+
"gingivitis": "Gingivitis is the inflammation of the gums, often caused by plaque buildup.",
|
| 9 |
+
"implant": "A dental implant is a surgical component that interfaces with the jawbone to support a dental prosthesis.",
|
| 10 |
+
"orthodontics": "Orthodontics is a branch of dentistry that corrects teeth and jaw alignment issues.",
|
| 11 |
+
"plaque": "Plaque is a sticky, colorless film of bacteria that forms on teeth.",
|
| 12 |
+
"enamel": "Enamel is the hard, outer surface layer of your teeth that protects against decay.",
|
| 13 |
+
"braces": "Braces are orthodontic devices used to straighten teeth and correct bite issues.",
|
| 14 |
+
"root canal": "A root canal is a treatment to repair and save a badly damaged or infected tooth.",
|
| 15 |
+
"crown": "A crown is a dental cap placed over a tooth to restore its shape, size, and strength.",
|
| 16 |
+
"veneers": "Veneers are thin shells placed over the front of teeth to improve appearance.",
|
| 17 |
+
"halitosis": "Halitosis is chronic bad breath caused by bacteria or other factors.",
|
| 18 |
+
"periodontitis": "Periodontitis is a serious gum infection that damages gums and can destroy the jawbone.",
|
| 19 |
+
"denture": "Dentures are removable appliances that replace missing teeth and surrounding tissues.",
|
| 20 |
+
"bridge": "A dental bridge is a fixed prosthetic device that replaces missing teeth.",
|
| 21 |
+
"tartar": "Tartar is hardened plaque that forms on teeth and can only be removed by a dentist.",
|
| 22 |
+
"x-ray": "A dental x-ray is an imaging technique used to view the inside of teeth and surrounding tissues.",
|
| 23 |
+
"flossing": "Flossing is the process of cleaning between your teeth with dental floss.",
|
| 24 |
+
"sealant": "A sealant is a protective coating applied to teeth to prevent decay.",
|
| 25 |
+
"bitewing": "A bitewing is a type of dental x-ray that shows the upper and lower back teeth.",
|
| 26 |
+
"occlusion": "Occlusion refers to the alignment and contact between teeth when the jaws close."
|
| 27 |
+
}
|
| 28 |
|
| 29 |
+
# 2) Set up a Transformer-based text generation pipeline
|
| 30 |
+
# (You can choose any model on Hugging Face; "gpt2" is just an example.)
|
| 31 |
+
generation_pipeline = pipeline("text-generation", model="gpt2")
|
| 32 |
|
| 33 |
+
def chatbot_response(message, history):
|
| 34 |
+
"""
|
| 35 |
+
A hybrid response function:
|
| 36 |
+
- Check if the user query matches a known dental term (direct or fuzzy).
|
| 37 |
+
- If not matched, use a transformer model to generate an open-ended response.
|
| 38 |
+
"""
|
| 39 |
+
print(f"User Input: {message}")
|
| 40 |
+
print(f"Chat History: {history}")
|
|
|
|
| 41 |
|
| 42 |
+
# Lowercase for simpler matching
|
| 43 |
+
input_lower = message.lower()
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# 1) Check for exact match
|
| 46 |
+
if input_lower in dental_terms:
|
| 47 |
+
response = dental_terms[input_lower]
|
| 48 |
+
print(f"Exact Match Response: {response}")
|
| 49 |
+
return response
|
| 50 |
|
| 51 |
+
# 2) Fuzzy matching for approximate matches
|
| 52 |
+
closest_match, score = process.extractOne(input_lower, dental_terms.keys())
|
| 53 |
+
print(f"Closest Match: {closest_match}, Score: {score}")
|
| 54 |
|
| 55 |
+
if score >= 80:
|
| 56 |
+
# We suspect the user intended a known term
|
| 57 |
+
return f"Did you mean '{closest_match}'? {dental_terms[closest_match]}"
|
| 58 |
+
else:
|
| 59 |
+
# 3) If no good match, let transformer-based AI handle it
|
| 60 |
+
# We'll generate a short text response.
|
| 61 |
+
generated = generation_pipeline(
|
| 62 |
+
message,
|
| 63 |
+
max_length=100, # adjust as needed
|
| 64 |
+
num_return_sequences=1,
|
| 65 |
+
do_sample=True,
|
| 66 |
+
top_p=0.9,
|
| 67 |
+
top_k=50
|
| 68 |
+
)
|
| 69 |
+
ai_response = generated[0]["generated_text"]
|
| 70 |
+
# Optionally, you might want to trim the prompt out of the generated text,
|
| 71 |
+
# but here we'll just return it as is.
|
| 72 |
+
print(f"Transformer-based response: {ai_response}")
|
| 73 |
+
return ai_response
|
| 74 |
|
| 75 |
+
# 3) Gradio ChatInterface (or you can use gr.Interface)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
demo = gr.ChatInterface(
|
| 77 |
+
fn=chatbot_response,
|
| 78 |
+
title="Hybrid Dental Terminology Chatbot",
|
| 79 |
+
description=(
|
| 80 |
+
"Enter a dental term to get its definition (20 known terms). "
|
| 81 |
+
"If the term isn't recognized, a transformer-based model will respond."
|
| 82 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
)
|
| 84 |
|
|
|
|
| 85 |
if __name__ == "__main__":
|
| 86 |
demo.launch()
|