Gen A I

                                           Installation Of Models

!pip install --upgrade numpy gensim scikit-learn matplotlib transformers torch sentence-transformers langchain cohere langchain-community pymupdf faiss-cpu wikipedia fitz random beautifulsoup4 requests 

or 

import subprocess

import sys

packages = [

    "numpy", "gensim", "scikit-learn", "matplotlib", "transformers", "torch",

    "sentence-transformers", "langchain", "cohere", "langchain-community",

    "pymupdf", "faiss-cpu", "wikipedia", "beautifulsoup4", "requests"

]

subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade"] + packages)

or

import sys

!{sys.executable} -m pip install --upgrade numpy gensim scikit-learn matplotlib \

    transformers torch sentence-transformers langchain cohere \

    langchain-community pymupdf faiss-cpu wikipedia beautifulsoup4 requests


 COLAB

genai

https://colab.research.google.com/drive/1FE8aYXNHH5I5IBCU2Nj50qvX5oMrnpzc?usp=sharing#scrollTo=4PVFhFSLEYuU

1 - 5

!pip uninstall -y numpy
!pip install --upgrade --force-reinstall scipy gensim
!pip install --upgrade --force-reinstall numpy==1.24.4

6-10 --> nothing


 #1

import numpy as np
from gensim.downloader import load
print("Loading pre-trained GloVe model (50 dimensions)...")
model = load("glove-wiki-gigaword-50")
def ewr():
    result = model.most_similar(positive=['king', 'woman'], negative=['man'], topn=1)
    print("\nking - man + woman = ?", result[0][0])
    print("similarity:", result[0][1])
    result = model.most_similar(positive=['paris', 'italy'], negative=['france'], topn=1)
    print("\nparis france + italy = ?", result[0][0])
    print("similarity:", result[0][1])
    result = model.most_similar(positive=['programming'], topn=5)
    print("\nTop 5 words similar to 'programming':")
    for word, similarity in result:
        print(word, similarity)
ewr()



#2
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from gensim.downloader import load
def reduce_dimensions(embeddings):
    pca = PCA(n_components=2)
    reduced_embeddings = pca.fit_transform(embeddings)
    return reduced_embeddings
def visualize_embeddings(words, reduced_embeddings):
    plt.figure(figsize=(10, 6))
    for i, word in enumerate(words):
        x, y = reduced_embeddings[i]
        plt.scatter(x, y, color='blue', marker='o')
        plt.text(x + 0.02, y + 0.02, word, fontsize=12)
    plt.title("2D Visualization of Word Embeddings")
    plt.xlabel("PCA Component 1")
    plt.ylabel("PCA Component 2")
    plt.grid()
    plt.show()
def get_similar_words(word, model):
    print(f"\nTop 5 words similar to '{word}':")
    similar_words = model.most_similar(word, topn=5)
    for similar_word, similarity in similar_words:
        print(f"{similar_word} ({similarity:.4f})")
print("Loading pre-trained GloVe model (50 dimensions)...")
model = load("glove-wiki-gigaword-50")
words = ['football', 'basketball', 'soccer', 'tennis', 'cricket',
         'hockey', 'baseball', 'golf', 'volleyball', 'rugby']
embeddings = [model[word] for word in words]
reduced_embeddings = reduce_dimensions(embeddings)
visualize_embeddings(words, reduced_embeddings)
get_similar_words("programming", model)



#3
from gensim.models import Word2Vec
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
corpus = [
    "The patient was diagnosed with diabetes and hypertension.",
    "MRI scans reveal abnormalities in the brain tissue.",
    "The treatment involves antibiotics and regular monitoring.",
    "Symptoms include fever, fatigue, and muscle pain.",
    "The vaccine is effective against several viral infections.",
    "Doctors recommend physical therapy for recovery.",
    "The clinical trial results were published in the journal.",
    "The surgeon performed a minimally invasive procedure.",
    "The prescription includes pain relievers and anti-inflammatory drugs.",
    "The diagnosis confirmed a rare genetic disorder."
]
tokenized_corpus = [sentence.lower().split() for sentence in corpus]
model = Word2Vec(sentences=tokenized_corpus, vector_size=5, window=2, min_count=1, epochs=5)
word = input("Enter a word: ").lower()
if word in model.wv:
    similar = model.wv.most_similar(word, topn=5)
    print(f"\nWords similar to '{word}':")
    for i, (w, score) in enumerate(similar, 1):
        print(f"{i}. {w} (Similarity: {score:.4f})")
else:
    print("\nWord not found in vocabulary.")
words = list(model.wv.index_to_key)
word_vectors = model.wv[words]
pca = PCA(n_components=2)
result = pca.fit_transform(word_vectors)
plt.figure(figsize=(10, 8))
plt.scatter(result[:, 0], result[:, 1], color='skyblue')
for i, word in enumerate(words):
    plt.annotate(word, xy=(result[i, 0], result[i, 1]), fontsize=12)
