Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,77 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 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 |
+
# Our dictionary of 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 |
+
# Set up a Flan-T5 pipeline (instruction-tuned T5)
|
| 30 |
+
# We'll use the "text2text-generation" pipeline for T5-like models
|
| 31 |
+
# Model: google/flan-t5-base
|
| 32 |
+
generation_pipeline = pipeline(
|
| 33 |
+
"text2text-generation",
|
| 34 |
+
model="google/flan-t5-base"
|
| 35 |
+
)
|
| 36 |
|
| 37 |
+
def chatbot_response(message, history):
|
| 38 |
+
"""
|
| 39 |
+
Hybrid response logic:
|
| 40 |
+
1) Check if user input matches a known dental term (exactly or via fuzzy matching).
|
| 41 |
+
2) If found or close match, return the definition from our dictionary.
|
| 42 |
+
3) Otherwise, use Flan-T5 to generate an open-ended response.
|
| 43 |
+
"""
|
| 44 |
+
print(f"User Input: {message}")
|
| 45 |
+
print(f"Chat History: {history}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# Normalize user input to lowercase for simpler matching
|
| 48 |
+
user_input_lower = message.lower()
|
| 49 |
|
| 50 |
+
# 1) Exact match check
|
| 51 |
+
if user_input_lower in dental_terms:
|
| 52 |
+
return dental_terms[user_input_lower]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# 2) Fuzzy match check
|
| 55 |
+
closest_match, score = process.extractOne(user_input_lower, dental_terms.keys())
|
| 56 |
+
if score >= 80:
|
| 57 |
+
return f"Did you mean '{closest_match}'? {dental_terms[closest_match]}"
|
| 58 |
|
| 59 |
+
# 3) If no match or fuzzy match is too low, use Flan-T5 for generation
|
| 60 |
+
# We'll prompt it directly with the user's message.
|
| 61 |
+
# Adjust parameters as desired for creativity, length, etc.
|
| 62 |
+
result = generation_pipeline(message, max_length=100, num_return_sequences=1)
|
| 63 |
+
generated_text = result[0]["generated_text"]
|
| 64 |
+
return generated_text
|
| 65 |
|
| 66 |
+
# Gradio chat interface
|
|
|
|
|
|
|
| 67 |
demo = gr.ChatInterface(
|
| 68 |
+
fn=chatbot_response,
|
| 69 |
+
title="Hybrid Dental Terminology Chatbot",
|
| 70 |
+
description=(
|
| 71 |
+
"Enter a dental term to get its definition (20 known terms). "
|
| 72 |
+
"If the term isn't recognized, Flan-T5 will respond."
|
| 73 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
)
|
| 75 |
|
|
|
|
| 76 |
if __name__ == "__main__":
|
| 77 |
demo.launch()
|