Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
| 1 |
from transformers import pipeline
|
| 2 |
-
import gradio as gr # Import Gradio for
|
| 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.",
|
|
@@ -15,10 +18,19 @@ faq_responses = {
|
|
| 15 |
|
| 16 |
# Define the chatbot's response function
|
| 17 |
def faq_chatbot(user_input):
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# If no FAQ match, use the AI model to generate a response
|
| 24 |
conversation = chatbot(user_input, max_length=50, num_return_sequences=1)
|
|
@@ -26,12 +38,12 @@ def faq_chatbot(user_input):
|
|
| 26 |
|
| 27 |
# Create the Gradio interface
|
| 28 |
interface = gr.Interface(
|
| 29 |
-
fn=faq_chatbot, #
|
| 30 |
-
inputs=gr.Textbox(lines=2, placeholder="Ask me about
|
| 31 |
-
outputs="text", # Output
|
| 32 |
title="Student FAQ Chatbot",
|
| 33 |
-
description="Ask me
|
| 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 UI
|
| 3 |
|
| 4 |
# Load a text-generation model
|
| 5 |
chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
|
| 6 |
|
| 7 |
+
# Load the classification model
|
| 8 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 9 |
+
|
| 10 |
# Customize the bot's knowledge base with predefined responses
|
| 11 |
faq_responses = {
|
| 12 |
"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.",
|
|
|
|
| 18 |
|
| 19 |
# Define the chatbot's response function
|
| 20 |
def faq_chatbot(user_input):
|
| 21 |
+
# Classify user input based on predefined FAQ categories
|
| 22 |
+
classified_user_input = classifier(user_input, candidate_labels=list(faq_responses.keys()))
|
| 23 |
+
|
| 24 |
+
# Get the highest confidence score label
|
| 25 |
+
predicted_label = classified_user_input["labels"][0]
|
| 26 |
+
confidence_score = classified_user_input["scores"][0]
|
| 27 |
+
|
| 28 |
+
# Confidence threshold (adjust as needed)
|
| 29 |
+
threshold = 0.5
|
| 30 |
+
|
| 31 |
+
# If classification confidence is high, return the corresponding FAQ response
|
| 32 |
+
if confidence_score > threshold:
|
| 33 |
+
return faq_responses[predicted_label]
|
| 34 |
|
| 35 |
# If no FAQ match, use the AI model to generate a response
|
| 36 |
conversation = chatbot(user_input, max_length=50, num_return_sequences=1)
|
|
|
|
| 38 |
|
| 39 |
# Create the Gradio interface
|
| 40 |
interface = gr.Interface(
|
| 41 |
+
fn=faq_chatbot, # Function to process user input
|
| 42 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask me about study tips, resources, or time management..."), # Input field
|
| 43 |
+
outputs="text", # Output text
|
| 44 |
title="Student FAQ Chatbot",
|
| 45 |
+
description="Ask me study tips, time management strategies, or where to find good study resources!"
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# Launch the chatbot and make it accessible via a public Gradio link
|
| 49 |
interface.launch(share=True)
|