Add gradio app
Browse files- README.md +1 -2
- app.py +97 -0
- package.json +0 -16
README.md
CHANGED
|
@@ -5,8 +5,7 @@ colorFrom: gray
|
|
| 5 |
colorTo: red
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
| 8 |
-
|
| 9 |
-
app_file: dist/index.html
|
| 10 |
short_description: LLM Demo of the One Big Beautiful Bill Act
|
| 11 |
---
|
| 12 |
|
|
|
|
| 5 |
colorTo: red
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
| 8 |
+
app_file: app.py
|
|
|
|
| 9 |
short_description: LLM Demo of the One Big Beautiful Bill Act
|
| 10 |
---
|
| 11 |
|
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!pip install python-dotenv
|
| 2 |
+
#pip install duckdb
|
| 3 |
+
#####
|
| 4 |
+
import duckdb
|
| 5 |
+
#pip install llama_index_core
|
| 6 |
+
import os
|
| 7 |
+
#pip install openai
|
| 8 |
+
import openai
|
| 9 |
+
import textwrap
|
| 10 |
+
#pip install llama_index.vector_stores.duckdb
|
| 11 |
+
import llama_index.vector_stores.duckdb
|
| 12 |
+
#pip install llama-index-embeddings-openai
|
| 13 |
+
#import llama-index-embeddings-openai
|
| 14 |
+
import os
|
| 15 |
+
import openai
|
| 16 |
+
import textwrap
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
file_path = 'persist/my_vector_store.duckdb'
|
| 21 |
+
|
| 22 |
+
# Check if file exists
|
| 23 |
+
if os.path.exists(file_path):
|
| 24 |
+
#Delete the file
|
| 25 |
+
os.remove(file_path)
|
| 26 |
+
print("File deleted successfully")
|
| 27 |
+
else:
|
| 28 |
+
print("File doesn't exist - first run - it's all good")
|
| 29 |
+
|
| 30 |
+
from dotenv import load_dotenv
|
| 31 |
+
load_dotenv()
|
| 32 |
+
|
| 33 |
+
api_key = os.getenv('OPENAI_API_KEY')
|
| 34 |
+
|
| 35 |
+
from openai import OpenAI
|
| 36 |
+
client = OpenAI(api_key=api_key)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
print("OpenAI key generated")
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
default_model = os.environ.get('OPENAI_DEFAULT_MODEL')
|
| 43 |
+
|
| 44 |
+
if default_model:
|
| 45 |
+
print(f"The default OpenAI model (from environment variable) is: {default_model}")
|
| 46 |
+
else:
|
| 47 |
+
print("The OPENAI_DEFAULT_MODEL environment variable is not set.")
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
|
| 52 |
+
from llama_index.vector_stores.duckdb import DuckDBVectorStore
|
| 53 |
+
from llama_index.core import StorageContext
|
| 54 |
+
|
| 55 |
+
print("Vector Stores Generated")
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# Initialize DuckDB vector store with persistence
|
| 59 |
+
vector_store = DuckDBVectorStore("my_vector_store.duckdb", persist_dir="./persist/")
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
print("Initialized DuckDB vector store with peristance")
|
| 63 |
+
|
| 64 |
+
documents = SimpleDirectoryReader("PDFs").load_data()
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
print("PDFs loaded into Duckdb")
|
| 68 |
+
|
| 69 |
+
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
| 70 |
+
|
| 71 |
+
print("storage_context defined")
|
| 72 |
+
|
| 73 |
+
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)
|
| 74 |
+
|
| 75 |
+
print("Index generated from vector stores in DuckDB")
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
import gradio as gr
|
| 80 |
+
|
| 81 |
+
# Create a custom theme with blue as the primary color
|
| 82 |
+
theme = gr.themes.Default(primary_hue="blue")
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def greet(query):
|
| 86 |
+
|
| 87 |
+
query_engine = index.as_query_engine()
|
| 88 |
+
response = query_engine.query(query)
|
| 89 |
+
strresponse = str(response)
|
| 90 |
+
#return(gradio.Markdown(strresponse))
|
| 91 |
+
#return(textwrap.fill(str(response), 80))
|
| 92 |
+
return(f"{response}")
|
| 93 |
+
#display(Markdown(f"<b>{response}</b>")
|
| 94 |
+
#return "Hello " + query + "!"
|
| 95 |
+
|
| 96 |
+
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 97 |
+
demo.launch(share=True)
|
package.json
DELETED
|
@@ -1,16 +0,0 @@
|
|
| 1 |
-
{
|
| 2 |
-
"name": "llm_demo",
|
| 3 |
-
"version": "1.0.0",
|
| 4 |
-
"description": "--- title: LLM Demo emoji: 📉 colorFrom: gray colorTo: red sdk: static pinned: false app_build_command: npm run build app_file: dist/index.html short_description: LLM Demo of the One Big Beautiful Bill Act ---",
|
| 5 |
-
"main": "index.js",
|
| 6 |
-
"scripts": {
|
| 7 |
-
"test": "echo \"Error: no test specified\" && exit 1"
|
| 8 |
-
},
|
| 9 |
-
"repository": {
|
| 10 |
-
"type": "git",
|
| 11 |
-
"url": "https://huggingface.co/spaces/GoData/LLM_Demo"
|
| 12 |
-
},
|
| 13 |
-
"keywords": [],
|
| 14 |
-
"author": "",
|
| 15 |
-
"license": "ISC"
|
| 16 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|