Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
|
5 |
+
|
6 |
+
API_KEY = "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"
|
7 |
+
|
8 |
+
# Write the API key to the .env file
|
9 |
+
with open(".env", "w") as env_file:
|
10 |
+
env_file.write(f"openai_api_key={API_KEY}\n")
|
11 |
+
|
12 |
+
#echo openai_api_key="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" > .env
|
13 |
+
|
14 |
+
load_dotenv(".env")
|
15 |
+
|
16 |
+
openai.api_key = os.environ.get("openai_api_key")
|
17 |
+
|
18 |
+
os.environ["OPENAI_API_KEY"] = openai.api_key
|
19 |
+
|
20 |
+
with open("sample_awards.csv") as f:
|
21 |
+
sample_awards = f.read()
|
22 |
+
|
23 |
+
from langchain.text_splitter import CharacterTextSplitter
|
24 |
+
|
25 |
+
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0, separator = "\n")
|
26 |
+
texts = text_splitter.split_text(sample_awards)
|
27 |
+
|
28 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
29 |
+
|
30 |
+
embeddings = OpenAIEmbeddings()
|
31 |
+
|
32 |
+
from langchain.vectorstores import Chroma
|
33 |
+
|
34 |
+
docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))]).as_retriever()
|
35 |
+
|
36 |
+
from langchain.chains.question_answering import load_qa_chain
|
37 |
+
from langchain.llms import OpenAI
|
38 |
+
|
39 |
+
chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")
|
40 |
+
|
41 |
+
def question(text):
|
42 |
+
query = text
|
43 |
+
docs = docsearch.get_relevant_documents(query)
|
44 |
+
return chain.run(input_documents=docs, question=query)
|
45 |
+
|
46 |
+
import gradio as gr
|
47 |
+
|
48 |
+
gr.Interface(
|
49 |
+
question,
|
50 |
+
inputs="text",
|
51 |
+
outputs="text",
|
52 |
+
title="Awards Question Answering",
|
53 |
+
).launch()
|