Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
|
| 3 |
+
|
| 4 |
+
# Load documents from "data" directory and build the index
|
| 5 |
+
documents = SimpleDirectoryReader("data").load_data()
|
| 6 |
+
index = VectorStoreIndex.from_documents(documents=documents)
|
| 7 |
+
query_engine = index.as_query_engine()
|
| 8 |
+
|
| 9 |
+
# Function to handle user queries
|
| 10 |
+
def query_document(query):
|
| 11 |
+
response = query_engine.query(query)
|
| 12 |
+
return str(response)
|
| 13 |
+
|
| 14 |
+
# Build Gradio app using Blocks for better layout and UX
|
| 15 |
+
with gr.Blocks(css=".gradio-container {font-family: 'Arial'; background-color: #fafafa;}") as demo:
|
| 16 |
+
gr.Markdown("<h1 style='text-align: center;'>📄 RAG Application with LlamaIndex</h1>")
|
| 17 |
+
gr.Markdown(
|
| 18 |
+
"Ask questions about the documents stored in the local directory. "
|
| 19 |
+
"This app uses Retrieval-Augmented Generation (RAG) powered by LlamaIndex."
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
with gr.Box():
|
| 23 |
+
query_input = gr.Textbox(
|
| 24 |
+
label="Enter your query",
|
| 25 |
+
placeholder="e.g., What is the refund policy mentioned in the document?",
|
| 26 |
+
lines=3
|
| 27 |
+
)
|
| 28 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
| 29 |
+
response_output = gr.Textbox(label="Response", lines=8)
|
| 30 |
+
|
| 31 |
+
submit_btn.click(fn=query_document, inputs=query_input, outputs=response_output)
|
| 32 |
+
|
| 33 |
+
# Run the app
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
demo.launch()
|