Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,50 +4,80 @@ from sentence_transformers import SentenceTransformer
|
|
| 4 |
import faiss
|
| 5 |
import numpy as np
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Sample knowledge base (documents)
|
| 8 |
documents = [
|
| 9 |
"""
|
| 10 |
-
Biodata or about ginni as name : GINNI GARG, email : gargginni01@gmail.com, phone : +91-8295954475, Date of Birth - 1st January 1998
|
| 11 |
""",
|
| 12 |
"""
|
| 13 |
-
Ginni
|
| 14 |
""",
|
| 15 |
"""
|
| 16 |
-
|
| 17 |
""",
|
| 18 |
"""
|
| 19 |
-
Ginni
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
""",
|
| 21 |
"""
|
| 22 |
ginni completed Schooling both 10th (2012-2013) with cgpa 10, and 12th (2014-2015) with 91% from D.A.V. Public School, Kalanwali.
|
| 23 |
""",
|
| 24 |
"""
|
| 25 |
-
|
| 26 |
-
1. Scientist 'B' @ CDOT
|
| 27 |
-
(April 2024 - Present)
|
| 28 |
-
Working on Software Security and Backend Engineering.
|
| 29 |
-
|
| 30 |
-
2.Software Engineer – Machine Learning @ SirionLabs (Location – Gurugram/Remote)
|
| 31 |
-
(Dec 2022 – March 2023)
|
| 32 |
-
Working in Client Legal Management (CLM) company, Tech used :-
|
| 33 |
-
Flask, Pulsar, Debugging, GCP buckets, YAML, Debugging, Python Scripting, Docanno, Model
|
| 34 |
-
Training/Validation, API formation, Postman.
|
| 35 |
-
|
| 36 |
-
3.Software Development Engineer – 2 @ Otipy (Crofarm) (Location – Gurugram)
|
| 37 |
-
(May 2022 – November 2022)
|
| 38 |
-
Working in E-commerce Company in Warehouse Team, Tech used :-
|
| 39 |
-
Django, SQL, Redis, Celery, Kafka, Pagination
|
| 40 |
-
|
| 41 |
-
4.Software Engineer (Full Time) @ Arcesium India Private Ltd (Location – Gurugram)
|
| 42 |
-
(August 2020 – May 2022)
|
| 43 |
-
Working in Fintech Company for specific clients on various Technologies :
|
| 44 |
-
ETL Framework, Flask Framework, Async Await Python, Python Scripting, Sqlite3 in-memory db, Postgres SQL, YAML, Gunicorn Server, Unit Test Cases (For Sync and Async Python), Git, Gitlab, S3
|
| 45 |
-
Buckets, Authentication – Kerberos and JWT, JIRA, Debugging, Threading and Multi-processing.
|
| 46 |
""",
|
| 47 |
"""
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
""",
|
| 52 |
"""
|
| 53 |
All academic Achievements of ginni:
|
|
@@ -58,7 +88,7 @@ All academic Achievements of ginni:
|
|
| 58 |
5. In National Level Science Talent Search Examination and secured 252 rank at National Level.
|
| 59 |
""",
|
| 60 |
"""
|
| 61 |
-
List of all Publications or research papers of ginni
|
| 62 |
1. Ginni Garg and Ritu Garg. “Brain Tumor Detection and Classification using Hybrid Ensemble Classifier”.
|
| 63 |
International Journal of Healthcare Information Systems and Informatics (IJHISI), IGI Global, Clarivate Analytics
|
| 64 |
indexed, scopus indexed.
|
|
@@ -71,12 +101,10 @@ List of all Publications or research papers of ginni ??
|
|
| 71 |
spectral Images”. International Conference Futuristic Trends in Networks and Computing Technologies, FTNCT-
|
| 72 |
2020 Approved by CCIS, Springer (Indexed by Scopus and DBLP), Southern Federal University, Russia.
|
| 73 |
arxiv Link: https://arxiv.org/abs/2101.00214
|
| 74 |
-
""",
|
| 75 |
-
"""
|
| 76 |
-
If you are not confident, just answer "I donot know"
|
| 77 |
"""
|
| 78 |
]
|
| 79 |
|
|
|
|
| 80 |
# Step 1: Embed documents using a transformer model
|
| 81 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 82 |
doc_embeddings = model.encode(documents)
|
|
@@ -85,13 +113,28 @@ doc_embeddings = model.encode(documents)
|
|
| 85 |
index = faiss.IndexFlatL2(doc_embeddings.shape[1])
|
| 86 |
index.add(np.array(doc_embeddings).astype("float32"))
|
| 87 |
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
def rag_qa(question):
|
|
|
|
|
|
|
| 90 |
question_embedding = model.encode([question])
|
| 91 |
-
|
| 92 |
retrieved_doc = documents[retrieved_indices[0][0]]
|
| 93 |
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-large")
|
| 94 |
-
prompt = f"Context: {retrieved_doc}\n\nQ: {question}\nA:"
|
| 95 |
response = qa_pipeline(prompt, max_length=1000)
|
| 96 |
return response[0]['generated_text']
|
| 97 |
|
|
|
|
| 4 |
import faiss
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
+
import re
|
| 8 |
+
import pkg_resources
|
| 9 |
+
from symspellpy import SymSpell, Verbosity
|
| 10 |
+
|
| 11 |
+
# Initialize SymSpell
|
| 12 |
+
sym_spell = SymSpell(max_dictionary_edit_distance=2, prefix_length=7)
|
| 13 |
+
|
| 14 |
+
# Load a dictionary
|
| 15 |
+
dictionary_path = pkg_resources.resource_filename(
|
| 16 |
+
"symspellpy", "frequency_dictionary_en_82_765.txt"
|
| 17 |
+
)
|
| 18 |
+
sym_spell.load_dictionary(dictionary_path, term_index=0, count_index=1)
|
| 19 |
+
sym_spell.create_dictionary_entry("cgpa", 100000)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def preprocess_text(text):
|
| 23 |
+
# Use regex to split text into tokens, preserving numeric/alphanumeric data
|
| 24 |
+
tokens = re.findall(r'\w+|\d+\w*|\S+', text)
|
| 25 |
+
return tokens
|
| 26 |
+
|
| 27 |
+
# Function to correct spelling while preserving numeric data
|
| 28 |
+
def correct_spelling(text):
|
| 29 |
+
# Split text into tokens
|
| 30 |
+
tokens = preprocess_text(text)
|
| 31 |
+
corrected_tokens = []
|
| 32 |
+
# print(tokens)
|
| 33 |
+
for token in tokens:
|
| 34 |
+
# If the token is numeric or alphanumeric, preserve it
|
| 35 |
+
if token.isdigit() or re.match(r'\d+\w*', token) or re.match(r'[.,]', token):# or re.match(r'\S+', token):
|
| 36 |
+
corrected_tokens.append(token)
|
| 37 |
+
else:
|
| 38 |
+
# Otherwise, correct the token using SymSpell
|
| 39 |
+
suggestions = sym_spell.lookup(token, max_edit_distance=2, verbosity=1)
|
| 40 |
+
# print(suggestions)
|
| 41 |
+
if suggestions:
|
| 42 |
+
corrected_token = suggestions[0].term # Use the best suggestion
|
| 43 |
+
else:
|
| 44 |
+
corrected_token = token # If no suggestion, keep the original token
|
| 45 |
+
corrected_tokens.append(corrected_token)
|
| 46 |
+
|
| 47 |
+
# Join the corrected tokens into a sentence
|
| 48 |
+
return " ".join(corrected_tokens)
|
| 49 |
+
|
| 50 |
# Sample knowledge base (documents)
|
| 51 |
documents = [
|
| 52 |
"""
|
| 53 |
+
Biodata or about ginni as name : GINNI GARG, email : gargginni01@gmail.com, phone : +91-8295954475, Date of Birth - 1st January 1998.
|
| 54 |
""",
|
| 55 |
"""
|
| 56 |
+
Ginni completed his Graduation B.Tech in Computer Engineering from National Institute of Technology, Kurukshetra in between 2016 -2020 with cgpa 9.65
|
| 57 |
""",
|
| 58 |
"""
|
| 59 |
+
Father name of ginni is DharamPal Garg. He is Director JSS Sirsa. Mother Name is Rajni Garg, She is Housewife. Wife name of ginni is Ekta, She is Bank Manager.
|
| 60 |
""",
|
| 61 |
"""
|
| 62 |
+
Ginni hobbies are reading books, Badminton, Yoga, Running, Walking, Exercies, GYM etc.
|
| 63 |
+
""",
|
| 64 |
+
"""
|
| 65 |
+
Ginni Favourite Books are Atomic Habits, IKigai, Biography of Swami Viveknand, Jeevan Amrit by OSHO etc.
|
| 66 |
+
""",
|
| 67 |
+
"""
|
| 68 |
+
Ginni Domain expertise is Software Engineering, specifically Backed Engineering.
|
| 69 |
""",
|
| 70 |
"""
|
| 71 |
ginni completed Schooling both 10th (2012-2013) with cgpa 10, and 12th (2014-2015) with 91% from D.A.V. Public School, Kalanwali.
|
| 72 |
""",
|
| 73 |
"""
|
| 74 |
+
all companies where ginni worked/experience as follow CDOT, SirionLabs, Otipy and Arcesium.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
""",
|
| 76 |
"""
|
| 77 |
+
Gate qualified in 2020 with All India Rank 2562, gate score 562 and GATE Marks as 46.67/100. JEE Main Qualified in 2016 with All India Rank 8123, JEE Marks as 231/360 and JEE Percentile 99.3%
|
| 78 |
+
""",
|
| 79 |
+
"""
|
| 80 |
+
social media of ginni as follow - 'linkedin : www.linkedin.com/in/ginni-garg', 'github : https://github.com/GinniIndia'
|
| 81 |
""",
|
| 82 |
"""
|
| 83 |
All academic Achievements of ginni:
|
|
|
|
| 88 |
5. In National Level Science Talent Search Examination and secured 252 rank at National Level.
|
| 89 |
""",
|
| 90 |
"""
|
| 91 |
+
List of all Publications or research papers of ginni as :
|
| 92 |
1. Ginni Garg and Ritu Garg. “Brain Tumor Detection and Classification using Hybrid Ensemble Classifier”.
|
| 93 |
International Journal of Healthcare Information Systems and Informatics (IJHISI), IGI Global, Clarivate Analytics
|
| 94 |
indexed, scopus indexed.
|
|
|
|
| 101 |
spectral Images”. International Conference Futuristic Trends in Networks and Computing Technologies, FTNCT-
|
| 102 |
2020 Approved by CCIS, Springer (Indexed by Scopus and DBLP), Southern Federal University, Russia.
|
| 103 |
arxiv Link: https://arxiv.org/abs/2101.00214
|
|
|
|
|
|
|
|
|
|
| 104 |
"""
|
| 105 |
]
|
| 106 |
|
| 107 |
+
|
| 108 |
# Step 1: Embed documents using a transformer model
|
| 109 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 110 |
doc_embeddings = model.encode(documents)
|
|
|
|
| 113 |
index = faiss.IndexFlatL2(doc_embeddings.shape[1])
|
| 114 |
index.add(np.array(doc_embeddings).astype("float32"))
|
| 115 |
|
| 116 |
+
d = doc_embeddings.shape[1]
|
| 117 |
+
#
|
| 118 |
+
# nlist = 5 # Number of clusters (adjust based on data size)
|
| 119 |
+
# quantizer = faiss.IndexFlatL2(d) # L2 distance metric for clustering
|
| 120 |
+
# ivf_index = faiss.IndexIVFFlat(quantizer, d, nlist, faiss.METRIC_L2)
|
| 121 |
+
#
|
| 122 |
+
# # Train IVF index (mandatory step before adding vectors)
|
| 123 |
+
# ivf_index.train(doc_embeddings)
|
| 124 |
+
# ivf_index.add(doc_embeddings)
|
| 125 |
+
#
|
| 126 |
+
hnsw_index = faiss.IndexHNSWFlat(d, 32) # 32 - recommended parameter for optimal search
|
| 127 |
+
hnsw_index.add(doc_embeddings)
|
| 128 |
+
|
| 129 |
+
# # Step 3: Define the RAG pipeline
|
| 130 |
def rag_qa(question):
|
| 131 |
+
question = correct_spelling(question)
|
| 132 |
+
print(f'correct_question : {question}')
|
| 133 |
question_embedding = model.encode([question])
|
| 134 |
+
distances, retrieved_indices = hnsw_index.search(np.array(question_embedding).astype("float32"), k=1)
|
| 135 |
retrieved_doc = documents[retrieved_indices[0][0]]
|
| 136 |
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-large")
|
| 137 |
+
prompt = f"Context: {retrieved_doc}\n\nQ: {question}\nA: If the answer is not clear from the context, respond with 'I don't know'"
|
| 138 |
response = qa_pipeline(prompt, max_length=1000)
|
| 139 |
return response[0]['generated_text']
|
| 140 |
|