Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
os.environ["ANTHROPIC_API_KEY"] = "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"
|
4 |
+
import gradio as gr
|
5 |
+
from langchain.memory import ConversationBufferMemory
|
6 |
+
from langchain.chains import RetrievalQA
|
7 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
8 |
+
from langchain.vectorstores import Chroma
|
9 |
+
from langchain.chat_models import ChatAnthropic
|
10 |
+
|
11 |
+
model_kwargs = {'trust_remote_code': True}
|
12 |
+
embeddings = HuggingFaceEmbeddings(model_name="jinaai/jina-embeddings-v2-base-en",
|
13 |
+
model_kwargs=model_kwargs)
|
14 |
+
llm = ChatAnthropic(model='claude-2',
|
15 |
+
temperature=0)
|
16 |
+
|
17 |
+
# Load the persisted Chroma database
|
18 |
+
persist_directory='/Electric_Machinerydb'
|
19 |
+
embeddings = embeddings
|
20 |
+
chroma_db = Chroma(persist_directory=persist_directory, embedding_function=embeddings)
|
21 |
+
retriever = chroma_db.as_retriever(search_kwargs={"k": 3}, return_source_documents=True)
|
22 |
+
|
23 |
+
# Initialize ConversationBufferMemory
|
24 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True, input_key="query", output_key="result")
|
25 |
+
|
26 |
+
# Create a question-answering chain using the retriever
|
27 |
+
qa_chain = RetrievalQA.from_chain_type(
|
28 |
+
llm=llm,
|
29 |
+
chain_type="stuff",
|
30 |
+
retriever=retriever,
|
31 |
+
return_source_documents=True,
|
32 |
+
memory=memory,
|
33 |
+
output_key="result" # Specify the output key
|
34 |
+
)
|
35 |
+
|
36 |
+
# Gradio app
|
37 |
+
def ask_question(question, chat_history):
|
38 |
+
query = question.strip()
|
39 |
+
if query:
|
40 |
+
result = qa_chain({"query": query})
|
41 |
+
answer = result['result']
|
42 |
+
chat_history.append((query, answer))
|
43 |
+
metadata = show_metadata(chat_history)
|
44 |
+
return "", chat_history, metadata
|
45 |
+
else:
|
46 |
+
chat_history.append(("", "Please enter a question."))
|
47 |
+
return "", chat_history, ""
|
48 |
+
|
49 |
+
def show_metadata(chat_history):
|
50 |
+
if chat_history:
|
51 |
+
query, answer = chat_history[-1]
|
52 |
+
result = qa_chain({"query": query})
|
53 |
+
metadata = ""
|
54 |
+
for doc in result['source_documents']:
|
55 |
+
metadata += f"Page: {doc.metadata['page']}\n"
|
56 |
+
metadata += f"Source: {doc.metadata['source']}\n"
|
57 |
+
metadata += f"Content: {doc.page_content}\n"
|
58 |
+
metadata += "---\n"
|
59 |
+
return metadata
|
60 |
+
return ""
|
61 |
+
|
62 |
+
def clean_history():
|
63 |
+
memory.clear()
|
64 |
+
return [], "", ""
|
65 |
+
|
66 |
+
with gr.Blocks() as demo:
|
67 |
+
gr.Markdown("# Electric Machinery QA by Tamil")
|
68 |
+
|
69 |
+
with gr.Row():
|
70 |
+
with gr.Column(scale=2):
|
71 |
+
question = gr.Textbox(label="Question")
|
72 |
+
ask_btn = gr.Button("Ask")
|
73 |
+
clean_btn = gr.Button("Clean")
|
74 |
+
chatbot = gr.Chatbot(label="Conversation")
|
75 |
+
|
76 |
+
with gr.Column(scale=1):
|
77 |
+
gr.Markdown("## Metadata")
|
78 |
+
metadata_output = gr.Textbox(label="Source Information", lines=10)
|
79 |
+
|
80 |
+
ask_btn.click(ask_question, inputs=[question, chatbot], outputs=[question, chatbot, metadata_output])
|
81 |
+
clean_btn.click(clean_history, inputs=[], outputs=[chatbot, question, metadata_output])
|
82 |
+
|
83 |
+
demo.launch()
|