File size: 2,678 Bytes
5cac51b
 
 
 
b523938
5cac51b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc7060c
 
 
 
 
 
 
 
 
 
 
 
 
5cac51b
 
c3326cd
 
5cac51b
dc7060c
f0ba4e8
0e5960c
5cac51b
 
 
 
d003e39
5cac51b
 
 
 
 
 
 
 
 
 
dc7060c
5cac51b
 
 
 
 
 
 
dc7060c
 
5cac51b
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import streamlit as st 
import llama_index 
import logging
import sys
import openai
import os 
import wikipedia
from llama_index import VectorStoreIndex, SimpleDirectoryReader
from llama_index.indices.query.query_transform import HyDEQueryTransform
from llama_index.query_engine.transform_query_engine import TransformQueryEngine
# from llama_index.indices.vector_store import ChatGPTRetrievalPluginIndex
from llama_index.readers import ChatGPTRetrievalPluginReader

def get_wikipedia_document(topic):
    wiki_wiki = wikipedia.Wikipedia('en')

    page = wiki_wiki.page(topic)
    if page.exists():
        return page.text
    else:
        return None
    
def write_string_to_file(text, filename):
    with open(filename, 'w') as file:
        file.write(text)

def remove_scrollbar():
    # Apply custom CSS to remove the scrollbar
    st.markdown(
        """
        <style>
        .css-1l02zno {
            overflow: hidden !important;
        }
        </style>
        """,
        unsafe_allow_html=True
    )


def ui(): 
    st.title('Hyde for Wikipedia Pages')
    st.markdown("This is question Answering for Wikipedia made using LLama Index and Hyde")
    api_key = st.text_input('Enter your OpenAI key here: ')
    if api_key is not None and api_key != '':
        os.environ["OPENAI_API_KEY"] = api_key
        openai.api_key = os.environ["OPENAI_API_KEY"]
        logging.basicConfig(stream=sys.stdout, level=logging.INFO)
        logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))

        topic_name= st.text_input('Enter your topic for Wikipedia: ')
        if topic_name is not None and topic_name!='':
            page_object = wikipedia.page(topic_name)
            content = page_object.content
            filename = f"./Data/{topic_name}.txt"
            write_string_to_file(content, filename)

            documents = SimpleDirectoryReader(f'./Data/{topic_name}.txt"').load_data()
            index = VectorStoreIndex.from_documents(documents)


            query_str= st.text_input('Enter your query for the document: ')
            if query_str is not None and query_str!='':
                query_engine = index.as_query_engine()
                response = query_engine.query(query_str)
                hyde = HyDEQueryTransform(include_original=True)
                hyde_query_engine = TransformQueryEngine(query_engine, hyde)

                response = hyde_query_engine.query(query_str)
                query_bundle = hyde(query_str)
                hyde_doc = query_bundle.embedding_strs[0]     
                remove_scrollbar()
                st.text(hyde_doc)
            


if __name__=="__main__":
    ui()