Upload rc_faq_bot.py
Browse files- rc_faq_bot.py +48 -0
rc_faq_bot.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from difflib import get_close_matches
|
| 3 |
+
|
| 4 |
+
# FAQ Data
|
| 5 |
+
faq_data = {
|
| 6 |
+
"questions": [
|
| 7 |
+
{
|
| 8 |
+
"question": "What is Resilient Coders?",
|
| 9 |
+
"answer": "Resilient Coders is a nonprofit that trains young people of color for careers in tech."
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"question": "Is the bootcamp free?",
|
| 13 |
+
"answer": "Yes, it is completely free and participants get a stipend."
|
| 14 |
+
},
|
| 15 |
+
{
|
| 16 |
+
"question": "How long is the program?",
|
| 17 |
+
"answer": "It typically lasts about 14–20 weeks."
|
| 18 |
+
}
|
| 19 |
+
]
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
# Extract all questions for fuzzy matching
|
| 23 |
+
questions_list = [q["question"] for q in faq_data["questions"]]
|
| 24 |
+
|
| 25 |
+
# Chatbot function
|
| 26 |
+
def chatbot(user_input):
|
| 27 |
+
if not user_input.strip():
|
| 28 |
+
return "Please ask a question."
|
| 29 |
+
|
| 30 |
+
match = get_close_matches(user_input, questions_list, n=1, cutoff=0.4)
|
| 31 |
+
if match:
|
| 32 |
+
for item in faq_data["questions"]:
|
| 33 |
+
if item["question"] == match[0]:
|
| 34 |
+
return item["answer"]
|
| 35 |
+
return "Sorry, I only answer questions about Resilient Coders."
|
| 36 |
+
|
| 37 |
+
# Gradio Interface
|
| 38 |
+
demo = gr.Interface(
|
| 39 |
+
fn=chatbot,
|
| 40 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask about Resilient Coders..."),
|
| 41 |
+
outputs="text",
|
| 42 |
+
title="Resilient Coders Chatbot",
|
| 43 |
+
description="Ask me anything about Resilient Coders!"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Run the app
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
demo.launch()
|