import gradio as gr
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader

# Load documents and build the index
documents = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()

# Define the function that handles the query
def query_document(query):
    response = query_engine.query(query)
    return str(response)

# Create a simple Gradio interface
interface = gr.Interface(
    fn=query_document,
    inputs=gr.Textbox(label="Enter your question", lines=2, placeholder="What do you want to know from the documents?"),
    outputs=gr.Textbox(label="Answer"),
    title="Document Q&A with LlamaIndex",
    description="Ask a question and get an answer based on documents stored in the 'data' folder."
)

# Launch the app
if __name__ == "__main__":
    interface.launch()