decodingdatascience commited on
Commit
60abb2d
·
verified ·
1 Parent(s): c3fd21d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -23
app.py CHANGED
@@ -1,35 +1,25 @@
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()
 
1
  import gradio as gr
2
  from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
3
 
4
+ # Load documents and build the index
5
  documents = SimpleDirectoryReader("data").load_data()
6
+ index = VectorStoreIndex.from_documents(documents)
7
  query_engine = index.as_query_engine()
8
 
9
+ # Define the function that handles the query
10
  def query_document(query):
11
  response = query_engine.query(query)
12
  return str(response)
13
 
14
+ # Create a simple Gradio interface
15
+ interface = gr.Interface(
16
+ fn=query_document,
17
+ inputs=gr.Textbox(label="Enter your question", lines=2, placeholder="What do you want to know from the documents?"),
18
+ outputs=gr.Textbox(label="Answer"),
19
+ title="Document Q&A with LlamaIndex",
20
+ description="Ask a question and get an answer based on documents stored in the 'data' folder."
21
+ )
22
 
23
+ # Launch the app
 
 
 
 
 
 
 
 
 
 
 
24
  if __name__ == "__main__":
25
+ interface.launch()