Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,31 @@
|
|
1 |
import streamlit as st
|
2 |
from llama_index import SimpleDirectoryReader, GPTVectorStoreIndex
|
|
|
|
|
3 |
import os
|
4 |
import shutil
|
5 |
|
6 |
-
|
7 |
-
def save_file(files):
|
8 |
-
directory_name = 'tmp_docs'
|
9 |
-
|
10 |
-
# Remove existing files in the directory
|
11 |
-
if os.path.exists(directory_name):
|
12 |
-
for filename in os.listdir(directory_name):
|
13 |
-
file_path = os.path.join(directory_name, filename)
|
14 |
-
try:
|
15 |
-
if os.path.isfile(file_path):
|
16 |
-
os.remove(file_path)
|
17 |
-
except Exception as e:
|
18 |
-
print(f"Error: {e}")
|
19 |
-
|
20 |
-
# Save the new file with original filename
|
21 |
-
if files is not None:
|
22 |
-
for file in files:
|
23 |
-
file_name = file.name
|
24 |
-
file_path = os.path.join(directory_name, file_name)
|
25 |
-
with open(file_path, 'wb') as f:
|
26 |
-
shutil.copyfileobj(file, f)
|
27 |
-
|
28 |
|
29 |
def ingest(docs_dir):
|
30 |
documents = SimpleDirectoryReader(docs_dir).load_data()
|
31 |
index = GPTVectorStoreIndex.from_documents(documents)
|
32 |
return index
|
33 |
|
34 |
-
|
35 |
-
def get_answer(index, message):
|
36 |
-
response = query(index, message)
|
37 |
-
return [('Chatbot', ''.join(response.response))]
|
38 |
-
|
39 |
-
|
40 |
def query(index, query_text):
|
41 |
query_engine = index.as_query_engine()
|
42 |
response = query_engine.query(query_text)
|
43 |
return response
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
|
49 |
if os.environ['OPENAI_API_KEY']:
|
50 |
# Initialize chatbot history
|
51 |
chatbot = []
|
52 |
|
53 |
-
|
54 |
-
files = st.file_uploader('Upload Files', accept_multiple_files=True)
|
55 |
-
if files is not None:
|
56 |
-
save_file(files)
|
57 |
-
|
58 |
-
index = ingest('tmp_docs')
|
59 |
|
60 |
# Display message input component
|
61 |
message = st.text_input('Enter message')
|
|
|
1 |
import streamlit as st
|
2 |
from llama_index import SimpleDirectoryReader, GPTVectorStoreIndex
|
3 |
+
from langchain import OpenAI
|
4 |
+
import gradio as gr
|
5 |
import os
|
6 |
import shutil
|
7 |
|
8 |
+
os.environ['OPENAI_API_KEY'] = "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"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def ingest(docs_dir):
|
11 |
documents = SimpleDirectoryReader(docs_dir).load_data()
|
12 |
index = GPTVectorStoreIndex.from_documents(documents)
|
13 |
return index
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
def query(index, query_text):
|
16 |
query_engine = index.as_query_engine()
|
17 |
response = query_engine.query(query_text)
|
18 |
return response
|
19 |
+
|
20 |
+
def get_answer(index, message):
|
21 |
+
response = query(index, message)
|
22 |
+
return [('Chatbot', ''.join(response.response))]
|
23 |
|
24 |
if os.environ['OPENAI_API_KEY']:
|
25 |
# Initialize chatbot history
|
26 |
chatbot = []
|
27 |
|
28 |
+
index = ingest('temp_docs')
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
# Display message input component
|
31 |
message = st.text_input('Enter message')
|