plt.title("Word Embeddings (PCA Projection)")
plt.xlabel("PCA 1")
plt.ylabel("PCA 2")
plt.grid(True)
plt.show()



#4

!pip install cohere gensim
import cohere
import gensim.downloader as api
co = cohere.Client("your-api-key-here")  
print("Loading word embeddings...")
model = api.load("glove-wiki-gigaword-100")
print("Model loaded successfully.")
def get_first_enriched_prompt(prompt, topn=3):
    for word in prompt.split():
        try:
            similar_words = model.most_similar(word.strip('.,!?').lower(), topn=topn)
            for sim, _ in similar_words:
                enriched = prompt.replace(word, sim)
                return enriched
        except :
            continue
    return None
def get_response(text):
    try:
        return co.chat(model="command-r", message=text).text.strip()
    except Exception as e:
        return f"Error: {e}"
prompt = "write an essay on natural disaster"
print(f"\nOriginal Prompt:\n{prompt}\nResponse:\n{get_response(prompt)}")
enriched_prompt = get_first_enriched_prompt(prompt)
if enriched_prompt:
    print(f"\nEnriched Prompt:\n{enriched_prompt}\nResponse:\n{get_response(enriched_prompt)}")
else:
    print("\nNo enriched prompt could be generated.")




#5
from gensim.downloader import load
import random
print("Loading pre-trained GloVe model (50 dimensions)...")
model = load("glove-wiki-gigaword-50")
print("Model loaded successfully!")
def create_paragraph(iw, sws):
    paragraph = f"The topic of '{iw}' is fascinating, often linked to terms like "
    random.shuffle(sws)
    for word in sws :
        paragraph += str(word)+","
    paragraph = paragraph.rstrip(", ") + "."
    return paragraph
iw = "hacking"
sws = model.most_similar(iw, topn=5)
words = [word for word, s in sws]
paragraph = create_paragraph(iw, words)
print("\nGenerated Paragraph:\n")
print(paragraph)





#6
!pip install transformers torch
from transformers import pipeline
sentiment_analyzer = pipeline("sentiment-analysis")
while True:
    user_input = input("\nEnter a sentence (or type 'exit' to quit): ")
    if user_input.lower() == "exit":
        print("Goodbye!")
        break
    if not user_input.strip():
        print("Please enter a non-empty sentence.")
        continue
    result = sentiment_analyzer(user_input)[0]
    print(f"\nLabel: {result['label']}")
    print(f"Confidence: {result['score']:.4f}")




#7
from transformers import pipeline
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
text = input("Enter text to summarize:\n")
if len(text.split()) < 30:
    print("Text is too short to summarize.")
else:
    summary = summarizer(text, max_length=50, min_length=30, do_sample=False)[0]['summary_text']
    print("\nSummary:", summary)





#8
!pip install langchain cohere langchain-community
from langchain_community.llms import Cohere
from langchain_community.document_loaders import TextLoader
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
text = TextLoader("/content/sample text.txt").load()[0].page_content
prompt = PromptTemplate(input_variables=["text"],template="Summarize this text:\n\n{text}\n\nSummary:")
llm = Cohere(cohere_api_key="JuJktEEFgPDKDmzaUDfmmEaIMldTroCQcvwiUInx", temperature=0.7)
summary = LLMChain(llm=llm, prompt=prompt).run(text=text)
print("\nSummary:\n", summary)





#9
!pip install wikipedia
import wikipedia, requests
from bs4 import BeautifulSoup as B
def f(n):
    try:
        p, s = wikipedia.page(n), wikipedia.summary(n, 2)
        i = {r.th.text.lower(): r.td.text for r in B(requests.get(p.url).text, 'html.parser').select('.infobox tr') if r.th and r.td}
        f = next((i[k] for k in i if 'found' in k or 'estab' in k), 'NA')
        h = next((i[k] for k in i if 'head' in k or 'loc' in k), 'NA')
        b = ', '.join([c for c in ['new york','london','tokyo','bangalore','davangere'] if c in p.content.lower()]) or 'NA'
        return f"Name: {n}\nFounded: {f}\nHead Quaters: {h}\nBranches: {b}\nSummary: {s}"
    except: return 'Not found or disambiguation issue.'
print(f(input("Enter institution name: ")))






#10
!pip install fitz faiss numpy
import fitz, faiss, numpy as np from sentence_transformers import SentenceTransformer as S t = "".join(p.get_text() for p in fitz.open("repealedfileopen.pdf")) c = [t[i:i+1000] for i in range(0, len(t), 1000)] m = S("all-MiniLM-L6-v2"); e = m.encode(c, convert_to_numpy=True).astype("float32") i = faiss.IndexFlatL2(e.shape[1]); i.add(e) print("IPC chatbot ready. Type 'bye' to exit.") while (q := input("\nYou: ")) != "bye": r = m.encode([q], convert_to_numpy=True).astype("float32") print("\nBot:", c[i.search(r, 1)[1][0][0]]) print("\nBot: Goodbye!")













Comments