Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +71 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import llama_index
|
| 3 |
+
import logging
|
| 4 |
+
import sys
|
| 5 |
+
import os
|
| 6 |
+
import wikipedia
|
| 7 |
+
from llama_index import VectorStoreIndex, SimpleDirectoryReader
|
| 8 |
+
from llama_index.indices.query.query_transform import HyDEQueryTransform
|
| 9 |
+
from llama_index.query_engine.transform_query_engine import TransformQueryEngine
|
| 10 |
+
# from llama_index.indices.vector_store import ChatGPTRetrievalPluginIndex
|
| 11 |
+
from llama_index.readers import ChatGPTRetrievalPluginReader
|
| 12 |
+
from IPython.display import Markdown, display
|
| 13 |
+
|
| 14 |
+
def get_wikipedia_document(topic):
|
| 15 |
+
wiki_wiki = wikipedia.Wikipedia('en')
|
| 16 |
+
|
| 17 |
+
page = wiki_wiki.page(topic)
|
| 18 |
+
if page.exists():
|
| 19 |
+
return page.text
|
| 20 |
+
else:
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
def write_string_to_file(text, filename):
|
| 24 |
+
with open(filename, 'w') as file:
|
| 25 |
+
file.write(text)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def ui():
|
| 29 |
+
|
| 30 |
+
api_key = st.text_input('Enter your OpenAI key here: ')
|
| 31 |
+
if api_key is not None:
|
| 32 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
| 33 |
+
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
| 34 |
+
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
|
| 35 |
+
|
| 36 |
+
topic_name= st.text_input('Enter your topic for Wikipedia: ')
|
| 37 |
+
if topic_name is not None:
|
| 38 |
+
page_object = wikipedia.page(topic_name)
|
| 39 |
+
content = page_object.content
|
| 40 |
+
filename = f"./Data/{topic_name}.txt"
|
| 41 |
+
write_string_to_file(content, filename)
|
| 42 |
+
|
| 43 |
+
documents = SimpleDirectoryReader(f'./Data/{topic_name}.txt"').load_data()
|
| 44 |
+
index = VectorStoreIndex.from_documents(documents)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
query_str= st.text_input('Enter your query for the document: ')
|
| 48 |
+
if query_str is not None:
|
| 49 |
+
query_engine = index.as_query_engine()
|
| 50 |
+
response = query_engine.query(query_str)
|
| 51 |
+
hyde = HyDEQueryTransform(include_original=True)
|
| 52 |
+
hyde_query_engine = TransformQueryEngine(query_engine, hyde)
|
| 53 |
+
|
| 54 |
+
response = hyde_query_engine.query(query_str)
|
| 55 |
+
query_bundle = hyde(query_str)
|
| 56 |
+
hyde_doc = query_bundle.embedding_strs[0]
|
| 57 |
+
st.text(hyde_doc)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
if __name__=="__main__":
|
| 62 |
+
ui()
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
llama_index
|
| 3 |
+
openai
|
| 4 |
+
tiktoken
|
| 5 |
+
chromadb
|
| 6 |
+
wikipedia
|