Spaces:
Runtime error
Runtime error
File size: 2,088 Bytes
0afafb1 14a59ca 0afafb1 9cb839e 0afafb1 9cb839e 0afafb1 1d7520f 0afafb1 1d7520f 9cb839e 0afafb1 1d7520f 0afafb1 9cb839e 1d7520f 0afafb1 | 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #!pip install python-dotenv
#pip install duckdb
import duckdb
#pip install llama_index_core
import os
#pip install openai
import openai
import textwrap
#pip install llama_index.vector_stores.duckdb
import llama_index.vector_stores.duckdb
#pip install llama-index-embeddings-openai
#import llama-index-embeddings-openai
import os
import openai
import textwrap
import os
file_path = 'persist/my_vector_store.duckdb'
# Check if file exists
if os.path.exists(file_path):
#Delete the file
os.remove(file_path)
print("File deleted successfully")
else:
print("File doesn't exist - first run - it's all good")
from dotenv import load_dotenv
load_dotenv()
api_key = os.getenv('OPENAI_API_KEY')
from openai import OpenAI
client = OpenAI(api_key=api_key)
print("OpenAI key generated")
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.vector_stores.duckdb import DuckDBVectorStore
from llama_index.core import StorageContext
print("Vector Stores Generated")
# Initialize DuckDB vector store with persistence
vector_store = DuckDBVectorStore("my_vector_store.duckdb", persist_dir="./persist/")
print("Initialized DuckDB vector store with peristance")
documents = SimpleDirectoryReader("PDFs").load_data()
print("PDFs loaded into Duckdb")
storage_context = StorageContext.from_defaults(vector_store=vector_store)
print("storage_context defined")
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)
print("Index generated from vector stores in DuckDB")
import gradio as gr
# Create a custom theme with blue as the primary color
theme = gr.themes.Default(primary_hue="blue")
def greet(query):
query_engine = index.as_query_engine()
response = query_engine.query(query)
strresponse = str(response)
#return(gradio.Markdown(strresponse))
#return(textwrap.fill(str(response), 80))
return(f"{response}")
#display(Markdown(f"<b>{response}</b>")
#return "Hello " + query + "!"
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
demo.launch(share=True)
|