Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions chapter-1/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello welcome to world of programming!
I am Eva, your personal assistanc. How may I help you today?
34 changes: 34 additions & 0 deletions chapter-1/problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
print('''Twinkle, twinkle, little star,
How I wonder what you are!
Up above the world so high,
Like a diamond in the sky.
Twinkle, twinkle, little star,
How I wonder what you are!

When the blazing sun is gone,
When he nothing shines upon,
Then you show your little light,
Twinkle, twinkle, all the night.
Twinkle, twinkle, little star,
How I wonder what you are!

Then the traveller in the dark,
Thanks you for your tiny spark,
He could not see which way to go,
If you did not twinkle so.
Twinkle, twinkle, little star,
How I wonder what you are!

In the dark blue sky you keep,
And often through my curtains peep,
For you never shut your eye,
Till the sun is in the sky.
Twinkle, twinkle, little star,
How I wonder what you are!

As your bright and tiny spark,
Lights the traveller in the dark,—
Though I know not what you are,
Twinkle, twinkle, little star.
Twinkle, twinkle, little star,
How I wonder what you are!''')
Empty file added chapter-1/problem2.py
Empty file.
5 changes: 5 additions & 0 deletions chapter-1/problem3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import pyttsx3

engine = pyttsx3.init()
engine.say("Hello, how are you?")
engine.runAndWait()
4 changes: 4 additions & 0 deletions chapter-1/problem4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import os
directory_path = '/'
contents = os.listdir(directory_path)
print(contents)
4 changes: 4 additions & 0 deletions chapter-2/01_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
a=int(input("Enter a number: "))
b=int(input("Enter another number: "))
c=a+b
print("The sum of", a, "and", b, "is", c,"and type is: ", type(c))
1 change: 1 addition & 0 deletions chapter-2/02_datatypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xl
Empty file added chapter-2/03_rules_variable.py
Empty file.
13 changes: 13 additions & 0 deletions done/;project 2-guess the nuber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import random
n=random.randint(1,100)
print("**Guess the number between 1 to 100 **")
a=-1
guess=1
while(a!=n):
a=int(input("Enter the number:"))
if(a>n):
print("Smaller number please!")
elif(a<n):
print("Greater number please!")
guess+=1
print(f"You won the game in {guess} attempts and the number was {n}")
157 changes: 157 additions & 0 deletions done/Codec.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#!/usr/bin/env python
# coding: utf-8

# Install necessary packages
!pip install sentence-transformers faiss-cpu langchain numba huggingface-hub

import os
import json
import numpy as np
import faiss
from sentence_transformers import SentenceTransformer
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from numba import cuda
from langchain.schema import SystemMessage, HumanMessage, AIMessage
from langchain.prompts import ChatPromptTemplate
from langchain_community.llms import LlamaCpp
from huggingface_hub import hf_hub_download

# Function to extract text and split into chunks
def extract_and_split_text(pdf_file, chunk_size=300):
loader = PyPDFLoader(pdf_file)
doc = loader.load_and_split()
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=chunk_size,
chunk_overlap=50,
length_function=len,
add_start_index=True,
)
chunks = text_splitter.split_documents(doc)

# Print and check chunks
for idx, chunk in enumerate(chunks):
print(f"Chunk {idx}:\n{chunk.page_content}\n{'-'*40}")

# Handle very small and very large chunks
chunks = [chunk for chunk in chunks if 50 < len(chunk.page_content) < 2000]

return chunks

# Function to create embeddings and handle edge cases
def create_embeddings(chunks, model_path='sentence-transformers/paraphrase-MiniLM-L6-v2'):
model = SentenceTransformer(model_path)
embeddings = []

for chunk in chunks:
embedding = model.encode(chunk.page_content)
if embedding.size == 0:
embedding = np.zeros((768,)) # Assuming 768-dimensional embeddings
embeddings.append(embedding)

embeddings = np.vstack(embeddings)

# Padding and normalization if necessary
embeddings = np.pad(embeddings, ((0, 0), (0, 0)), mode='constant', constant_values=0)
embeddings = embeddings / np.linalg.norm(embeddings, axis=1, keepdims=True)

return embeddings

# Function to create FAISS index
def create_faiss_index(embeddings, index_file):
index = faiss.IndexFlatL2(embeddings.shape[1])
index.add(embeddings)
faiss.write_index(index, index_file)
return index

# Function to load FAISS index
def load_faiss_index(index_file):
if not os.path.exists(index_file):
raise FileNotFoundError(f"Index file does not exist: {index_file}")
return faiss.read_index(index_file)

# Function to perform similarity search
def similarity_search(index, query_embedding, k=10):
query_embedding = np.array(query_embedding).astype('float32')
D, I = index.search(np.array([query_embedding]), k)
return I[0], D[0]

# Main process
pdf_file_name = '/content/the-story-of-doctor-dolittle-1-15.pdf'
DB_file_name = os.path.basename(pdf_file_name).split('.')[0]
index_file = f"{DB_file_name}.index"

# Extract text and split into chunks
chunks = extract_and_split_text(pdf_file_name)

# Create embeddings and handle edge cases
embeddings = create_embeddings(chunks)

# Create FAISS index if it doesn't exist
if not os.path.exists(index_file):
index = create_faiss_index(embeddings, index_file)
else:
index = load_faiss_index(index_file)

