Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,38 @@
|
|
| 1 |
from transformers import pipeline
|
| 2 |
-
import gradio as gr
|
|
|
|
| 3 |
|
| 4 |
# Load a text-generation model
|
| 5 |
chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
# Customize the bot's knowledge base with predefined responses
|
| 8 |
faq_responses = {
|
| 9 |
"study tips": "Here are some study tips: 1) Break your study sessions into 25-minute chunks (Pomodoro Technique). 2) Test yourself frequently. 3) Stay organized using planners or apps like Notion or Todoist.",
|
| 10 |
-
"resources for studying": "You can find free study resources on websites like Khan Academy, Coursera, and edX. For research papers, check Google Scholar.",
|
| 11 |
-
"how to focus": "To improve focus, try studying in a quiet place, remove distractions like your phone, and use apps like Forest or Focus@Will.",
|
| 12 |
-
"time management tips": "Start by creating a to-do list each morning. Prioritize tasks using methods like Eisenhower Matrix and allocate specific time blocks for each task.",
|
| 13 |
-
"how to avoid procrastination": "Break tasks into smaller steps, set deadlines, and reward yourself after completing milestones. Tools like Trello can help you stay organized."
|
| 14 |
-
}
|
| 15 |
|
| 16 |
# Define the chatbot's response function
|
| 17 |
def faq_chatbot(user_input):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Check if the user's input matches any FAQ keywords
|
| 19 |
-
for key, response in faq_responses.items():
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
conversation = chatbot(user_input, max_length=50, num_return_sequences=1)
|
| 25 |
-
return conversation[0]['generated_text']
|
| 26 |
-
|
| 27 |
-
# Create the Gradio interface
|
| 28 |
-
interface = gr.Interface(
|
| 29 |
-
fn=faq_chatbot, # The function to handle user input
|
| 30 |
-
inputs=gr.Textbox(lines=2, placeholder="Ask me about studying tips or resources..."), # Input text box
|
| 31 |
-
outputs="text", # Output as text
|
| 32 |
-
title="Student FAQ Chatbot",
|
| 33 |
-
description="Ask me for study tips, time management advice, or about resources to help with your studies!"
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
-
# Launch the chatbot and make it public
|
| 37 |
-
interface.launch(share=True)
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
+
import gradio as gr # Import Gradio for the interface
|
| 3 |
+
|
| 4 |
|
| 5 |
# Load a text-generation model
|
| 6 |
chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
|
| 7 |
|
| 8 |
+
# Load the classification model
|
| 9 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 10 |
+
|
| 11 |
# Customize the bot's knowledge base with predefined responses
|
| 12 |
faq_responses = {
|
| 13 |
"study tips": "Here are some study tips: 1) Break your study sessions into 25-minute chunks (Pomodoro Technique). 2) Test yourself frequently. 3) Stay organized using planners or apps like Notion or Todoist.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Define the chatbot's response function
|
| 16 |
def faq_chatbot(user_input):
|
| 17 |
+
# Classify the user input by passing the FAQ keywords as labels
|
| 18 |
+
classified_user_input = classifier(user_input, candidate_labels=list(faq_responses.keys()))
|
| 19 |
+
|
| 20 |
+
# Get the highest confidence score label, ie. the most likely of the FAQ
|
| 21 |
+
predicted_label = classified_user_input["labels"][0]
|
| 22 |
+
confidence_score = classified_user_input["scores"][0]
|
| 23 |
+
|
| 24 |
+
# Confidence threshold (adjust if needed)
|
| 25 |
+
threshold = 0.5
|
| 26 |
+
|
| 27 |
+
# If the classification confidence is high, return the corresponding FAQ response
|
| 28 |
+
if confidence_score > threshold:
|
| 29 |
+
return faq_responses[predicted_label]
|
| 30 |
+
|
| 31 |
+
|
| 32 |
# Check if the user's input matches any FAQ keywords
|
| 33 |
+
# for key, response in faq_responses.items():
|
| 34 |
+
# if key in user_input.lower():
|
| 35 |
+
# return response
|
| 36 |
|
| 37 |
+
# If no FAQ match, use the AI model to generate a response
|
| 38 |
conversation = chatbot(user_input, max_length=50, num_return_sequences=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|