Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,77 @@
|
|
|
|
1 |
import faiss
|
2 |
import numpy as np
|
3 |
import torch
|
4 |
-
from sentence_transformers import SentenceTransformer
|
5 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
6 |
import gradio as gr
|
7 |
-
import
|
8 |
from huggingface_hub import hf_hub_download
|
9 |
|
10 |
# πΉ Hugging Face Credentials
|
11 |
-
HF_REPO = "Futuresony/
|
12 |
-
HF_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
|
13 |
|
14 |
-
# πΉ
|
15 |
FAISS_PATH = "asa_faiss.index"
|
16 |
-
DATASET_PATH = "responses.txt"
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
print("β
Responses dataset loaded!")
|
27 |
|
28 |
-
#
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
model = AutoModelForCausalLM.from_pretrained(HF_REPO, token=HF_TOKEN)
|
|
|
31 |
|
32 |
-
# πΉ
|
33 |
-
|
34 |
-
|
35 |
-
# πΉ Adjust FAISS Threshold
|
36 |
-
THRESHOLD = 100 # Adjust based on actual FAISS distances
|
37 |
-
|
38 |
|
39 |
def embed(text):
|
40 |
-
"""Convert text
|
41 |
-
|
42 |
-
|
43 |
|
44 |
def chatbot_response(user_query):
|
45 |
-
"""
|
46 |
-
query_vector = embed(user_query)
|
47 |
-
D, I = faiss_index.search(query_vector, k=1)
|
48 |
|
49 |
-
print(f"Closest FAISS match index: {I[0][0]}, Distance: {D[0][0]}")
|
50 |
|
51 |
-
if D[0][0] < THRESHOLD
|
52 |
-
response = dataset[I[0][0]].strip()
|
53 |
print("β
FAISS response used!")
|
54 |
else:
|
55 |
-
|
56 |
-
print("β οΈ FAISS match too weak, using model instead.")
|
57 |
inputs = tokenizer(user_query, return_tensors="pt")
|
58 |
outputs = model.generate(**inputs, max_new_tokens=150)
|
59 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
60 |
|
61 |
return response
|
62 |
|
63 |
-
|
64 |
# πΉ Gradio UI
|
65 |
-
iface = gr.Interface(
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
import faiss
|
3 |
import numpy as np
|
4 |
import torch
|
|
|
|
|
5 |
import gradio as gr
|
6 |
+
from transformers import AutoModelForCausalLM, GemmaTokenizer
|
7 |
from huggingface_hub import hf_hub_download
|
8 |
|
9 |
# πΉ Hugging Face Credentials
|
10 |
+
HF_REPO = "Futuresony/my_model" # Ensure this is correct
|
11 |
+
HF_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN') # Ensure this is set in your environment
|
12 |
|
13 |
+
# πΉ FAISS Index Path
|
14 |
FAISS_PATH = "asa_faiss.index"
|
15 |
+
DATASET_PATH = "responses.txt" # Ensure this file contains indexed responses
|
16 |
|
17 |
+
# β
Load FAISS Index from Hugging Face if not available locally
|
18 |
+
if not os.path.exists(FAISS_PATH):
|
19 |
+
print("π Downloading FAISS index...")
|
20 |
+
FAISS_PATH = hf_hub_download(HF_REPO, "asa_faiss.index", token=HF_TOKEN)
|
21 |
|
22 |
+
print(f"π Loading FAISS index from {FAISS_PATH}...")
|
23 |
+
faiss_index = faiss.read_index(FAISS_PATH)
|
24 |
+
print("β
FAISS index loaded successfully!")
|
|
|
25 |
|
26 |
+
# β
Load responses dataset
|
27 |
+
if os.path.exists(DATASET_PATH):
|
28 |
+
with open(DATASET_PATH, "r", encoding="utf-8") as f:
|
29 |
+
dataset = f.readlines()
|
30 |
+
print("β
Responses dataset loaded!")
|
31 |
+
else:
|
32 |
+
print(f"β οΈ Warning: {DATASET_PATH} not found!")
|
33 |
+
dataset = []
|
34 |
+
|
35 |
+
# β
Load model & tokenizer
|
36 |
+
print("π Loading tokenizer and model...")
|
37 |
+
tokenizer = GemmaTokenizer.from_pretrained(HF_REPO, token=HF_TOKEN)
|
38 |
model = AutoModelForCausalLM.from_pretrained(HF_REPO, token=HF_TOKEN)
|
39 |
+
print("β
Model and tokenizer loaded!")
|
40 |
|
41 |
+
# πΉ Set FAISS distance threshold (lower values = more strict matches)
|
42 |
+
THRESHOLD = 80 # Adjusted threshold for better accuracy
|
|
|
|
|
|
|
|
|
43 |
|
44 |
def embed(text):
|
45 |
+
"""Convert text to FAISS-compatible vector."""
|
46 |
+
tokens = tokenizer.encode(text, add_special_tokens=True)
|
47 |
+
return np.array(tokens, dtype=np.float32).reshape(1, -1)
|
48 |
|
49 |
def chatbot_response(user_query):
|
50 |
+
"""Fetches response from FAISS or falls back to the model."""
|
51 |
+
query_vector = embed(user_query)
|
52 |
+
D, I = faiss_index.search(query_vector, k=1)
|
53 |
|
54 |
+
print(f"π Closest FAISS match index: {I[0][0]}, Distance: {D[0][0]}")
|
55 |
|
56 |
+
if D[0][0] < THRESHOLD and 0 <= I[0][0] < len(dataset):
|
57 |
+
response = dataset[I[0][0]].strip()
|
58 |
print("β
FAISS response used!")
|
59 |
else:
|
60 |
+
print("β οΈ FAISS match too weak, generating response using model.")
|
|
|
61 |
inputs = tokenizer(user_query, return_tensors="pt")
|
62 |
outputs = model.generate(**inputs, max_new_tokens=150)
|
63 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
64 |
|
65 |
return response
|
66 |
|
|
|
67 |
# πΉ Gradio UI
|
68 |
+
iface = gr.Interface(
|
69 |
+
fn=chatbot_response,
|
70 |
+
inputs="text",
|
71 |
+
outputs="text",
|
72 |
+
title="ASA Microfinance Chatbot",
|
73 |
+
description="A chatbot that provides information using FAISS and a language model."
|
74 |
+
)
|
75 |
+
|
76 |
+
if __name__ == "__main__":
|
77 |
+
iface.launch()
|