Team Ai
Apppublic

DeveloperAkhil/Personal-Chatbot

sourceHugging Faceupdated 2y agoView on Hugging Face
3likes
app.py159 linesDownload Raw Back to root
1 2from llama_index.core import VectorStoreIndex,download_loader, VectorStoreIndex, ServiceContext, StorageContext, load_index_from_storage3from pathlib import Path4from github import Github5import os6import shutil7import openai8import gradio as gr9 10from pathlib import Path11 12"""# Github Configeration"""13 14openai.api_key = os.environ.get("OPENAPI_API_KEY")15 16# username = 'Akhil-Sharma30'17 18 19"""# Reading the Files for LLM Model"""20 21 22# Specify the path to the repository23repo_dir = "/content/Akhil-Sharma30.github.io"24 25# Check if the repository exists and delete it if it does26if os.path.exists(repo_dir):27    shutil.rmtree(repo_dir)28 29 30# def combine_md_files(folder_path):31#     MarkdownReader = download_loader("MarkdownReader")32#     loader = MarkdownReader()33 34#     md_files = [file for file in folder_path.glob('*.md')]35#     documents = None36 37#     for file_path in md_files:38#         document = loader.load_data(file=file_path)39#         documents += document40 41#     return documents42 43# folder_path = Path('/content/Akhil-Sharma30.github.io/content')44#combined_documents = combine_md_files(folder_path)45 46# combined_documents will be a list containing the contents of all .md files in the folder47 48RemoteReader = download_loader("RemoteReader")49 50loader = RemoteReader()51 52 53document1 = loader.load_data(url="https://raw.githubusercontent.com/Akhil-Sharma30/Akhil-Sharma30.github.io/main/assets/README.md")54document2 = loader.load_data(url="https://raw.githubusercontent.com/Akhil-Sharma30/Akhil-Sharma30.github.io/main/content/about.md")55document3 = loader.load_data(url="https://raw.githubusercontent.com/Akhil-Sharma30/Akhil-Sharma30.github.io/main/content/cv.md")56document4 = loader.load_data(url="https://raw.githubusercontent.com/Akhil-Sharma30/Akhil-Sharma30.github.io/main/content/post.md")57document5 = loader.load_data(url="https://raw.githubusercontent.com/Akhil-Sharma30/Akhil-Sharma30.github.io/main/content/opensource.md")58document6 = loader.load_data(url="https://raw.githubusercontent.com/Akhil-Sharma30/Akhil-Sharma30.github.io/main/content/supervised.md")59 60data = document1+ document2 + document3+ document4 + document5+document661 62 63"""# Vector Embedding"""64 65index = VectorStoreIndex.from_documents(data)66 67query_engine = index.as_query_engine()68response = query_engine.query("know akhil?")69print(response)70 71response = query_engine.query("what is name of the person?")72print(response)73 74"""# ChatBot Interface"""75 76def chat(chat_history, user_input):77 78  bot_response = query_engine.query(user_input)79  #print(bot_response)80  response = ""81  for letter in ''.join(bot_response.response): #[bot_response[i:i+1] for i in range(0, len(bot_response), 1)]:82      response += letter + ""83      yield chat_history + [(user_input, response)]84 85with gr.Blocks() as demo:86    gr.Markdown('# Robotic Akhil')87    gr.Markdown('## "Innovating Intelligence - Unveil the secrets of a cutting-edge ChatBot project that introduces you to the genius behind the machine. ๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป๐Ÿ˜Ž')88    gr.Markdown('> Hint: Akhil 2.0')89    gr.Markdown('## Some question you can ask to test Bot:')90    gr.Markdown('#### :) know akhil?')91    gr.Markdown('#### :) write about my work at Agnisys?')92    gr.Markdown('#### :) write about my work at IIT Delhi?')93    gr.Markdown('#### :) was work in P1 Virtual Civilization Initiative opensource?')94    gr.Markdown('#### many more......')95    with gr.Tab("Knowledge Bot"):96#inputbox = gr.Textbox("Input your text to build a Q&A Bot here.....")97          chatbot = gr.Chatbot()98          message = gr.Textbox ("know akhil?")99          message.submit(chat, [chatbot, message], chatbot)100 101demo.queue().launch()102 103 104"""# **Github Setup**"""105 106 107 108"""## Launch Phoenix109 110Define 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`.111"""112 113# # get a random sample of 500 documents (including retrieved documents)114# # this will be handled by by the application in a coming release115# num_sampled_point = 500116# retrieved_document_ids = set(117#     [118#         doc_id119#         for doc_ids in query_df[":feature.[str].retrieved_document_ids:prompt"].to_list()120#         for doc_id in doc_ids121#     ]122# )123# retrieved_document_mask = database_df["document_id"].isin(retrieved_document_ids)124# num_retrieved_documents = len(retrieved_document_ids)125# num_additional_samples = num_sampled_point - num_retrieved_documents126# unretrieved_document_mask = ~retrieved_document_mask127# sampled_unretrieved_document_ids = set(128#     database_df[unretrieved_document_mask]["document_id"]129#     .sample(n=num_additional_samples, random_state=0)130#     .to_list()131# )132# sampled_unretrieved_document_mask = database_df["document_id"].isin(133#     sampled_unretrieved_document_ids134# )135# sampled_document_mask = retrieved_document_mask | sampled_unretrieved_document_mask136# sampled_database_df = database_df[sampled_document_mask]137 138# database_schema = px.Schema(139#     prediction_id_column_name="document_id",140#     prompt_column_names=px.EmbeddingColumnNames(141#         vector_column_name="text_vector",142#         raw_data_column_name="text",143#     ),144# )145# database_ds = px.Dataset(146#     dataframe=sampled_database_df,147#     schema=database_schema,148#     name="database",149# )150 151"""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."""152 153# query_ds = px.Dataset.from_open_inference(query_df)154 155"""Launch Phoenix. Follow the instructions in the cell output to open the Phoenix UI."""156 157# session = px.launch_app(primary=query_ds, corpus=database_ds)158 159