Spaces:
Sleeping
Sleeping
Download app.py from mtechcalibration/README: direct link, hf CLI and curl.
- Browser
- Download file 1.28 kB
-
https://huggingface.co/spaces/mtechcalibration/README/resolve/main/app.py
- Command line
-
hf download hf://spaces/mtechcalibration/README/app.py
-
curl -L -o app.py https://huggingface.co/spaces/mtechcalibration/README/resolve/main/app.py
1.28 kB
| 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() | |