Spaces:
Runtime error
Runtime error
create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.llms import OpenAI
|
2 |
+
from langchain.chains.qa_with_sources import load_qa_with_sources_chain
|
3 |
+
from langchain.docstore.document import Document
|
4 |
+
import requests
|
5 |
+
import pathlib
|
6 |
+
import subprocess
|
7 |
+
import tempfile
|
8 |
+
import os
|
9 |
+
import gradio as gr
|
10 |
+
|
11 |
+
# using a vector space for our search
|
12 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
13 |
+
from langchain.vectorstores.faiss import FAISS
|
14 |
+
from langchain.text_splitter import CharacterTextSplitter
|
15 |
+
|
16 |
+
#To get markdowns from github fo Gradio (/or your) repo
|
17 |
+
def get_github_docs(repo_owner, repo_name):
|
18 |
+
with tempfile.TemporaryDirectory() as d:
|
19 |
+
subprocess.check_call(
|
20 |
+
f"git clone https://github.com/{repo_owner}/{repo_name}.git .",
|
21 |
+
cwd=d,
|
22 |
+
shell=True,
|
23 |
+
)
|
24 |
+
git_sha = (
|
25 |
+
subprocess.check_output("git rev-parse HEAD", shell=True, cwd=d)
|
26 |
+
.decode("utf-8")
|
27 |
+
.strip()
|
28 |
+
)
|
29 |
+
repo_path = pathlib.Path(d)
|
30 |
+
markdown_files = list(repo_path.rglob("*.md")) + list(
|
31 |
+
repo_path.rglob("*.mdx")
|
32 |
+
)
|
33 |
+
for markdown_file in markdown_files:
|
34 |
+
try:
|
35 |
+
with open(markdown_file, "r") as f:
|
36 |
+
relative_path = markdown_file.relative_to(repo_path)
|
37 |
+
github_url = f"https://github.com/{repo_owner}/{repo_name}/blob/{git_sha}/{relative_path}"
|
38 |
+
yield Document(page_content=f.read(), metadata={"source": github_url})
|
39 |
+
except FileNotFoundError:
|
40 |
+
print(f"Could not open file: {markdown_file}")
|
41 |
+
|
42 |
+
#sources = get_github_docs("gradio-app", "gradio")
|
43 |
+
#source_chunks = []
|
44 |
+
#splitter = CharacterTextSplitter(separator=" ", chunk_size=1024, chunk_overlap=0)
|
45 |
+
|
46 |
+
#for source in sources:
|
47 |
+
# for chunk in splitter.split_text(source.page_content):
|
48 |
+
# source_chunks.append(Document(page_content=chunk, metadata=source.metadata))
|
49 |
+
|
50 |
+
#search_index = FAISS.from_documents(source_chunks, OpenAIEmbeddings()) #(source_chunks, OpenAIEmbeddings()) # <------
|
51 |
+
#chain = load_qa_with_sources_chain(OpenAI(temperature=0)) ## <<---------
|
52 |
+
|
53 |
+
|
54 |
+
#loading FAISS search index from disk
|
55 |
+
with open("search_index.pickle", "rb") as f:
|
56 |
+
search_index = pickle.load(f)
|
57 |
+
|
58 |
+
|
59 |
+
def print_answer(question, openai): #openai_embeddings
|
60 |
+
#search_index = get_search_index()
|
61 |
+
chain = load_qa_with_sources_chain(openai) #(OpenAI(temperature=0))
|
62 |
+
response = (
|
63 |
+
chain(
|
64 |
+
{
|
65 |
+
"input_documents": search_index.similarity_search(question, k=4),
|
66 |
+
"question": question,
|
67 |
+
},
|
68 |
+
return_only_outputs=True,
|
69 |
+
)["output_text"]
|
70 |
+
)
|
71 |
+
#print(response)
|
72 |
+
if len(r.split('\n')[-1].split())>2:
|
73 |
+
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()))])
|
74 |
+
else:
|
75 |
+
response = response.split('\n')[0] + ' <a href="' + response.split('\n')[-1].split() + '" target="_blank"><u>Click Link</u></a>'
|
76 |
+
return response
|
77 |
+
|
78 |
+
|
79 |
+
|
80 |
+
def chat(message, history, openai_api_key):
|
81 |
+
#openai_embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
|
82 |
+
openai = OpenAI(temperature=0, openai_api_key=openai_api_key )
|
83 |
+
#os.environ["OPENAI_API_KEY"] = openai_api_key
|
84 |
+
history = history or []
|
85 |
+
message = message.lower()
|
86 |
+
response = print_answer(message, openai) #openai_embeddings
|
87 |
+
history.append((message, response))
|
88 |
+
return history, history
|
89 |
+
|
90 |
+
#chatbot = gr.Chatbot().style(color_map=("green", "orange"))
|
91 |
+
|
92 |
+
with gr.Blocks() as demo:
|
93 |
+
gr.Markdown("""<h1><centre>LangChain - powered - Gradio-Helper-Bot </h1></centre>
|
94 |
+
""")
|
95 |
+
with gr.Row():
|
96 |
+
question = gr.Textbox(label = 'Type in your questions about Gradio here', placeholder = 'What is the role of "every" argument in a component')
|
97 |
+
openai_api_key = gr.Textbox()
|
98 |
+
state = gr.State()
|
99 |
+
chatbot = gr.Chatbot()
|
100 |
+
question.submit(chat, [question, state, openai_api_key], [chatbot, state])
|
101 |
+
|
102 |
+
if __name__ == "__main__":
|
103 |
+
demo.launch()
|