Spaces:
Sleeping
Sleeping
Commit
·
b84ccee
1
Parent(s):
61d37c6
updated
Browse files
app.py
CHANGED
|
@@ -1,21 +1,38 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Keep conversation history
|
| 6 |
history = []
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
# Gradio
|
| 16 |
-
demo = gr.
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
+
import joblib
|
| 4 |
+
from src.preprocess import clean_text
|
| 5 |
+
|
| 6 |
+
# Load trained model and responses
|
| 7 |
+
model = joblib.load("models/lms_chatbot.joblib")
|
| 8 |
+
responses = joblib.load("models/responses.joblib")
|
| 9 |
|
| 10 |
# Keep conversation history
|
| 11 |
history = []
|
| 12 |
|
| 13 |
+
def chatbot_response(user_input):
|
| 14 |
+
clean_input = clean_text(user_input)
|
| 15 |
+
tag = model.predict([clean_input])[0]
|
| 16 |
+
bot_reply = responses.get(tag, ["Sorry, I don't understand."])[0]
|
| 17 |
+
|
| 18 |
+
# Append to history
|
| 19 |
+
history.append((user_input, bot_reply))
|
| 20 |
+
|
| 21 |
+
# Format chat history nicely
|
| 22 |
+
formatted_history = ""
|
| 23 |
+
for user_msg, bot_msg in history:
|
| 24 |
+
formatted_history += f"**You:** {user_msg}\n**Bot:** {bot_msg}\n\n"
|
| 25 |
+
|
| 26 |
+
return formatted_history
|
| 27 |
|
| 28 |
+
# Gradio Interface
|
| 29 |
+
demo = gr.Interface(
|
| 30 |
+
fn=chatbot_response,
|
| 31 |
+
inputs=gr.Textbox(lines=2, placeholder="Type your message here..."),
|
| 32 |
+
outputs=gr.Markdown(),
|
| 33 |
+
title="LMS Chatbot",
|
| 34 |
+
description="Ask anything about LMS and get automated responses."
|
| 35 |
+
)
|
| 36 |
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
demo.launch()
|