Spaces:
Sleeping
Sleeping
Commit ·
abe9de5
1
Parent(s): 5cb8733
added streamlit app
Browse files- app.py +49 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from langchain_pinecone import PineconeVectorStore
|
| 3 |
+
from langchain.memory import ConversationBufferMemory
|
| 4 |
+
from langchain.embeddings import SentenceTransformerEmbeddings
|
| 5 |
+
from typing import List
|
| 6 |
+
import torch
|
| 7 |
+
from langchain_core.vectorstores import VectorStoreRetriever
|
| 8 |
+
from langchain_community.llms import Together
|
| 9 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 10 |
+
from pinecone import Pinecone, ServerlessSpec
|
| 11 |
+
import os
|
| 12 |
+
|
| 13 |
+
pinecone_api_key = os.getenv('pinecone_api_key')
|
| 14 |
+
together_api_key = os.getenv('together_api_key')
|
| 15 |
+
|
| 16 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 17 |
+
if device == 'cuda':
|
| 18 |
+
print(torch.cuda.get_device_name(0))
|
| 19 |
+
|
| 20 |
+
index_name = 'lex-index'
|
| 21 |
+
embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 22 |
+
pinecone = PineconeVectorStore(index_name=index_name, embedding=embeddings,pinecone_api_key = pinecone_api_key, text_key='title')
|
| 23 |
+
|
| 24 |
+
st.title("🦜🔗 Lex Fridman Podcast QnA")
|
| 25 |
+
|
| 26 |
+
st.markdown("This app was developed by [Viren Dhanwani](https://github.com/virendhanwani) "
|
| 27 |
+
"by creating a Vector Database of transcripts of Lex Fridman Podcast using the [dataset](https://huggingface.co/datasets/jamescalam/lex-transcripts/viewer/default/train?row=30) "
|
| 28 |
+
"and by implementing the Langhcain QA chain")
|
| 29 |
+
|
| 30 |
+
def generate_response(input_text):
|
| 31 |
+
llm = Together(
|
| 32 |
+
model="mistralai/Mistral-7B-Instruct-v0.2",
|
| 33 |
+
temperature=0.7,
|
| 34 |
+
max_tokens=500,
|
| 35 |
+
top_k=50,
|
| 36 |
+
together_api_key= together_api_key
|
| 37 |
+
)
|
| 38 |
+
chain = load_qa_chain(llm, chain_type='stuff')
|
| 39 |
+
docs = pinecone.similarity_search(input_text, k=3)
|
| 40 |
+
response = chain.run(input_documents=docs, question=input_text)
|
| 41 |
+
return response
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
with st.container():
|
| 45 |
+
messages = st.container(height=500)
|
| 46 |
+
messages.chat_message("assistant").write('How may i help you ?')
|
| 47 |
+
if prompt := st.chat_input("Say something"):
|
| 48 |
+
messages.chat_message("user").write(prompt)
|
| 49 |
+
messages.chat_message("assistant").write(generate_response(prompt))
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain==0.1.12
|
| 2 |
+
langchain-community==0.0.28
|
| 3 |
+
langchain-pinecone==0.0.3
|
| 4 |
+
langchain-together==0.0.2.post1
|
| 5 |
+
langchain-core==0.1.32
|
| 6 |
+
pinecone-client==3.1.0
|
| 7 |
+
sentence-transformers==2.3.1
|