Futuresony commited on
Commit
56d52d4
Β·
verified Β·
1 Parent(s): a6ff621

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -37
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 os
8
  from huggingface_hub import hf_hub_download
9
 
10
  # πŸ”Ή Hugging Face Credentials
11
- HF_REPO = "Futuresony/future_ai_12_10_2024.gguf"
12
- HF_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
13
 
14
- # πŸ”Ή Paths
15
  FAISS_PATH = "asa_faiss.index"
16
- DATASET_PATH = "responses.txt"
17
 
18
- # πŸ”Ή Load FAISS Index
19
- faiss_local_path = hf_hub_download(HF_REPO, FAISS_PATH, token=HF_TOKEN)
20
- faiss_index = faiss.read_index(faiss_local_path)
21
- print("βœ… FAISS index loaded successfully!")
22
 
23
- # πŸ”Ή Load Dataset Responses
24
- with open(DATASET_PATH, "r", encoding="utf-8") as f:
25
- dataset = f.readlines()
26
- print("βœ… Responses dataset loaded!")
27
 
28
- # πŸ”Ή Load Model & Tokenizer Correctly
29
- tokenizer = AutoTokenizer.from_pretrained(HF_REPO, token=HF_TOKEN)
 
 
 
 
 
 
 
 
 
 
30
  model = AutoModelForCausalLM.from_pretrained(HF_REPO, token=HF_TOKEN)
 
31
 
32
- # πŸ”Ή Load Sentence Transformer (Ensures proper FAISS embedding match)
33
- embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
34
-
35
- # πŸ”Ή Adjust FAISS Threshold
36
- THRESHOLD = 100 # Adjust based on actual FAISS distances
37
-
38
 
39
  def embed(text):
40
- """Convert text into FAISS-compatible vector using the same method as training."""
41
- return embedder.encode([text], convert_to_tensor=True).cpu().numpy()
42
-
43
 
44
  def chatbot_response(user_query):
45
- """Fetch response from FAISS or generate with model if needed."""
46
- query_vector = embed(user_query) # Convert input to vector
47
- D, I = faiss_index.search(query_vector, k=1) # Search FAISS index
48
 
49
- print(f"Closest FAISS match index: {I[0][0]}, Distance: {D[0][0]}") # Debugging info
50
 
51
- if D[0][0] < THRESHOLD: # If FAISS match is good
52
- response = dataset[I[0][0]].strip() # Retrieve FAISS response
53
  print("βœ… FAISS response used!")
54
  else:
55
- # πŸ”₯ Fallback: Generate response with model
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(fn=chatbot_response, inputs="text", outputs="text", title="ASA Microfinance Chatbot")
66
- iface.launch()
 
 
 
 
 
 
 
 
 
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()