File size: 1,669 Bytes
a76b7d1
 
 
 
 
 
 
 
 
 
 
 
 
 
4e77644
 
a76b7d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10c3b87
a76b7d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
741af02
a76b7d1
 
 
 
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
# app.py

import os
import gradio as gr
import openai
import logging
from pinecone import Pinecone, ServerlessSpec
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, StorageContext
from llama_index.vector_stores.pinecone import PineconeVectorStore

# 1. Configure Logging
logging.basicConfig(level=logging.INFO)

# 2. Set API Keys (Use environment variables for security)
os.environ["OPENAI_API_KEY"] 
os.environ["PINECONE_API_KEY"] 

# 3. Initialize Pinecone
pc = Pinecone(api_key=os.environ["PINECONE_API_KEY"])
index_name = "quickstart"

# Delete & Create Index if needed
if index_name in [idx["name"] for idx in pc.list_indexes()]:
    pc.delete_index(index_name)

pc.create_index(
    name=index_name,
    dimension=1536,
    metric="euclidean",
    spec=ServerlessSpec(cloud="aws", region="us-east-1"),
)

pinecone_index = pc.Index(index_name)

# 4. Load Documents
documents = SimpleDirectoryReader("./data").load_data()

# 5. Create Vector Index
vector_store = PineconeVectorStore(pinecone_index=pinecone_index)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)

# 6. Create Query Engine
query_engine = index.as_query_engine()

# 7. Gradio Interface
def query_document(user_query):
    response = query_engine.query(user_query)
    return str(response)

# 8. Gradio App
interface = gr.Interface(
    fn=query_document,
    inputs=gr.Textbox(label="Enter your query", placeholder="Ask something about the essay..."),
    outputs=gr.Textbox(label="Response"),
    title="HR Chat Bot"
)

if __name__ == "__main__":
    interface.launch()