Spaces:
Sleeping
Sleeping
apahilaj
commited on
Commit
•
fc78b7c
1
Parent(s):
b3627cb
pls
Browse files
app.py
CHANGED
@@ -1,68 +1,68 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
|
29 |
-
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
|
43 |
-
|
44 |
-
#
|
45 |
-
|
46 |
-
|
47 |
-
#
|
48 |
-
|
49 |
-
|
50 |
-
#
|
51 |
-
|
52 |
-
#
|
53 |
-
|
54 |
-
#
|
55 |
-
|
56 |
-
#
|
57 |
-
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
|
67 |
# return qa
|
68 |
|
@@ -128,35 +128,18 @@ Question: {question}
|
|
128 |
Helpful Answer:"""
|
129 |
QA_CHAIN_PROMPT = PromptTemplate.from_template(template)
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
#
|
141 |
-
|
142 |
-
|
143 |
-
retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": k})
|
144 |
-
# create a chatbot chain. Memory is managed externally.
|
145 |
-
question_generator_chain = LLMChain(llm=model, prompt=QA_CHAIN_PROMPT)
|
146 |
-
|
147 |
-
qa = ConversationalRetrievalChain.from_llm(
|
148 |
-
llm=model,
|
149 |
-
chain_type="stuff",
|
150 |
-
retriever=retriever,
|
151 |
-
return_source_documents=True,
|
152 |
-
return_generated_question=True,
|
153 |
-
)
|
154 |
-
|
155 |
-
return qa
|
156 |
-
|
157 |
-
def greet(messages, chat_history):
|
158 |
-
user_message = messages[-1]['content']
|
159 |
-
a = load_db("temp.pdf", 3) # assuming you've uploaded the file and saved it as "temp.pdf"
|
160 |
r = a.invoke({"question": user_message, "chat_history": chat_history})
|
161 |
match = re.search(r'Helpful Answer:(.*)', r['answer'])
|
162 |
if match:
|
@@ -167,5 +150,6 @@ def greet(messages, chat_history):
|
|
167 |
else:
|
168 |
return [{"role": "assistant", "content": "No helpful answer found."}]
|
169 |
|
170 |
-
|
171 |
-
iface.
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
4 |
+
from langchain.vectorstores import Chroma, faiss
|
5 |
+
from langchain_community.llms import HuggingFaceEndpoint, HuggingFaceHub
|
6 |
+
from langchain.chains import LLMChain
|
7 |
+
from langchain_community.document_loaders.csv_loader import CSVLoader
|
8 |
+
from langchain_community.document_loaders import PyPDFLoader
|
9 |
+
from langchain.text_splitter import CharacterTextSplitter
|
10 |
+
from langchain_community.document_loaders import TextLoader
|
11 |
+
from langchain_community import vectorstores
|
12 |
+
from langchain.prompts import PromptTemplate
|
13 |
+
from langchain.chains import RetrievalQA
|
14 |
+
from langchain.memory import ConversationBufferMemory
|
15 |
+
from langchain.chains import ConversationalRetrievalChain
|
16 |
+
from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
|
17 |
+
from langchain.vectorstores import DocArrayInMemorySearch
|
18 |
+
from langchain.document_loaders import TextLoader
|
19 |
+
from langchain.chains import RetrievalQA, ConversationalRetrievalChain
|
20 |
+
from langchain.memory import ConversationBufferMemory
|
21 |
+
from langchain.chat_models import ChatOpenAI
|
22 |
+
from langchain.document_loaders import TextLoader
|
23 |
+
from langchain.document_loaders import PyPDFLoader
|
24 |
+
import panel as pn
|
25 |
+
import param
|
26 |
+
import re
|
27 |
+
import os
|
28 |
|
29 |
+
api_token = os.environ.get('HUGGINGFACEHUB_API_TOKEN')
|
30 |
|
31 |
+
model = HuggingFaceHub(
|
32 |
+
huggingfacehub_api_token=api_token,
|
33 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.2",
|
34 |
+
task="conversational",
|
35 |
+
model_kwargs={"temperature": 0.8, "max_length": 1000},
|
36 |
+
)
|
37 |
+
template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. Use three sentences maximum. Keep the answer as concise as possible. Always say "thanks for asking!" at the end of the answer.
|
38 |
+
{context}
|
39 |
+
Question: {question}
|
40 |
+
Helpful Answer:"""
|
41 |
+
QA_CHAIN_PROMPT = PromptTemplate.from_template(template)
|
42 |
|
43 |
+
def load_db(file, k):
|
44 |
+
# load documents
|
45 |
+
loader = PyPDFLoader(file)
|
46 |
+
documents = loader.load()
|
47 |
+
# split documents
|
48 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
|
49 |
+
docs = text_splitter.split_documents(documents)
|
50 |
+
# define embedding
|
51 |
+
embeddings = HuggingFaceEmbeddings()
|
52 |
+
# create vector database from data
|
53 |
+
db = vectorstores.FAISS.from_documents(docs, embeddings)
|
54 |
+
# define retriever
|
55 |
+
retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": k})
|
56 |
+
# create a chatbot chain. Memory is managed externally.
|
57 |
+
question_generator_chain = LLMChain(llm=model, prompt=QA_CHAIN_PROMPT)
|
58 |
|
59 |
+
qa = ConversationalRetrievalChain.from_llm(
|
60 |
+
llm=model,
|
61 |
+
chain_type="stuff",
|
62 |
+
retriever=retriever,
|
63 |
+
return_source_documents=True,
|
64 |
+
return_generated_question=True,
|
65 |
+
)
|
66 |
|
67 |
# return qa
|
68 |
|
|
|
128 |
Helpful Answer:"""
|
129 |
QA_CHAIN_PROMPT = PromptTemplate.from_template(template)
|
130 |
|
131 |
+
# Updated greet function to handle file upload
|
132 |
+
def greet(message, pdf_file):
|
133 |
+
global chat_history
|
134 |
+
user_message = message
|
135 |
+
if pdf_file is not None:
|
136 |
+
# Save the uploaded PDF file
|
137 |
+
with open("uploaded_file.pdf", "wb") as f:
|
138 |
+
f.write(pdf_file.read())
|
139 |
+
a = load_db("uploaded_file.pdf", 3)
|
140 |
+
# else:
|
141 |
+
# a = load_db("temp.pdf", 3) # assuming you've uploaded the file and saved it as "temp.pdf"
|
142 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
r = a.invoke({"question": user_message, "chat_history": chat_history})
|
144 |
match = re.search(r'Helpful Answer:(.*)', r['answer'])
|
145 |
if match:
|
|
|
150 |
else:
|
151 |
return [{"role": "assistant", "content": "No helpful answer found."}]
|
152 |
|
153 |
+
# Gradio ChatInterface with file upload support
|
154 |
+
iface = gr.ChatInterface(fn=greet, examples=["hello", "hola", "merhaba"], title="Your Chatbot Title", additional_inputs="file")
|
155 |
+
iface.launch(share=True)
|