File size: 5,253 Bytes
51009b9
 
 
 
 
 
 
 
 
5df7ef1
667f32f
51009b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd4f43e
51009b9
 
7fdb71a
51009b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36b5fbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51009b9
c43c8e9
 
51009b9
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
import openai

# 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>  """)
  gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">
        <div
        style="
            display: inline-flex;
            align-items: center;
            gap: 0.8rem;
            font-size: 1.75rem;
        "
        >
        <h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">
            Gradio QandA - LangChain Bot
        </h1>
        </div>
        <p style="margin-bottom: 10px; font-size: 94%">
        Hi, I'm a Q and A Gradio expert bot, start by typing in your OpenAI API key, questions/issues you are facing in your Gradio implementations and then press enter.<br>
        <a href="https://huggingface.co/spaces/ysharma/InstructPix2Pix_Chatbot?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate Space with GPU Upgrade for fast Inference & no queue<br> 
        Built using <a href="https://langchain.readthedocs.io/en/latest/" target="_blank">LangChain</a> and <a href="https://github.com/gradio-app/gradio" target="_blank">Gradio Github repo</a>
        </p>
    </div>""")  
  with gr.Row():
    question = gr.Textbox(label = 'Type in your questions about Gradio here and press Enter!', placeholder = 'What is the role of "every" argument in a component')
    openai_api_key = gr.Textbox(type='password', label="Enter your OpenAI API key here")
  state = gr.State()
  chatbot = gr.Chatbot()
  question.submit(chat, [question, state, openai_api_key], [chatbot, state])

if __name__ == "__main__":
    demo.launch()