nehatarey commited on
Commit
304b828
1 Parent(s): 50faae3

updated app.py

Browse files
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.embeddings.openai import OpenAIEmbeddings
2
+ from langchain.vectorstores import Chroma
3
+ from langchain.text_splitter import CharacterTextSplitter
4
+ from langchain.chains.question_answering import load_qa_chain
5
+ from langchain.llms import OpenAI
6
+ from langchain.document_loaders import BSHTMLLoader, DirectoryLoader
7
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
8
+ from langchain.embeddings.openai import OpenAIEmbeddings
9
+ from langchain.chat_models import ChatOpenAI
10
+ from langchain.chains import RetrievalQA
11
+ import os
12
+
13
+ import gradio as gr
14
+
15
+
16
+ print("LOGGING")
17
+
18
+ # Load the files
19
+ directory = './data/'
20
+ bshtml_dir_loader = DirectoryLoader(directory, loader_cls=BSHTMLLoader)
21
+ data = bshtml_dir_loader.load()
22
+ #Split the document into chunks
23
+ text_splitter = RecursiveCharacterTextSplitter(
24
+ chunk_size = 1000,
25
+ chunk_overlap = 20,
26
+ length_function = len,
27
+ )
28
+
29
+ documents = text_splitter.split_documents(data)
30
+
31
+ # Create the embeddings
32
+ embeddings = OpenAIEmbeddings()
33
+ print("Got embeddings")
34
+ #Input openai api key
35
+ # Check if the OpenAI API Key is empty
36
+ def check_api_key(api_key):
37
+ if api_key == "":
38
+ return False
39
+ return True
40
+
41
+
42
+
43
+ # Create the Gradio interface
44
+ def gradio_interface(api_key):
45
+ if not check_api_key(api_key):
46
+ return "API key is empty. Please provide a valid API key."
47
+ else:
48
+ # Create the second page interface
49
+ question_input = gr.inputs.Textbox(label="Question")
50
+ answer_output = gr.outputs.Textbox(label="Answer")
51
+
52
+ # Function for the second page interface
53
+ def infer_question(question):
54
+ answer = make_inference(question)
55
+ return answer
56
+
57
+ # Define the second page of the Gradio interface
58
+ second_page = gr.Interface(fn=infer_question, inputs=question_input, outputs=answer_output, title="Ask me about Ted Lasso 📺⚽")
59
+
60
+ # Launch the second page interface
61
+ second_page.launch()
62
+
63
+ # Define the input interface for the first page (OpenAI API Key entry)
64
+ api_key_input = gr.inputs.Textbox(label="OpenAI API Key")
65
+
66
+ # Create the first page interface
67
+ first_page = gr.Interface(fn=gradio_interface, inputs=api_key_input, title="OpenAI API Key Entry")
68
+
69
+ # Launch the first page interface
70
+ first_page.launch()
71
+
72
+ print("got API key")
73
+ #Load the model
74
+ llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo",openai_api_key=api_key_input)
75
+
76
+ # Create vectorstore to use as the index
77
+ vectordb = Chroma.from_documents(documents=documents, embedding=embeddings)
78
+
79
+ #expose this index in a retriever object
80
+ doc_retriever = vectordb.as_retriever()
81
+ print("Created retriever")
82
+ #create the QA chain
83
+ ted_lasso_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=doc_retriever)
84
+ # Function to make inferences and provide answers
85
+ def make_inference(question):
86
+ # Perform inference using the question and return the answer
87
+ print("reached inference")
88
+ return ted_lasso_qa.run(question)
89
+
90
+
91
+
data/Ted Lasso S1 E01 _Pilot_ _ Recap - TV Tropes.html ADDED
The diff for this file is too large to render. See raw diff
 
data/Ted Lasso S1 E02 _Biscuits_ _ Recap - TV Tropes.html ADDED
The diff for this file is too large to render. See raw diff
 
data/Ted Lasso S1 E03 _Trent Crimm_ The Independent_ _ Recap - TV Tropes.html ADDED
The diff for this file is too large to render. See raw diff
 
data/Ted Lasso S1 E04 _For the Children_ _ Recap - TV Tropes.html ADDED
The diff for this file is too large to render. See raw diff
 
data/Ted Lasso S1 E05 _Tan Lines_ _ Recap - TV Tropes.html ADDED
The diff for this file is too large to render. See raw diff
 
data/Ted Lasso S1 E06 _Two Aces_ _ Recap - TV Tropes.html ADDED
The diff for this file is too large to render. See raw diff
 
data/Ted Lasso S1 E07 _Make Rebecca Great Again_ _ Recap - TV Tropes.html ADDED
The diff for this file is too large to render. See raw diff
 
data/Ted Lasso S1 E08 _The Diamond Dogs_ _ Recap - TV Tropes.html ADDED
The diff for this file is too large to render. See raw diff
 
data/Ted Lasso S1 E09 _All Apologies_ _ Recap - TV Tropes.html ADDED
The diff for this file is too large to render. See raw diff
 
data/Ted Lasso S1 E10 _The Hope that Kills You_ _ Recap - TV Tropes.html ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ openai
2
+ tiktoken
3
+ langchain
4
+ chromadb
5
+ beautifulsoup4