Aichatbot12321 commited on
Commit
6b46741
·
verified ·
1 Parent(s): eb10f3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -3
app.py CHANGED
@@ -1,19 +1,43 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
 
 
3
 
4
- # Use a small, fast model
5
  chatbot = pipeline("text-generation", model="google/flan-t5-small")
6
 
 
 
 
 
 
 
 
 
7
  def chat_fn(message):
8
- response = chatbot(message, max_length=100, do_sample=False)
 
 
 
 
 
 
 
 
 
 
 
 
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="Fast chatbot for calibration services and instruments."
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()