Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,58 @@
|
|
1 |
import faiss
|
2 |
import numpy as np
|
|
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
FAISS_PATH = "asa_faiss.index"
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
query_vector = np.random.rand(1, index.d).astype('float32')
|
10 |
|
11 |
-
# Search FAISS index
|
12 |
-
D, I = index.search(query_vector, k=1) # k=1 means get 1 nearest neighbor
|
13 |
|
14 |
-
|
|
|
|
|
|
1 |
import faiss
|
2 |
import numpy as np
|
3 |
+
import torch
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
+
import gradio as gr
|
6 |
|
7 |
+
# Set paths
|
8 |
FAISS_PATH = "asa_faiss.index"
|
9 |
+
DATASET_PATH = "responses.txt" # Ensure this file contains indexed responses
|
10 |
+
|
11 |
+
# Load FAISS index
|
12 |
+
print(f"Loading FAISS index from {FAISS_PATH}...")
|
13 |
+
faiss_index = faiss.read_index(FAISS_PATH)
|
14 |
+
print("✅ FAISS index loaded successfully!")
|
15 |
+
|
16 |
+
# Load dataset responses
|
17 |
+
with open(DATASET_PATH, "r", encoding="utf-8") as f:
|
18 |
+
dataset = f.readlines()
|
19 |
+
print("✅ Responses dataset loaded!")
|
20 |
+
|
21 |
+
# Load model & tokenizer (Ensure model path is correct)
|
22 |
+
MODEL_NAME = "Futuresony/my_model" # Change this if using a local model
|
23 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
24 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
25 |
+
|
26 |
+
# Set FAISS threshold (Adjust based on FAISS distance values)
|
27 |
+
THRESHOLD = 100 # Lower threshold to improve response accuracy
|
28 |
+
|
29 |
+
|
30 |
+
def embed(text):
|
31 |
+
"""Convert text to FAISS-compatible vector (Ensure same embeddings as FAISS training)."""
|
32 |
+
tokens = tokenizer.encode(text, add_special_tokens=True)
|
33 |
+
return np.array(tokens, dtype=np.float32).reshape(1, -1)
|
34 |
+
|
35 |
+
|
36 |
+
def chatbot_response(user_query):
|
37 |
+
"""Fetches response from FAISS or falls back to the model."""
|
38 |
+
query_vector = embed(user_query) # Convert input to vector
|
39 |
+
D, I = faiss_index.search(query_vector, k=1) # Search FAISS
|
40 |
+
|
41 |
+
print(f"Closest FAISS match index: {I[0][0]}, Distance: {D[0][0]}") # Debugging info
|
42 |
+
|
43 |
+
if D[0][0] < THRESHOLD: # Check if FAISS result is relevant
|
44 |
+
response = dataset[I[0][0]].strip() # Fetch matched response
|
45 |
+
print("✅ FAISS response used!")
|
46 |
+
else:
|
47 |
+
# Fallback to model-generated response
|
48 |
+
print("⚠️ FAISS match too weak, using model instead.")
|
49 |
+
inputs = tokenizer(user_query, return_tensors="pt")
|
50 |
+
outputs = model.generate(**inputs, max_new_tokens=150)
|
51 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
52 |
|
53 |
+
return response
|
|
|
54 |
|
|
|
|
|
55 |
|
56 |
+
# Gradio UI
|
57 |
+
iface = gr.Interface(fn=chatbot_response, inputs="text", outputs="text", title="ASA Microfinance Chatbot")
|
58 |
+
iface.launch()
|