thoristhor commited on
Commit
c618779
β€’
1 Parent(s): 98264de

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import Optional, Tuple
3
+ import gradio as gr
4
+ from langchain.vectorstores import Pinecone
5
+ # initialize pinecone
6
+
7
+ Pinecone.init(
8
+ api_key="PINECONE_API_KEY", # find at app.pinecone.io
9
+ environment="PINECONE_ENV" # next to api key in console
10
+ )
11
+
12
+ vectorstore = Pinecone.from_texts(texts, embeddings, index_name="sethgodin")
13
+ from query_data import get_chain
14
+ from threading import Lock
15
+
16
+
17
+ def set_openai_api_key(api_key: str):
18
+ """Set the api key and return chain.
19
+ If no api_key, then None is returned.
20
+ """
21
+ if api_key:
22
+ os.environ["OPENAI_API_KEY"] = api_key
23
+ chain = get_chain(vectorstore)
24
+ os.environ["OPENAI_API_KEY"] = ""
25
+ return chain
26
+
27
+ class ChatWrapper:
28
+
29
+ def __init__(self):
30
+ self.lock = Lock()
31
+ def __call__(
32
+ self, api_key: str, inp: str, history: Optional[Tuple[str, str]], chain
33
+ ):
34
+ """Execute the chat functionality."""
35
+ self.lock.acquire()
36
+ try:
37
+ history = history or []
38
+ # If chain is None, that is because no API key was provided.
39
+ if chain is None:
40
+ history.append((inp, "Please paste your OpenAI key to use"))
41
+ return history, history
42
+ # Set OpenAI key
43
+ import openai
44
+ openai.api_key = api_key
45
+ # Run chain and append input.
46
+ output = chain({"question": inp, "chat_history": history})["answer"]
47
+ history.append((inp, output))
48
+ except Exception as e:
49
+ raise e
50
+ finally:
51
+ self.lock.release()
52
+ return history, history
53
+
54
+ chat = ChatWrapper()
55
+
56
+ block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
57
+
58
+ with block:
59
+ with gr.Row():
60
+ chatbot = gr.Chatbot()
61
+ with gr.Row():
62
+ message = gr.Textbox(
63
+ label="What's your question?",
64
+ placeholder="Ask questions about the most recent state of the union",
65
+ lines=1,
66
+ )
67
+ submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
68
+
69
+ gr.Examples(
70
+ examples=[
71
+ "Tell me a story about the bootstrapper's manifesto",
72
+ "What are the examples of ",
73
+ "What was his stance on Ukraine",
74
+ ],
75
+ inputs=message,
76
+ )
77
+
78
+ gr.HTML("Demo application of a LangChain chain.")
79
+
80
+ gr.HTML(
81
+ "<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain πŸ¦œοΈπŸ”—</a></center>"
82
+ )
83
+
84
+ state = gr.State()
85
+ agent_state = gr.State()
86
+
87
+ submit.click(chat, inputs=['OPENAI_API_KEY', message, state, agent_state], outputs=[chatbot, state])
88
+ message.submit(chat, inputs=["OPENAI_API_KEY", message, state, agent_state], outputs=[chatbot, state])
89
+
90
+
91
+ block.launch(debug=True)