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
|
|
|
| 1 |
+
|
| 2 |
+
from llama_index import VectorStoreIndex,download_loader, VectorStoreIndex, ServiceContext, StorageContext, load_index_from_storage
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from github import Github
|
| 5 |
+
import os
|
| 6 |
+
import shutil
|
| 7 |
+
import openai
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
from llama_index import download_loader
|
| 12 |
+
|
| 13 |
+
"""# Github Configeration"""
|
| 14 |
+
|
| 15 |
+
openai.api_key = os.environ.get("OPENAPI_API_KEY")
|
| 16 |
+
|
| 17 |
+
# username = 'Akhil-Sharma30'
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
"""# Reading the Files for LLM Model"""
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# Specify the path to the repository
|
| 24 |
+
repo_dir = "/content/Akhil-Sharma30.github.io"
|
| 25 |
+
|
| 26 |
+
# Check if the repository exists and delete it if it does
|
| 27 |
+
if os.path.exists(repo_dir):
|
| 28 |
+
shutil.rmtree(repo_dir)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# def combine_md_files(folder_path):
|
| 32 |
+
# MarkdownReader = download_loader("MarkdownReader")
|
| 33 |
+
# loader = MarkdownReader()
|
| 34 |
+
|
| 35 |
+
# md_files = [file for file in folder_path.glob('*.md')]
|
| 36 |
+
# documents = None
|
| 37 |
+
|
| 38 |
+
# for file_path in md_files:
|
| 39 |
+
# document = loader.load_data(file=file_path)
|
| 40 |
+
# documents += document
|
| 41 |
+
|
| 42 |
+
# return documents
|
| 43 |
+
|
| 44 |
+
# folder_path = Path('/content/Akhil-Sharma30.github.io/content')
|
| 45 |
+
#combined_documents = combine_md_files(folder_path)
|
| 46 |
+
|
| 47 |
+
# combined_documents will be a list containing the contents of all .md files in the folder
|
| 48 |
+
|
| 49 |
+
MarkdownReader = download_loader("MarkdownReader")
|
| 50 |
+
|
| 51 |
+
loader = MarkdownReader()
|
| 52 |
+
document1 = loader.load_data(file=Path('Akhil-Sharma30.github.io/assets/README.md'))
|
| 53 |
+
document2 = loader.load_data(file=Path('Akhil-Sharma30.github.io/content/about.md'))
|
| 54 |
+
document3 = loader.load_data(file=Path('Akhil-Sharma30.github.io/content/cv.md'))
|
| 55 |
+
document4 = loader.load_data(file=Path('Akhil-Sharma30.github.io/content/post.md'))
|
| 56 |
+
document5 = loader.load_data(file=Path('Akhil-Sharma30.github.io/content/opensource.md'))
|
| 57 |
+
document6 = loader.load_data(file=Path('Akhil-Sharma30.github.io/content/supervised.md'))
|
| 58 |
+
|
| 59 |
+
data = document1+ document2 + document3+ document4 + document5+document6
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
"""# Vector Embedding"""
|
| 63 |
+
|
| 64 |
+
index = VectorStoreIndex.from_documents(data)
|
| 65 |
+
|
| 66 |
+
query_engine = index.as_query_engine()
|
| 67 |
+
response = query_engine.query("know akhil?")
|
| 68 |
+
print(response)
|
| 69 |
+
|
| 70 |
+
response = query_engine.query("what is name of the person?")
|
| 71 |
+
print(response)
|
| 72 |
+
|
| 73 |
+
"""# ChatBot Interface"""
|
| 74 |
+
|
| 75 |
+
def chat(chat_history, user_input):
|
| 76 |
+
|
| 77 |
+
bot_response = query_engine.query(user_input)
|
| 78 |
+
#print(bot_response)
|
| 79 |
+
response = ""
|
| 80 |
+
for letter in ''.join(bot_response.response): #[bot_response[i:i+1] for i in range(0, len(bot_response), 1)]:
|
| 81 |
+
response += letter + ""
|
| 82 |
+
yield chat_history + [(user_input, response)]
|
| 83 |
+
|
| 84 |
+
with gr.Blocks() as demo:
|
| 85 |
+
gr.Markdown('# Robotic Akhil')
|
| 86 |
+
gr.Markdown('## "Innovating Intelligence - Unveil the secrets of a cutting-edge ChatBot project that introduces you to the genius behind the machine. π¨π»βπ»π')
|
| 87 |
+
gr.Markdown('> Hint: Akhil 2.0')
|
| 88 |
+
gr.Markdown('## Some question you can ask to test Bot:')
|
| 89 |
+
gr.Markdown('#### :) know akhil?')
|
| 90 |
+
gr.Markdown('#### :) write about my work at Agnisys?')
|
| 91 |
+
gr.Markdown('#### :) write about my work at IIT Delhi?')
|
| 92 |
+
gr.Markdown('#### :) was work in P1 Virtual Civilization Initiative opensource?')
|
| 93 |
+
gr.Markdown('#### many more......')
|
| 94 |
+
with gr.Tab("Knowledge Bot"):
|
| 95 |
+
#inputbox = gr.Textbox("Input your text to build a Q&A Bot here.....")
|
| 96 |
+
chatbot = gr.Chatbot()
|
| 97 |
+
message = gr.Textbox ("know akhil?")
|
| 98 |
+
message.submit(chat, [chatbot, message], chatbot)
|
| 99 |
+
|
| 100 |
+
demo.queue().launch(share=True)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
"""# **Github Setup**"""
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
"""## Launch Phoenix
|
| 108 |
+
|
| 109 |
+
Define your knowledge base dataset with a schema that specifies the meaning of each column (features, predictions, actuals, tags, embeddings, etc.). See the [docs](https://docs.arize.com/phoenix/) for guides on how to define your own schema and API reference on `phoenix.Schema` and `phoenix.EmbeddingColumnNames`.
|
| 110 |
+
"""
|
| 111 |
+
|
| 112 |
+
# # get a random sample of 500 documents (including retrieved documents)
|
| 113 |
+
# # this will be handled by by the application in a coming release
|
| 114 |
+
# num_sampled_point = 500
|
| 115 |
+
# retrieved_document_ids = set(
|
| 116 |
+
# [
|
| 117 |
+
# doc_id
|
| 118 |
+
# for doc_ids in query_df[":feature.[str].retrieved_document_ids:prompt"].to_list()
|
| 119 |
+
# for doc_id in doc_ids
|
| 120 |
+
# ]
|
| 121 |
+
# )
|
| 122 |
+
# retrieved_document_mask = database_df["document_id"].isin(retrieved_document_ids)
|
| 123 |
+
# num_retrieved_documents = len(retrieved_document_ids)
|
| 124 |
+
# num_additional_samples = num_sampled_point - num_retrieved_documents
|
| 125 |
+
# unretrieved_document_mask = ~retrieved_document_mask
|
| 126 |
+
# sampled_unretrieved_document_ids = set(
|
| 127 |
+
# database_df[unretrieved_document_mask]["document_id"]
|
| 128 |
+
# .sample(n=num_additional_samples, random_state=0)
|
| 129 |
+
# .to_list()
|
| 130 |
+
# )
|
| 131 |
+
# sampled_unretrieved_document_mask = database_df["document_id"].isin(
|
| 132 |
+
# sampled_unretrieved_document_ids
|
| 133 |
+
# )
|
| 134 |
+
# sampled_document_mask = retrieved_document_mask | sampled_unretrieved_document_mask
|
| 135 |
+
# sampled_database_df = database_df[sampled_document_mask]
|
| 136 |
+
|
| 137 |
+
# database_schema = px.Schema(
|
| 138 |
+
# prediction_id_column_name="document_id",
|
| 139 |
+
# prompt_column_names=px.EmbeddingColumnNames(
|
| 140 |
+
# vector_column_name="text_vector",
|
| 141 |
+
# raw_data_column_name="text",
|
| 142 |
+
# ),
|
| 143 |
+
# )
|
| 144 |
+
# database_ds = px.Dataset(
|
| 145 |
+
# dataframe=sampled_database_df,
|
| 146 |
+
# schema=database_schema,
|
| 147 |
+
# name="database",
|
| 148 |
+
# )
|
| 149 |
+
|
| 150 |
+
"""Define your query dataset. Because the query dataframe is in OpenInference format, Phoenix is able to infer the meaning of each column without a user-defined schema by using the `phoenix.Dataset.from_open_inference` class method."""
|
| 151 |
+
|
| 152 |
+
# query_ds = px.Dataset.from_open_inference(query_df)
|
| 153 |
+
|
| 154 |
+
"""Launch Phoenix. Follow the instructions in the cell output to open the Phoenix UI."""
|
| 155 |
+
|
| 156 |
+
# session = px.launch_app(primary=query_ds, corpus=database_ds)
|
| 157 |
+
|