Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- ai_brain.py +0 -33
- ask_brain.py +0 -44
ai_brain.py
DELETED
|
@@ -1,33 +0,0 @@
|
|
| 1 |
-
from sentence_transformers import SentenceTransformer
|
| 2 |
-
from supabase import create_client, Client
|
| 3 |
-
from config import settings
|
| 4 |
-
|
| 5 |
-
SUPABASE_URL = settings.SUPABASE_URL
|
| 6 |
-
SUPABASE_KEY = settings.SUPABASE_KEY
|
| 7 |
-
|
| 8 |
-
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 9 |
-
|
| 10 |
-
def test_ai_database():
|
| 11 |
-
print("Downloading AI tool (this takes a few seconds)...")
|
| 12 |
-
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 13 |
-
|
| 14 |
-
print("Converting text into numbers...")
|
| 15 |
-
# This turns English text into numbers so the database can search it later
|
| 16 |
-
embedding_numbers = model.encode("Farmers get 6000 rupees a year").tolist()
|
| 17 |
-
|
| 18 |
-
print("Saving to Supabase...")
|
| 19 |
-
data = {
|
| 20 |
-
"scheme_title": "Farmer Test Scheme",
|
| 21 |
-
"chunk_text": "Farmers get 6000 rupees a year",
|
| 22 |
-
"embedding": embedding_numbers,
|
| 23 |
-
"source_url": "test.com"
|
| 24 |
-
}
|
| 25 |
-
|
| 26 |
-
try:
|
| 27 |
-
supabase.table("document_chunks").insert(data).execute()
|
| 28 |
-
print("✅ SUCCESS! The data is saved in your database!")
|
| 29 |
-
except Exception as e:
|
| 30 |
-
print(f"❌ Error: {e}")
|
| 31 |
-
|
| 32 |
-
if __name__ == "__main__":
|
| 33 |
-
test_ai_database()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ask_brain.py
DELETED
|
@@ -1,44 +0,0 @@
|
|
| 1 |
-
from sentence_transformers import SentenceTransformer
|
| 2 |
-
from supabase import create_client, Client
|
| 3 |
-
from config import settings
|
| 4 |
-
|
| 5 |
-
SUPABASE_URL = settings.SUPABASE_URL
|
| 6 |
-
SUPABASE_KEY = settings.SUPABASE_KEY
|
| 7 |
-
|
| 8 |
-
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 9 |
-
|
| 10 |
-
def ask_question():
|
| 11 |
-
print("Waking up the AI...")
|
| 12 |
-
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 13 |
-
|
| 14 |
-
# We are asking about a "kisan" (which we didn't save in the database)
|
| 15 |
-
user_question = "How can a kisan get financial help?"
|
| 16 |
-
print(f"\nQuestion: '{user_question}'")
|
| 17 |
-
|
| 18 |
-
print("Converting question into numbers...")
|
| 19 |
-
query_numbers = model.encode(user_question).tolist()
|
| 20 |
-
|
| 21 |
-
print("Searching Supabase...")
|
| 22 |
-
try:
|
| 23 |
-
# Ask Supabase to run the 'match_documents' SQL function
|
| 24 |
-
response = supabase.rpc("match_documents", {
|
| 25 |
-
"query_embedding": query_numbers,
|
| 26 |
-
"match_count": 1
|
| 27 |
-
}).execute()
|
| 28 |
-
|
| 29 |
-
matches = response.data
|
| 30 |
-
|
| 31 |
-
if matches:
|
| 32 |
-
best_match = matches[0]
|
| 33 |
-
print("\n✨ --- AI FOUND A MATCH! --- ✨")
|
| 34 |
-
print(f"Scheme: {best_match['scheme_title']}")
|
| 35 |
-
print(f"Details: {best_match['chunk_text']}")
|
| 36 |
-
print(f"Accuracy Score: {best_match['similarity']:.2f}")
|
| 37 |
-
else:
|
| 38 |
-
print("\n❌ No documents found.")
|
| 39 |
-
|
| 40 |
-
except Exception as e:
|
| 41 |
-
print(f"❌ Error: {e}")
|
| 42 |
-
|
| 43 |
-
if __name__ == "__main__":
|
| 44 |
-
ask_question()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|