|
import os |
|
import json |
|
import gradio as gr |
|
from transformers import pipeline |
|
|
|
|
|
def load_dataset(folder_path): |
|
data = [] |
|
for filename in os.listdir(folder_path): |
|
if filename.endswith(".json"): |
|
with open(os.path.join(folder_path, filename), "r") as file: |
|
data.extend(json.load(file)) |
|
return data |
|
|
|
|
|
model = pipeline("question-answering", model="deepset/roberta-base-squad2") |
|
|
|
|
|
def query_chatbot(query, name, email, contact): |
|
|
|
dataset = load_dataset("dataset/") |
|
|
|
|
|
responses = [] |
|
for entry in dataset: |
|
context = entry.get("content", "") |
|
if query.lower() in context.lower(): |
|
response = model(question=query, context=context) |
|
responses.append(response["answer"]) |
|
|
|
|
|
response = ( |
|
f"Hello {name}!\n\n" |
|
f"Based on your query: '{query}', here are some relevant insights:\n\n" |
|
+ "\n".join(responses)[:3] |
|
) |
|
|
|
|
|
profile = { |
|
"name": name, |
|
"email": email, |
|
"contact": contact, |
|
"query": query, |
|
"responses": responses, |
|
} |
|
|
|
|
|
return response |
|
|
|
|
|
def chatbot_ui(): |
|
with gr.Blocks() as app: |
|
gr.Markdown("# π Education Consultant Chatbot") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
query_input = gr.Textbox(label="Your Query", placeholder="Ask about courses, visas, or programs...") |
|
name_input = gr.Textbox(label="Name", placeholder="Your Full Name") |
|
email_input = gr.Textbox(label="Email", placeholder="Your Email Address") |
|
contact_input = gr.Textbox(label="Contact (Optional)", placeholder="Your Contact Number") |
|
submit_btn = gr.Button("Submit") |
|
|
|
with gr.Column(): |
|
output_text = gr.Textbox(label="Chatbot Response") |
|
|
|
submit_btn.click( |
|
query_chatbot, |
|
inputs=[query_input, name_input, email_input, contact_input], |
|
outputs=output_text, |
|
) |
|
|
|
return app |
|
|
|
|
|
if __name__ == "__main__": |
|
chatbot = chatbot_ui() |
|
chatbot.launch() |
|
|