Update app.py
Browse files1 unmodified line234567818 unmodified lines2728293031323319 unmodified lines53545556575859606162633 unmodified lines6768697071727374757677787980818283841 unmodified linefrom fuzzywuzzy import processfrom transformers import pipeline# 1) Our 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.",18 unmodified lines}# 2) Set up a Transformer-based text generation pipeline# (You can choose any model on Hugging Face; "gpt2" is just an example.)generation_pipeline = pipeline("text-generation", model="gpt2")def chatbot_response(message, history):19 unmodified linesprint(f"Closest Match: {closest_match}, Score: {score}")if score >= 80:# We suspect the user intended a known termreturn f"Did you mean '{closest_match}'? {dental_terms[closest_match]}"else:# 3) If no good match, let transformer-based AI handle it# We'll generate a short text response.generated = generation_pipeline(message,max_length=100, # adjust as needed3 unmodified linestop_k=50)ai_response = generated[0]["generated_text"]# Optionally, you might want to trim the prompt out of the generated text,# but here we'll just return it as is.print(f"Transformer-based response: {ai_response}")return ai_response# 3) Gradio ChatInterface (or you can use gr.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, a transformer-based model will respond."))1 unmodified line234567818 unmodified lines27282930313219 unmodified lines52535455565758596061623 unmodified lines666768697071727374757677787980811 unmodified linefrom fuzzywuzzy import processfrom 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.",18 unmodified lines}# 2) Set up a Transformer-based text generation pipelinegeneration_pipeline = pipeline("text-generation", model="gpt2")def chatbot_response(message, history):19 unmodified linesprint(f"Closest Match: {closest_match}, Score: {score}")if score >= 80:# Suspect the user intended a known termreturn 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 needed3 unmodified linestop_k=50)ai_response = generated[0]["generated_text"]print(f"Transformer-based response: {ai_response}")return ai_response# 3) Gradio ChatInterfacedemo = 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 :) "))
app.py+5-8