Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,43 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
chatbot = pipeline("text-generation", model="google/flan-t5-small")
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def chat_fn(message):
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
return response[0]["generated_text"]
|
| 10 |
|
|
|
|
| 11 |
iface = gr.Interface(
|
| 12 |
fn=chat_fn,
|
| 13 |
inputs="text",
|
| 14 |
outputs="text",
|
| 15 |
title="M Tech Calibration Lab Chatbot",
|
| 16 |
-
description="
|
| 17 |
)
|
| 18 |
|
| 19 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
import faiss
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
+
# Load a small, fast model
|
| 8 |
chatbot = pipeline("text-generation", model="google/flan-t5-small")
|
| 9 |
|
| 10 |
+
# Load embedding model
|
| 11 |
+
embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
| 12 |
+
|
| 13 |
+
# Load FAISS index and chunks
|
| 14 |
+
index = faiss.read_index("data/faiss_index")
|
| 15 |
+
with open("data/chunks.txt", "r", encoding="utf-8") as f:
|
| 16 |
+
chunks = f.readlines()
|
| 17 |
+
|
| 18 |
def chat_fn(message):
|
| 19 |
+
# Embed user query
|
| 20 |
+
query_vec = embedder.encode([message])
|
| 21 |
+
query_vec = np.array(query_vec)
|
| 22 |
+
|
| 23 |
+
# Search FAISS for most relevant chunk
|
| 24 |
+
D, I = index.search(query_vec, k=1) # top 1 result
|
| 25 |
+
context = chunks[I[0][0]].strip()
|
| 26 |
+
|
| 27 |
+
# Build prompt with context
|
| 28 |
+
prompt = f"You are an assistant for M Tech Calibration Lab LLP, Faridabad, Haryana. Use this context from the website: {context}. Question: {message}"
|
| 29 |
+
|
| 30 |
+
# Generate response
|
| 31 |
+
response = chatbot(prompt, max_length=150, do_sample=False)
|
| 32 |
return response[0]["generated_text"]
|
| 33 |
|
| 34 |
+
# Gradio interface
|
| 35 |
iface = gr.Interface(
|
| 36 |
fn=chat_fn,
|
| 37 |
inputs="text",
|
| 38 |
outputs="text",
|
| 39 |
title="M Tech Calibration Lab Chatbot",
|
| 40 |
+
description="Answers based on mtechcalibration.in content"
|
| 41 |
)
|
| 42 |
|
| 43 |
iface.launch()
|