Spaces:
Runtime error
Runtime error
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
import pickle
|
11 |
+
|
12 |
+
# using a vector space for our search
|
13 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
14 |
+
from langchain.vectorstores.faiss import FAISS
|
15 |
+
from langchain.text_splitter import CharacterTextSplitter
|
16 |
+
|
17 |
+
#loading FAISS search index from disk
|
18 |
+
with open("search_index.pickle", "rb") as f:
|
19 |
+
search_index = pickle.load(f)
|
20 |
+
|
21 |
+
#Get GPT3 response using Langchain
|
22 |
+
def print_answer(question, openai): #openai_embeddings
|
23 |
+
#search_index = get_search_index()
|
24 |
+
chain = load_qa_with_sources_chain(openai) #(OpenAI(temperature=0))
|
25 |
+
response = (
|
26 |
+
chain(
|
27 |
+
{
|
28 |
+
"input_documents": search_index.similarity_search(question, k=4),
|
29 |
+
"question": question,
|
30 |
+
},
|
31 |
+
return_only_outputs=True,
|
32 |
+
)["output_text"]
|
33 |
+
)
|
34 |
+
if len(response.split('\n')[-1].split())>2:
|
35 |
+
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()))])
|
36 |
+
else:
|
37 |
+
response = response.split('\n')[0] + ' <a href="' + response.split('\n')[-1].split()[-1] + '" target="_blank"><u>Click Link</u></a>'
|
38 |
+
return response
|
39 |
+
|
40 |
+
|
41 |
+
def chat(message, history, openai_api_key):
|
42 |
+
#openai_embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
|
43 |
+
openai = OpenAI(temperature=0, openai_api_key=openai_api_key )
|
44 |
+
#os.environ["OPENAI_API_KEY"] = openai_api_key
|
45 |
+
history = history or []
|
46 |
+
message = message.lower()
|
47 |
+
response = print_answer(message, openai) #openai_embeddings
|
48 |
+
history.append((message, response))
|
49 |
+
return history, history
|
50 |
+
|
51 |
+
|
52 |
+
with gr.Blocks() as demo:
|
53 |
+
gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">
|
54 |
+
<div
|
55 |
+
style="
|
56 |
+
display: inline-flex;
|
57 |
+
align-items: center;
|
58 |
+
gap: 0.8rem;
|
59 |
+
font-size: 1.75rem;
|
60 |
+
"
|
61 |
+
>
|
62 |
+
<h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">
|
63 |
+
lumukso QandA - LangChain Bot
|
64 |
+
</h1>
|
65 |
+
</div>
|
66 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
67 |
+
Hi, I'm a Q and A lumukso expert bot, start by typing in your OpenAI API key, questions/issues you are facing in your lumukso implementations and then press enter.<br>
|
68 |
+
<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>
|
69 |
+
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</a> for the lumukso Repo
|
70 |
+
</p>
|
71 |
+
</div>""")
|
72 |
+
with gr.Row():
|
73 |
+
question = gr.Textbox(label = 'Type in your questions about lumukso here and press Enter!', placeholder = 'What questions do you want to ask about the lumukso library?')
|
74 |
+
openai_api_key = gr.Textbox(type='password', label="Enter your OpenAI API key here")
|
75 |
+
state = gr.State()
|
76 |
+
chatbot = gr.Chatbot()
|
77 |
+
question.submit(chat, [question, state, openai_api_key], [chatbot, state])
|
78 |
+
|
79 |
+
if __name__ == "__main__":
|
80 |
+
demo.launch()
|