Spaces:
Sleeping
Sleeping
uploaded
Browse files- app.py +38 -70
- chatbot.pkl +3 -0
app.py
CHANGED
|
@@ -1,70 +1,38 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
):
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
type="messages",
|
| 49 |
-
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
with gr.Blocks() as demo:
|
| 64 |
-
with gr.Sidebar():
|
| 65 |
-
gr.LoginButton()
|
| 66 |
-
chatbot.render()
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
if __name__ == "__main__":
|
| 70 |
-
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# --- Load chatbot data ---
|
| 6 |
+
data = joblib.load("chatbot.pkl")
|
| 7 |
+
questions = data["questions"]
|
| 8 |
+
answers = data["answers"]
|
| 9 |
+
embeddings = np.array(data["embeddings"])
|
| 10 |
+
model = data["model"]
|
| 11 |
+
|
| 12 |
+
def chatbot_reply(user_input, threshold=0.4):
|
| 13 |
+
if not user_input.strip():
|
| 14 |
+
return "Please ask a question."
|
| 15 |
+
|
| 16 |
+
# Encode user input
|
| 17 |
+
user_emb = model.encode([user_input])[0]
|
| 18 |
+
# Compute cosine similarity
|
| 19 |
+
cos_scores = np.dot(embeddings, user_emb) / (np.linalg.norm(embeddings, axis=1) * np.linalg.norm(user_emb))
|
| 20 |
+
idx = np.argmax(cos_scores)
|
| 21 |
+
score = cos_scores[idx]
|
| 22 |
+
|
| 23 |
+
if score < threshold:
|
| 24 |
+
return "Sorry, I am not able to answer that."
|
| 25 |
+
|
| 26 |
+
return answers[idx]
|
| 27 |
+
|
| 28 |
+
# --- Gradio UI ---
|
| 29 |
+
chatbot = gr.ChatInterface(
|
| 30 |
+
fn=lambda msg, hist: (hist + [(msg, chatbot_reply(msg))], hist + [(msg, chatbot_reply(msg))]),
|
| 31 |
+
title="Sajid's FAQ Chatbot 🤖",
|
| 32 |
+
description="A chatbot trained on my custom FAQ data using embeddings.",
|
| 33 |
+
theme="soft"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
chatbot.launch()
|
| 38 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
chatbot.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ddbd04f2ce3492b01ad090c0d680612ebf4b3bf66a7a2105d5150bdb2c51c5f5
|
| 3 |
+
size 1182871
|