ritikjain51 commited on
Commit
2f190d5
·
1 Parent(s): c4875ad

Added Question Answering System

Browse files
.chroma/index/id_to_uuid_dc0bfad7-5592-4a68-a0b7-fb5f1e137b26.pkl ADDED
Binary file (98 Bytes). View file
 
.chroma/index/index_dc0bfad7-5592-4a68-a0b7-fb5f1e137b26.bin ADDED
Binary file (12.7 kB). View file
 
.chroma/index/index_metadata_dc0bfad7-5592-4a68-a0b7-fb5f1e137b26.pkl ADDED
Binary file (103 Bytes). View file
 
.chroma/index/uuid_to_id_dc0bfad7-5592-4a68-a0b7-fb5f1e137b26.pkl ADDED
Binary file (90 Bytes). View file
 
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ .Chroma
2
+ *.ipynb
3
+ *.pyc
Dockerfile ADDED
File without changes
__init__.py ADDED
File without changes
main.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import os
3
+
4
+ import gradio as gr
5
+ from langchain import OpenAI
6
+ from langchain.chains import ConversationalRetrievalChain
7
+ from langchain.document_loaders import PyPDFLoader
8
+ from langchain.embeddings import OpenAIEmbeddings
9
+ from langchain.text_splitter import CharacterTextSplitter
10
+ from langchain.vectorstores import Chroma
11
+
12
+ documents = []
13
+ qa = None
14
+
15
+
16
+ def get_file(file):
17
+ try:
18
+ global documents
19
+ data = PyPDFLoader(file.name)
20
+ documents = data.load_and_split(CharacterTextSplitter(chunk_size=2000, chunk_overlap=0))
21
+ except Exception as e:
22
+ logging.error(e, exc_info=True)
23
+ return "Failed to upload."
24
+ return "File Uploaded."
25
+
26
+
27
+ def model_configuration(model_name, api_key=None, hug_model=None, hug_token=None):
28
+ try:
29
+ embeddings, llm = None, None
30
+ if not documents:
31
+ return gr.update(value="Please upload correct PDF!", visible=True)
32
+ global qa
33
+ if model_name == "OpenAI":
34
+ os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", api_key)
35
+ embeddings = OpenAIEmbeddings()
36
+ llm = OpenAI(temperature=1)
37
+ elif model_name == "HuggingFace":
38
+ return gr.update(value="Hugging Face is not yet supported!", visible=True)
39
+ os.environ["HUGGINGFACEHUB_API_TOKEN"] = os.getenv("", hug_token)
40
+ # embeddings = HuggingFaceEmbeddings(model_name=hug_model, model_kwargs={'device': 'cpu'})
41
+ # llm = HuggingFaceHub(repo_id=hug_model)
42
+
43
+ if embeddings:
44
+ db = Chroma.from_documents(documents, embeddings)
45
+ retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 2})
46
+ qa = ConversationalRetrievalChain.from_llm(llm, chain_type="map_reduce", retriever=retriever,
47
+ return_source_documents=True, verbose=False)
48
+ except Exception as e:
49
+ logging.error(e, exc_info=True)
50
+ return gr.update(value="Error occurred!", visible=True)
51
+ return gr.update(value="Model Built", visible=True)
52
+
53
+
54
+ def response(msg, chat_history):
55
+ global qa
56
+ result = qa({"question": msg, "chat_history": map(tuple, chat_history)})
57
+ final_resp = result.get("answer", "").strip()
58
+ chat_history.append((msg, final_resp))
59
+ return "", chat_history
60
+
61
+
62
+ with gr.Blocks() as demo:
63
+ with gr.Tab("PDF Ingestion") as pdf_input:
64
+ file = None
65
+ with gr.Column() as r1:
66
+ file = gr.File(file_types=[".pdf"])
67
+ op_txt = gr.Label(value="", label="")
68
+ fl_btn = gr.Button("Upload & Ingest 🚀")
69
+ fl_btn.click(get_file, inputs=[file], outputs=op_txt)
70
+
71
+ with gr.Tab("Select Model") as model:
72
+ model_name = gr.Dropdown(
73
+ ["NA", "OpenAI", "HuggingFace"],
74
+ show_label=True,
75
+ label="Model Name",
76
+ multiselect=False,
77
+ value="NA"
78
+ )
79
+ with gr.Column(visible=False) as openai_config:
80
+ api_key = gr.Textbox(value="", label="OPENAI API KEY", placeholder="sk-...", visible=True, interactive=True)
81
+
82
+ with gr.Column(visible=False) as huggy_config:
83
+ hug_model = gr.Dropdown(["distilbert-base-uncased"],
84
+ value="distilbert-base-uncased", multiselect=False)
85
+ hug_token = gr.Textbox(value="", placeholder="hf-...", interactive=True)
86
+
87
+
88
+ def show_configuration(model_name):
89
+ if model_name == "OpenAI":
90
+ return {
91
+ openai_config: gr.update(visible=True),
92
+ huggy_config: gr.update(visible=False)
93
+ }
94
+ elif model_name == "HuggingFace":
95
+ return {
96
+ openai_config: gr.update(visible=False),
97
+ huggy_config: gr.update(visible=True)
98
+ }
99
+ return {
100
+ openai_config: gr.update(visible=False),
101
+ huggy_config: gr.update(visible=False)
102
+ }
103
+
104
+
105
+ model_name.change(show_configuration, inputs=[model_name], outputs=[openai_config, huggy_config])
106
+ model_updated = gr.Label("", visible=False)
107
+ btn = gr.Button("Configure Model 🤖")
108
+ btn.click(model_configuration, inputs=[model_name, api_key, hug_model, hug_token], outputs=model_updated)
109
+
110
+ with gr.Tab("Q&A") as qna:
111
+ with gr.Column() as r:
112
+ chatbot = gr.Chatbot(show_label=True)
113
+ msg = gr.Textbox(placeholder="Ask Something")
114
+ clear = gr.Button("Clear")
115
+ msg.submit(response, [msg, chatbot], [msg, chatbot])
116
+ clear.click(lambda: None, None, chatbot, queue=False)
117
+
118
+ if __name__ == "__main__":
119
+ demo.launch()
qna.py ADDED
File without changes