ysharma's picture
ysharma HF staff
update password type field textbox
1335864
raw
history blame
4.11 kB
from langchain.llms import OpenAI
from langchain.chains.qa_with_sources import load_qa_with_sources_chain
from langchain.docstore.document import Document
import requests
import pathlib
import subprocess
import tempfile
import os
import gradio as gr
import pickle
# using a vector space for our search
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores.faiss import FAISS
from langchain.text_splitter import CharacterTextSplitter
#To get markdowns from github fo Gradio (/or your) repo
def get_github_docs(repo_owner, repo_name):
with tempfile.TemporaryDirectory() as d:
subprocess.check_call(
f"git clone https://github.com/{repo_owner}/{repo_name}.git .",
cwd=d,
shell=True,
)
git_sha = (
subprocess.check_output("git rev-parse HEAD", shell=True, cwd=d)
.decode("utf-8")
.strip()
)
repo_path = pathlib.Path(d)
markdown_files = list(repo_path.rglob("*.md")) + list(
repo_path.rglob("*.mdx")
)
for markdown_file in markdown_files:
try:
with open(markdown_file, "r") as f:
relative_path = markdown_file.relative_to(repo_path)
github_url = f"https://github.com/{repo_owner}/{repo_name}/blob/{git_sha}/{relative_path}"
yield Document(page_content=f.read(), metadata={"source": github_url})
except FileNotFoundError:
print(f"Could not open file: {markdown_file}")
#sources = get_github_docs("gradio-app", "gradio")
#source_chunks = []
#splitter = CharacterTextSplitter(separator=" ", chunk_size=1024, chunk_overlap=0)
#for source in sources:
# for chunk in splitter.split_text(source.page_content):
# source_chunks.append(Document(page_content=chunk, metadata=source.metadata))
#search_index = FAISS.from_documents(source_chunks, OpenAIEmbeddings()) #(source_chunks, OpenAIEmbeddings()) # <------
#chain = load_qa_with_sources_chain(OpenAI(temperature=0)) ## <<---------
#loading FAISS search index from disk
with open("search_index.pickle", "rb") as f:
search_index = pickle.load(f)
def print_answer(question, openai): #openai_embeddings
#search_index = get_search_index()
chain = load_qa_with_sources_chain(openai) #(OpenAI(temperature=0))
response = (
chain(
{
"input_documents": search_index.similarity_search(question, k=4),
"question": question,
},
return_only_outputs=True,
)["output_text"]
)
#print(response)
if len(response.split('\n')[-1].split())>2:
response = response.split('\n')[0] + ', '.join([' <a href="' + response.split('\n')[-1].split()[i] + '" target="_blank"><u>Click Link' + str(i) + '</u></a>' for i in range(1,len(response.split('\n')[-1].split()))])
else:
response = response.split('\n')[0] + ' <a href="' + response.split('\n')[-1].split()[-1] + '" target="_blank"><u>Click Link</u></a>'
return response
def chat(message, history, openai_api_key):
#openai_embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
openai = OpenAI(temperature=0, openai_api_key=openai_api_key )
#os.environ["OPENAI_API_KEY"] = openai_api_key
history = history or []
message = message.lower()
response = print_answer(message, openai) #openai_embeddings
history.append((message, response))
return history, history
#chatbot = gr.Chatbot().style(color_map=("green", "orange"))
with gr.Blocks() as demo:
gr.Markdown("""<h1><centre>LangChain - powered - Gradio-Helper-Bot </h1></centre>
""")
with gr.Row():
question = gr.Textbox(label = 'Type in your questions about Gradio here', placeholder = 'What is the role of "every" argument in a component')
openai_api_key = gr.Textbox(type='password')
state = gr.State()
chatbot = gr.Chatbot()
question.submit(chat, [question, state, openai_api_key], [chatbot, state])
if __name__ == "__main__":
demo.launch()