Spaces:
Runtime error
Runtime error
Commit
·
83df85e
1
Parent(s):
a6184a4
Upload 2 files
Browse files- app.py +118 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
#import your_module # Replace this with the name of the Python file containing your existing code
|
| 3 |
+
|
| 4 |
+
# Playing with the new Open AI API : gpt-4
|
| 5 |
+
#importing dependencies
|
| 6 |
+
import langchain
|
| 7 |
+
import openai
|
| 8 |
+
from langchain.chat_models import ChatOpenAI
|
| 9 |
+
from langchain import PromptTemplate
|
| 10 |
+
from langchain.prompts.chat import (
|
| 11 |
+
ChatPromptTemplate,
|
| 12 |
+
SystemMessagePromptTemplate,
|
| 13 |
+
AIMessagePromptTemplate,
|
| 14 |
+
HumanMessagePromptTemplate,
|
| 15 |
+
)
|
| 16 |
+
import os
|
| 17 |
+
from langchain.vectorstores import Chroma, Pinecone
|
| 18 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 19 |
+
import pinecone
|
| 20 |
+
import os
|
| 21 |
+
from langchain.chains.qa_with_sources import load_qa_with_sources_chain
|
| 22 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 23 |
+
from langchain.embeddings import HuggingFaceEmbeddings, SentenceTransformerEmbeddings
|
| 24 |
+
from supabase.client import Client, create_client
|
| 25 |
+
from langchain.vectorstores import SupabaseVectorStore
|
| 26 |
+
|
| 27 |
+
st.title("Rev79 Knowledge Base Assistant")
|
| 28 |
+
# Language selection
|
| 29 |
+
languages = [
|
| 30 |
+
"English",
|
| 31 |
+
"Mandarin Chinese",
|
| 32 |
+
"Spanish",
|
| 33 |
+
"Hindi",
|
| 34 |
+
"Arabic",
|
| 35 |
+
"Portuguese",
|
| 36 |
+
"Bengali",
|
| 37 |
+
"Russian",
|
| 38 |
+
"Japanese",
|
| 39 |
+
"French",
|
| 40 |
+
"Indonesian",
|
| 41 |
+
"German",
|
| 42 |
+
"Korean",
|
| 43 |
+
"Turkish"
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
selected_language = st.selectbox("Select language:", languages)
|
| 47 |
+
|
| 48 |
+
# Input question
|
| 49 |
+
question = st.text_input("Enter your question:")
|
| 50 |
+
|
| 51 |
+
system_template = """
|
| 52 |
+
You are a helpful AI Assistant. Use the following pieces of context to answer the user's question.
|
| 53 |
+
If you don't know the answer, just say that you don't know. Don't try to make up an answer.
|
| 54 |
+
Question are related to how project managemnet software works. The documentation seeks to help users naviagte and use the software.
|
| 55 |
+
Do not include "https://rev79" as a source. Source will always be longer URLs, for example,
|
| 56 |
+
"https://rev79.freshdesk.com/en/support/solutions/articles/47001227676"
|
| 57 |
+
Answer the question in the following language, {language}. You have to include the "SOURCES" at all times regardless of the language used.
|
| 58 |
+
Translate the word "SOURCES" in the language that is being used and make sure the "SOURCES" are in a new line.
|
| 59 |
+
|
| 60 |
+
Example:
|
| 61 |
+
|
| 62 |
+
```
|
| 63 |
+
Question: What is Rev79?
|
| 64 |
+
|
| 65 |
+
Answer: Rev79 is a project management platform named after God's promise in Revelation 7:9 of all languages communities being included in his eternal purpose
|
| 66 |
+
of blessing and recreation. The platform aims to help organizations, teams, and communities move forward towards this vision by providing tools for
|
| 67 |
+
managing projects and facilitating Bible translation and integral mission in all language communities. Rev79 can be used to envision, organize, collaborate,
|
| 68 |
+
and transform projects and activities.
|
| 69 |
+
|
| 70 |
+
SOURCES:
|
| 71 |
+
- https://rev79.freshdesk.com/en/support/solutions/articles/47001223622-what-is-the-rev79-app-where-did-it-come-from-
|
| 72 |
+
```
|
| 73 |
+
----------------
|
| 74 |
+
{summaries}"""
|
| 75 |
+
|
| 76 |
+
# Submit button
|
| 77 |
+
|
| 78 |
+
if st.button("Submit"):
|
| 79 |
+
# Call the function from your_module with the selected language and question
|
| 80 |
+
OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
|
| 81 |
+
PINECONE_API_KEY = st.secrets["PINECONE_API_KEY"]
|
| 82 |
+
PINECONE_API_ENV = st.secrets["PINECONE_API_ENV"]
|
| 83 |
+
supabase_url = st.secrets["SUPABASE_URL"]
|
| 84 |
+
supabase_key = st.secrets["SUPABASE_KEY"]
|
| 85 |
+
|
| 86 |
+
#Use OpenAI's embedding
|
| 87 |
+
Embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/stsb-xlm-r-multilingual')
|
| 88 |
+
|
| 89 |
+
# initialize pinecone
|
| 90 |
+
supabase: Client = create_client(supabase_url, supabase_key)
|
| 91 |
+
|
| 92 |
+
docsearch = SupabaseVectorStore(embedding=Embeddings, table_name='documents', client=supabase)
|
| 93 |
+
|
| 94 |
+
st.markdown("Assistant is typing...")
|
| 95 |
+
|
| 96 |
+
messages = [
|
| 97 |
+
SystemMessagePromptTemplate.from_template(system_template),
|
| 98 |
+
HumanMessagePromptTemplate.from_template("{question}")
|
| 99 |
+
#SystemMessage(content=system_message),
|
| 100 |
+
#HumanMessage(content="{question}")
|
| 101 |
+
]
|
| 102 |
+
prompt = ChatPromptTemplate.from_messages(messages)
|
| 103 |
+
|
| 104 |
+
docs = docsearch.similarity_search(question)
|
| 105 |
+
chain_type_kwargs = {"prompt": prompt}
|
| 106 |
+
chain = load_qa_with_sources_chain(
|
| 107 |
+
ChatOpenAI(model_name="gpt-4", openai_api_key=OPENAI_API_KEY),
|
| 108 |
+
chain_type="stuff",
|
| 109 |
+
prompt=prompt
|
| 110 |
+
)
|
| 111 |
+
text = chain({"input_documents": docs, "question": question, "language":selected_language}, return_only_outputs=True)
|
| 112 |
+
|
| 113 |
+
result = text['output_text'] # Replace 'run' with the appropriate function in your module
|
| 114 |
+
|
| 115 |
+
# Display a header
|
| 116 |
+
st.header("Answer")
|
| 117 |
+
st.write(result)
|
| 118 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain
|
| 2 |
+
openai
|
| 3 |
+
streamlit
|
| 4 |
+
pinecone-client
|
| 5 |
+
beautifulsoup4
|
| 6 |
+
sentence_transformers
|
| 7 |
+
supabase
|