Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generative ai projects added #1057

Closed
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
Binary file not shown.
6,348 changes: 6,348 additions & 0 deletions GenerativeAI/Advance/Langchain based Chat Retrival Chatbot/Mumbai1.csv

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: !
emoji: 🏠
colorFrom: #FF5733
colorTo: #FFBF00
sdk: streamlit
sdk_version: 1.25.0
app_file: app.py
pinned: false
---

Dataset Link : https://www.kaggle.com/datasets/sameep98/housing-prices-in-mumbai

# Llama

LlaMA (Large Language Model Meta AI) is a Generative AI model, specifically a group of foundational Large Language Models developed by Meta AI, a company owned by Meta(Formerly Facebook). Meta announced Llama in Feb of 2023. Meta released Llama in different sizes(based on parameters), i.e., 7,13,33, and 65 billion parameters with a context length of 2k tokens. The model is with the intent to help researchers advance their knowledge in the field of AI. The small 7B models allow researchers with low computation power to study these models.

# Llama 2

LlaMA 2 surpasses the previous version, LlaMA version 1, which Meta released in July of 2023. It came out in three sizes: 7B, 13B, and 70B parameter models. Upon its release, LlaMA 2 achieved the highest score on Hugging Face. Even across all segments (7B, 13B, and 70B), the top-performing model on Hugging Face originates from LlaMA 2, having been fine-tuned or retrained.

This release includes model weights and starting code for pretrained and fine-tuned Llama language models — ranging from 7B to 70B parameters.

## Download

