Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files
.env
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
PALM="AIzaSyBVojf3nBKO_UITOwZtDVyAejW_2Qne1KY"
|
| 2 |
+
HOST= "pod1.sonity.net"
|
| 3 |
+
PORT= "8564"
|
| 4 |
+
USER= "sonity"
|
| 5 |
+
PASSWORD= "Docs4Sonity!"
|
| 6 |
+
DBNAME= "postgres"
|
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
colorFrom: red
|
| 5 |
-
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 4.
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: sonity_companies
|
| 3 |
+
app_file: demo.py
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
+
sdk_version: 4.1.2
|
|
|
|
|
|
|
| 6 |
---
|
|
|
|
|
|
demo.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import time
|
| 4 |
+
from tqdm import tqdm
|
| 5 |
+
import google.generativeai as palm
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from langchain.chains import LLMChain
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
from langchain.prompts import PromptTemplate
|
| 11 |
+
|
| 12 |
+
import os
|
| 13 |
+
|
| 14 |
+
import gradio as gr
|
| 15 |
+
import io
|
| 16 |
+
|
| 17 |
+
from langchain.llms import GooglePalm
|
| 18 |
+
import pandas as pd
|
| 19 |
+
#from yolopandas import pd
|
| 20 |
+
|
| 21 |
+
from langchain.embeddings import GooglePalmEmbeddings
|
| 22 |
+
from langchain.memory import ConversationBufferMemory
|
| 23 |
+
from dotenv import load_dotenv
|
| 24 |
+
from langchain.chains import RetrievalQA
|
| 25 |
+
|
| 26 |
+
load_dotenv()
|
| 27 |
+
|
| 28 |
+
from langchain.vectorstores.pgvector import PGVector
|
| 29 |
+
|
| 30 |
+
embeddings = GooglePalmEmbeddings(google_api_key=os.environ['PALM'])
|
| 31 |
+
|
| 32 |
+
host= os.environ['HOST']
|
| 33 |
+
port= os.environ['PORT']
|
| 34 |
+
user= os.environ['USER']
|
| 35 |
+
password= os.environ['PASSWORD']
|
| 36 |
+
dbname= os.environ['DBAME']
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
CONNECTION_STRING = f"postgresql+psycopg2://{user}:{password}@{host}:{port}/{dbname}?sslmode=disable"
|
| 40 |
+
|
| 41 |
+
store = PGVector(
|
| 42 |
+
collection_name="coms_data",
|
| 43 |
+
connection_string=CONNECTION_STRING,
|
| 44 |
+
embedding_function=embeddings,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
retriever = store.as_retriever(
|
| 48 |
+
search_kwargs={"k": 3}
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
llm = GooglePalm(temperature=0, google_api_key=os.environ['PALM'])
|
| 52 |
+
|
| 53 |
+
def answer(query):
|
| 54 |
+
# New chain to return context and sources
|
| 55 |
+
qa_stuff_with_sources = RetrievalQA.from_chain_type(
|
| 56 |
+
llm=llm,
|
| 57 |
+
chain_type="stuff",
|
| 58 |
+
retriever=retriever,
|
| 59 |
+
return_source_documents=True,
|
| 60 |
+
verbose=True,
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# To run the query, we use a different syntax since we're returning more than just the response text
|
| 64 |
+
responses = qa_stuff_with_sources({"query": query})
|
| 65 |
+
|
| 66 |
+
source_documents = responses["source_documents"]
|
| 67 |
+
source_content = [doc.page_content for doc in source_documents]
|
| 68 |
+
source_metadata = [doc.metadata for doc in source_documents]
|
| 69 |
+
|
| 70 |
+
result = responses['result']
|
| 71 |
+
result += "\n\n"
|
| 72 |
+
result += "Sources used:"
|
| 73 |
+
for i in range(len(source_content)):
|
| 74 |
+
|
| 75 |
+
result += "\n\n"
|
| 76 |
+
result += source_metadata[i]['company_name']
|
| 77 |
+
result += "\n\n"
|
| 78 |
+
result += source_metadata[i]['website']
|
| 79 |
+
return result
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
demo = gr.Interface(
|
| 83 |
+
|
| 84 |
+
fn=answer,
|
| 85 |
+
inputs=["text"],
|
| 86 |
+
outputs=["text"],
|
| 87 |
+
title="Ask Sonity",
|
| 88 |
+
)
|
| 89 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
google-ai-generativelanguage==0.1.0
|
| 2 |
+
google-api-core==2.14.0
|
| 3 |
+
google-auth==2.23.4
|
| 4 |
+
google-generativeai==0.1.0rc1
|
| 5 |
+
googleapis-common-protos==1.61.0
|
| 6 |
+
gradio==4.7.1
|
| 7 |
+
pgvector
|
| 8 |
+
psycopg2-binary
|
| 9 |
+
langchain==0.0.333
|
| 10 |
+
python-dotenv==1.0.0
|