Spaces:
Sleeping
Sleeping
File size: 1,275 Bytes
bd2be4c 6b46741 bd2be4c 6b46741 a64db20 bd2be4c 6b46741 bd2be4c 6b46741 bd2be4c 6b46741 bd2be4c 4745a07 6b46741 bd2be4c | 1 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 40 41 42 43 44 | import gradio as gr
from transformers import pipeline
from sentence_transformers import SentenceTransformer
import faiss
import numpy as np
# Load a small, fast model
chatbot = pipeline("text-generation", model="google/flan-t5-small")
# Load embedding model
embedder = SentenceTransformer("all-MiniLM-L6-v2")
# Load FAISS index and chunks
index = faiss.read_index("data/faiss_index")
with open("data/chunks.txt", "r", encoding="utf-8") as f:
chunks = f.readlines()
def chat_fn(message):
# Embed user query
query_vec = embedder.encode([message])
query_vec = np.array(query_vec)
# Search FAISS for most relevant chunk
D, I = index.search(query_vec, k=1) # top 1 result
context = chunks[I[0][0]].strip()
# Build prompt with context
prompt = f"You are an assistant for M Tech Calibration Lab LLP, Faridabad, Haryana. Use this context from the website: {context}. Question: {message}"
# Generate response
response = chatbot(prompt, max_length=150, do_sample=False)
return response[0]["generated_text"]
# Gradio interface
iface = gr.Interface(
fn=chat_fn,
inputs="text",
outputs="text",
title="M Tech Calibration Lab Chatbot",
description="Answers based on mtechcalibration.in content"
)
iface.launch()
|