# Define and do the embeddings of query
model = SentenceTransformer('sentence-transformers/paraphrase-MiniLM-L6-v2')
query = 'who is the author of this book?'
query_embedding = model.encode(query)

# Perform similarity search
indices, distances = similarity_search(index, query_embedding, k=10)

# Print top results and check
print("Top results from similarity search:")
for idx in indices:
print(f"Chunk {idx}:\n{chunks[int(idx)].page_content}\n{'-'*40}")

# Convert searched chunks to JSON format
searched_chunks = [{"chunk_id": int(idx), "content": chunks[int(idx)].page_content} for idx in indices]
context_json = json.dumps(searched_chunks, indent=2)

n_gpu_layers = -1
n_batch = 512

# Use the Hugging Face model directly (replace with an appropriate model if needed)
model_name = 'TheBloke/Mistral-7B-Instruct-v0.2-GGUF'
model_file = hf_hub_download(repo_id=model_name, filename='mistral-7b-instruct-v0.2.Q4_K_M.gguf', local_dir='./')

print(f"Downloaded model file path: {model_file}")

llm = LlamaCpp(
model_path=model_file,
n_gpu_layers=-1,
n_batch=512,
temperature=.5,
n_ctx=10000,
)

# Define prompt template
PROMPT_TEMPLATE = """
Given the following document context in JSON format, answer the question:
{context}
Question: {question}
Answer:
"""

# Generate sample text for LLM check
def generate_sample_prompt(question, context_json):
prompt_template = ChatPromptTemplate.from_template(PROMPT_TEMPLATE)
prompt = prompt_template.format_messages(context=context_json, question=question)
return prompt

sample_prompt = generate_sample_prompt('What is the document about?', context_json)
print("Generated sample prompt:")
print(sample_prompt)

# Define prompt with context and generate answer
prompt = generate_sample_prompt(query, context_json)

# Pass query, retrieved context, prompt to LLM to generate answer
try:
response = llm.invoke(prompt)
print("Response:")
print(response)
except Exception as e:
print(f"Error during model invocation: {e}")
20 changes: 20 additions & 0 deletions done/Match case.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def https_status(status):
match status:
case 200:
return "ok"
case 404:
return "unknown"
case _:
return "default"
print(https_status(200))

dic1={
1:'krishn',
2:'Jatav'
}
dic2={
2:'Jattu',
3:'bhai'
}
merge=dic1 | dic2
print(merge)
Binary file added done/beef.pdf
Binary file not shown.
8 changes: 8 additions & 0 deletions done/calculator by class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from typing import Union

ID:Union[str,int]=input("Enter your college ID: ")
x=ID.upper()
if(x=="20AI016"):
print("Welcome Krishn Jatav!")
else:
print("X invalide ID X")
6 changes: 6 additions & 0 deletions done/celcius_to_fehrnite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
c=int(input("Enter the temprature: "))

def temp(c):
feh=c+32
return feh
print("Fehernite: ",temp(c))
8 changes: 8 additions & 0 deletions done/check to file equale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
with open("name.txt","r") as f1:
c1=f1.read()
with open("copy_name.txt","r") as f2:
c2=f2.read()
if(c1==c2):
print("Both file have same content!")
else:
print("Both files are different.")
6 changes: 6 additions & 0 deletions done/copy file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
with open("name.txt")as f:
content=f.read()

with open("copy_name.txt","w") as f:
f.write(content)

3 changes: 3 additions & 0 deletions done/copy_name.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
I am AI research Analyst at Biz4Group.
hello python
python
10 changes: 10 additions & 0 deletions done/count the line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
with open("name.txt", "r") as f:
lines=f.readlines()
lineno=1
for line in lines:
if("researcher" in line):
print(f"word found at line no:{lineno}")
break
lineno+=1
else:
print("not found")
46 changes: 46 additions & 0 deletions done/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from typing import Union
ID: Union[str, int] = input("Enter your college ID: ")
x = ID.upper()

if x == "20AI016":
print(" ")
print(" ** Login Successfully ** ")
print(" ")
print(" Welcome Krishn Jatav! ")
print(""" -- Choose the following Options --
1. Account Info
2. Update Account
3. Game mode

""")

while True:
n = int(input("Enter your option: "))

if n == 1:
print(""" -- Account Information --
Name: Krishn Jatav
Age : 21
ID : 20AI016

Enter 0 to Exit!""")

elif n == 2:
print(''' ! Service Under Maintenance.

Enter 0 to Exit!''')

elif n == 3:
print("""* Error: No game Found! *

Enter 0 to Exit!""")

elif n == 0:
print("Exiting...")
break

else:
print("Invalid option, please try again.")

else:
print("** Error: Invalid ID **")
3 changes: 3 additions & 0 deletions done/demo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
I am AI research Analyst at Biz4Group.
hello python
python
31 changes: 31 additions & 0 deletions done/demo2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Union
ID:Union[str,int]=input("Enter your ID: ")
id="20AI016"
x=ID.upper()
if x==id:
print(" ")
print(" ** Login Successfully ** ")
print(" ")
print(" Welcome Krishn Jatav! ")
print(""" -- Choose the following Options --
1. Account Info
2. Update Account
3. Game mode

""")

while True:
n=int(input("Enter your response: "))
if n==1:
print("No Account found!")

print(" Enter 0 for Exit!")
elif n==2:
print("No Account found!")

print(" Enter 0 for Exit!")
elif n==0:
print("Exiting...")
break
else:
print(" Invalide ID.")
Loading