The source code for Llama 2 is available on GitHub. If you want to work with the original weights, these are also available, but for this, you need to provide your name and email to the Meta AIs website. So go to the Meta AI by [clicking here](https://ai.meta.com/resources/models-and-libraries/llama-downloads/), then enter your name, email address, and organization(student if you are not working). Then scroll down and click on accept and continue. Now you will get a mail stating that you can download the model weights

### Access on Hugging Face

We are also providing downloads on [Hugging Face](https://huggingface.co/TheBloke/Llama-2-7B-GGML/tree/main). You must first request a download from the Meta website using the same email address as your Hugging Face account. After doing so, you can request access to any of the models on Hugging Face and within 1-2 days your account will be granted access to all versions.


## References

1. [Research Paper](https://ai.meta.com/research/publications/llama-2-open-foundation-and-fine-tuned-chat-models/)
2. [Llama 2 technical overview](https://ai.meta.com/resources/models-and-libraries/llama)
3. [Hugging Face](https://huggingface.co/meta-llama)

## Original LLaMA

The repo for the original llama release is in the [`llama_v1`](https://github.com/facebookresearch/llama/tree/llama_v1) branch.

90 changes: 90 additions & 0 deletions GenerativeAI/Advance/Langchain based Chat Retrival Chatbot/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import streamlit as st
from langchain.chains import ConversationalRetrievalChain
from langchain.document_loaders import CSVLoader
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.llms import CTransformers
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import FAISS
from langchain.memory import ConversationBufferMemory
from langchain.document_loaders.csv_loader import CSVLoader

# Set Streamlit page configuration
st.set_page_config(
page_title="ApnaGhar 🏠 - Mumbai Property Insights",
page_icon="", # You can set an icon if needed
layout="wide",
initial_sidebar_state="expanded",
)

# Load data and set up language chain components
loader = CSVLoader(file_path='Mumbai1.csv')
documents = loader.load()

text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
text_chunks = text_splitter.split_documents(documents)

embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",
model_kwargs={'device': "cpu"})

vector_store = FAISS.from_documents(text_chunks, embeddings)

llm = CTransformers(model="link llma model", model_type="llama",
config={'max_new_tokens': 128, 'temperature': 0.01})

memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

chain = ConversationalRetrievalChain.from_llm(llm=llm, chain_type='stuff',
retriever=vector_store.as_retriever(search_kwargs={"k": 2}),
memory=memory)

st.sidebar.title("ApnaGhar 🏠 - Mumbai Property Insights")
st.sidebar.info("Welcome to ApnaGhar! Explore Mumbai's real estate and neighborhood information.")
github_link = "[GitHub]()"
st.sidebar.info("To contribute and Sponsor - " + github_link)

if 'query_stack' not in st.session_state:
st.session_state['query_stack'] = []

st.sidebar.subheader("Property Queries:")
for i, past_query in enumerate(reversed(st.session_state['query_stack'])):
st.sidebar.write(f"{i + 1}. {past_query}")

st.title("ApnaGhar - Mumbai Property Insights")

if 'history' not in st.session_state:
st.session_state['history'] = []

if 'generated' not in st.session_state:
st.session_state['generated'] = ["Hello! I'm ApnaGhar, here to help you with Mumbai property insights."]

if 'past' not in st.session_state:
st.session_state['past'] = ["Hello!"]

reply_container = st.container()
container = st.container()

with container:
with st.form(key='user_form', clear_on_submit=True):
user_input = st.text_input("Your Query:", placeholder="Ask anything about Mumbai properties or neighborhoods", key='input_user')
submit_button_user = st.form_submit_button(label='Send')

try:
if submit_button_user and user_input:
output = chain({"question": user_input, "chat_history": st.session_state['history']})["answer"]
st.session_state['past'].append(user_input)
st.session_state['generated'].append(output)

# Push user input to the stack
st.session_state['query_stack'].append(user_input)
except Exception as e:
st.error(f"An error occurred: {str(e)}")

if st.session_state['generated']:
with reply_container:
for i in range(len(st.session_state['generated'])):
user_message = st.session_state["past"][i]
generated_message = st.session_state["generated"][i]

st.text(f"User: {user_message}")

st.text(f"ApnaGhar: {generated_message}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
langchain
torch
accelerate
#bitsandbytes
transformers
sentence_transformers
streamlit
streamlit_chat
faiss-cpu
altair
tiktoken
huggingface-hub
pypdf
ctransformers
210 changes: 210 additions & 0 deletions GenerativeAI/Basic/ImageToTextGenerator/ImageToTextGenerator.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"gpuType": "T4"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU",
"gpuClass": "standard"
},
"cells": [
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"id": "SeT-a9Byby1n"
},
"outputs": [],
"source": [
"\n",
"from transformers import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer\n",
"import torch\n",
"from PIL import Image"
]
},
{
"cell_type": "code",
"source": [
"\n",
"model = VisionEncoderDecoderModel.from_pretrained(\"nlpconnect/vit-gpt2-image-captioning\")\n",
"feature_extractor = ViTImageProcessor.from_pretrained(\"nlpconnect/vit-gpt2-image-captioning\")\n",
"tokenizer = AutoTokenizer.from_pretrained(\"nlpconnect/vit-gpt2-image-captioning\")\n",
"\n",
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
"model.to(device)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Gh2jscQnot8g",
"outputId": "fe64ca40-7f91-4cd4-8967-5c00bb0ce857"
},
"execution_count": 17,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"VisionEncoderDecoderModel(\n",
" (encoder): ViTModel(\n",
" (embeddings): ViTEmbeddings(\n",
" (patch_embeddings): ViTPatchEmbeddings(\n",
" (projection): Conv2d(3, 768, kernel_size=(16, 16), stride=(16, 16))\n",
" )\n",
" (dropout): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (encoder): ViTEncoder(\n",
" (layer): ModuleList(\n",
" (0-11): 12 x ViTLayer(\n",
" (attention): ViTAttention(\n",
" (attention): ViTSelfAttention(\n",
" (query): Linear(in_features=768, out_features=768, bias=True)\n",
" (key): Linear(in_features=768, out_features=768, bias=True)\n",
" (value): Linear(in_features=768, out_features=768, bias=True)\n",
" (dropout): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (output): ViTSelfOutput(\n",
" (dense): Linear(in_features=768, out_features=768, bias=True)\n",
" (dropout): Dropout(p=0.0, inplace=False)\n",
" )\n",
" )\n",
" (intermediate): ViTIntermediate(\n",
" (dense): Linear(in_features=768, out_features=3072, bias=True)\n",
" (intermediate_act_fn): GELUActivation()\n",
" )\n",
" (output): ViTOutput(\n",
" (dense): Linear(in_features=3072, out_features=768, bias=True)\n",
" (dropout): Dropout(p=0.0, inplace=False)\n",
" )\n",
" (layernorm_before): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
" (layernorm_after): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
" )\n",
" )\n",
" )\n",
" (layernorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
" (pooler): ViTPooler(\n",
" (dense): Linear(in_features=768, out_features=768, bias=True)\n",
" (activation): Tanh()\n",
" )\n",
" )\n",
" (decoder): GPT2LMHeadModel(\n",
" (transformer): GPT2Model(\n",
" (wte): Embedding(50257, 768)\n",
" (wpe): Embedding(1024, 768)\n",
" (drop): Dropout(p=0.1, inplace=False)\n",
" (h): ModuleList(\n",
" (0-11): 12 x GPT2Block(\n",
" (ln_1): LayerNorm((768,), eps=1e-05, elementwise_affine=True)\n",
" (attn): GPT2Attention(\n",
" (c_attn): Conv1D()\n",
" (c_proj): Conv1D()\n",
" (attn_dropout): Dropout(p=0.1, inplace=False)\n",
" (resid_dropout): Dropout(p=0.1, inplace=False)\n",
" )\n",
" (ln_2): LayerNorm((768,), eps=1e-05, elementwise_affine=True)\n",
" (crossattention): GPT2Attention(\n",
" (c_attn): Conv1D()\n",
" (q_attn): Conv1D()\n",
" (c_proj): Conv1D()\n",
" (attn_dropout): Dropout(p=0.1, inplace=False)\n",
" (resid_dropout): Dropout(p=0.1, inplace=False)\n",
" )\n",
" (ln_cross_attn): LayerNorm((768,), eps=1e-05, elementwise_affine=True)\n",
" (mlp): GPT2MLP(\n",
" (c_fc): Conv1D()\n",
" (c_proj): Conv1D()\n",
" (act): NewGELUActivation()\n",
" (dropout): Dropout(p=0.1, inplace=False)\n",
" )\n",
" )\n",
" )\n",
" (ln_f): LayerNorm((768,), eps=1e-05, elementwise_affine=True)\n",
" )\n",
" (lm_head): Linear(in_features=768, out_features=50257, bias=False)\n",
" )\n",
")"
]
},
"metadata": {},
"execution_count": 17
}
]
},
{
"cell_type": "code",
"source": [
"max_length = 16\n",
"num_beams = 4\n",
"gen_kwargs = {\"max_length\": max_length, \"num_beams\": num_beams}"
],
"metadata": {
"id": "hm6EtiPoot27"
},
"execution_count": 18,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def predict_step(image_paths):\n",
" images = []\n",
" for image_path in image_paths:\n",
" i_image = Image.open(image_path)\n",
" if i_image.mode != \"RGB\":\n",
" i_image = i_image.convert(mode=\"RGB\")\n",
"\n",
" images.append(i_image)\n",
"\n",
" pixel_values = feature_extractor(images=images, return_tensors=\"pt\").pixel_values\n",
" pixel_values = pixel_values.to(device)\n",
"\n",
" output_ids = model.generate(pixel_values, **gen_kwargs)\n",
"\n",
" preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)\n",
" preds = [pred.strip() for pred in preds]\n",
" return preds"
],
"metadata": {
"id": "FQ298E4gotu-"
},
"execution_count": 19,
"outputs": []
},
{
"cell_type": "code",
"source": [
"predict_step(['/content/drive/MyDrive/images/Plane-flying-on-earth-atmosphere.jpg']) "
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "u5ajgeTho6G_",
"outputId": "e6f62aa7-b0b7-4eff-947f-85a3a84e87ce"
},
"execution_count": 22,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['a large jetliner flying through a blue sky']"
]
},
"metadata": {},
"execution_count": 22
}
]
}
]
}
Loading
Loading