Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import os
|
3 |
+
import logging
|
4 |
+
import gradio as gr
|
5 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, StorageContext
|
6 |
+
from llama_index.vector_stores.pinecone import PineconeVectorStore
|
7 |
+
from pinecone import Pinecone, ServerlessSpec
|
8 |
+
|
9 |
+
# Logging setup
|
10 |
+
logging.basicConfig(level=logging.INFO)
|
11 |
+
|
12 |
+
|
13 |
+
api_key = os.environ["PINECONE_API_KEY"]
|
14 |
+
|
15 |
+
# Initialize Pinecone
|
16 |
+
pc = Pinecone(api_key=api_key)
|
17 |
+
index_name = "quickstart"
|
18 |
+
dimension = 1536
|
19 |
+
|
20 |
+
# Delete index if exists (optional)
|
21 |
+
if index_name in [idx['name'] for idx in pc.list_indexes()]:
|
22 |
+
pc.delete_index(index_name)
|
23 |
+
|
24 |
+
# Create new index
|
25 |
+
pc.create_index(
|
26 |
+
name=index_name,
|
27 |
+
dimension=dimension,
|
28 |
+
metric="euclidean",
|
29 |
+
spec=ServerlessSpec(cloud="aws", region="us-east-1"),
|
30 |
+
)
|
31 |
+
|
32 |
+
pinecone_index = pc.Index(index_name)
|
33 |
+
|
34 |
+
# Download data if not exists
|
35 |
+
os.makedirs("data/paul_graham", exist_ok=True)
|
36 |
+
file_path = "data/paul_graham/paul_graham_essay.txt"
|
37 |
+
if not os.path.exists(file_path):
|
38 |
+
import urllib.request
|
39 |
+
urllib.request.urlretrieve(
|
40 |
+
"https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt",
|
41 |
+
file_path
|
42 |
+
)
|
43 |
+
|
44 |
+
# Load documents
|
45 |
+
documents = SimpleDirectoryReader("data/paul_graham/").load_data()
|
46 |
+
|
47 |
+
# Build vector index
|
48 |
+
vector_store = PineconeVectorStore(pinecone_index=pinecone_index)
|
49 |
+
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
50 |
+
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)
|
51 |
+
|
52 |
+
query_engine = index.as_query_engine()
|
53 |
+
|
54 |
+
# Gradio UI function
|
55 |
+
def query_doc(prompt):
|
56 |
+
try:
|
57 |
+
response = query_engine.query(prompt)
|
58 |
+
return str(response)
|
59 |
+
except Exception as e:
|
60 |
+
return f"Error: {str(e)}"
|
61 |
+
|
62 |
+
# Launch Gradio app
|
63 |
+
gr.Interface(
|
64 |
+
fn=query_doc,
|
65 |
+
inputs=gr.Textbox(label="Ask a question about the document"),
|
66 |
+
outputs=gr.Textbox(label="Answer"),
|
67 |
+
title="Paul Graham Document QA (LlamaIndex + Pinecone)",
|
68 |
+
description="Ask questions based on the indexed Paul Graham essay. Powered by LlamaIndex & Pinecone."
|
69 |
+
).launch()
|