Spaces:
Sleeping
Sleeping
test file_research
Browse files- app.py +109 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import time
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
from getpass import getpass
|
6 |
+
from langchain.llms import replicate
|
7 |
+
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
8 |
+
from langchain.prompts import PromptTemplate
|
9 |
+
|
10 |
+
from PyPDF2 import PdfReader
|
11 |
+
from streamlit_extras.add_vertical_space import add_vertical_space
|
12 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
13 |
+
#from langchain.embeddings.openai import OpenAIEmbeddings
|
14 |
+
from langchain.vectorstores import faiss
|
15 |
+
|
16 |
+
load_dotenv()
|
17 |
+
REPLICATE_API_TOKEN = os.environ.get("REPLICATE_API_TOKEN")
|
18 |
+
|
19 |
+
with st.sidebar:
|
20 |
+
st.title("File Research using LLM")
|
21 |
+
st.markdown(''' Upload your file and ask questions and do Research''')
|
22 |
+
add_vertical_space(5)
|
23 |
+
pdf=st.file_uploader('Upload your file (PDF)', type='pdf')
|
24 |
+
if pdf is not None:
|
25 |
+
pdf_reader=PdfReader(pdf)
|
26 |
+
text=""
|
27 |
+
for page in pdf_reader.pages:
|
28 |
+
text+=page.extract_text()
|
29 |
+
text_splitter=RecursiveCharacterTextSplitter(
|
30 |
+
chunk_size=1000,
|
31 |
+
chunk_overlap=200,
|
32 |
+
length_function=len
|
33 |
+
)
|
34 |
+
chunks=text_splitter.split_text(text)
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
st.write('Made by ALOK')
|
40 |
+
|
41 |
+
|
42 |
+
def main():
|
43 |
+
st.header('Talk to your file')
|
44 |
+
os.environ["REPLICATE_API_TOKEN"]=REPLICATE_API_TOKEN
|
45 |
+
#embeddings=OpenAIEmbeddings()
|
46 |
+
#vectorstore=faiss.FAISS.from_texts(chunks, embedding=embeddings)
|
47 |
+
|
48 |
+
# The meta/llama-2-70b-chat model can stream output as it's running.
|
49 |
+
|
50 |
+
|
51 |
+
if "messages" not in st.session_state:
|
52 |
+
st.session_state.messages = []
|
53 |
+
|
54 |
+
# Display chat messages from history on app rerun
|
55 |
+
for message in st.session_state.messages:
|
56 |
+
with st.chat_message(message["role"]):
|
57 |
+
st.markdown(message["content"])
|
58 |
+
|
59 |
+
# React to user input
|
60 |
+
if prompt := st.chat_input("Type Here"):
|
61 |
+
# Display user message in chat message container
|
62 |
+
st.chat_message("user").markdown(prompt)
|
63 |
+
# Add user message to chat history
|
64 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
65 |
+
replite_api='r8_4fktoXrDGkgHY8uw1XlVtQJKQlAILKv0iBmPI'
|
66 |
+
|
67 |
+
|
68 |
+
# rep = replicate.Client(api_token=replite_api)
|
69 |
+
# output = replicate.run(
|
70 |
+
# "meta/llama-2-70b-chat:02e509c789964a7ea8736978a43525956ef40397be9033abf9fd2badfe68c9e3",
|
71 |
+
# input={"prompt": prompt}
|
72 |
+
# )
|
73 |
+
|
74 |
+
model="meta/llama-2-70b-chat:02e509c789964a7ea8736978a43525956ef40397be9033abf9fd2badfe68c9e3"
|
75 |
+
llm=replicate.Replicate(
|
76 |
+
streaming=True,
|
77 |
+
callbacks=[StreamingStdOutCallbackHandler()],
|
78 |
+
model=model,
|
79 |
+
model_kwargs={"temperature": 0.75, "max_length": 500, "top_p": 1},
|
80 |
+
replicate_api_token=REPLICATE_API_TOKEN
|
81 |
+
)
|
82 |
+
prompt = """
|
83 |
+
User: Answer the following yes/no question by reasoning step by step. Please don't provide incomplete answer. Can a dog drive a car?
|
84 |
+
Assistant:
|
85 |
+
"""
|
86 |
+
|
87 |
+
|
88 |
+
# Display assistant response in chat message container
|
89 |
+
with st.chat_message("assistant"):
|
90 |
+
message_placeholder = st.empty()
|
91 |
+
message_placeholder.markdown(llm(prompt) + "▌")
|
92 |
+
|
93 |
+
# # The predict method returns an iterator, and you can iterate over that output.
|
94 |
+
# response_till_now=''
|
95 |
+
# for item in output:
|
96 |
+
# response_till_now+=item
|
97 |
+
# time.sleep(0.03)
|
98 |
+
# message_placeholder.markdown(response_till_now + "▌")
|
99 |
+
# message_placeholder.markdown(response_till_now)
|
100 |
+
# response = f"AI: {response_till_now}"
|
101 |
+
|
102 |
+
|
103 |
+
# Add assistant response to chat history
|
104 |
+
# st.session_state.messages.append({"role": "assistant", "content": response})
|
105 |
+
# https://replicate.com/meta/llama-2-70b-chat/versions/02e509c789964a7ea8736978a43525956ef40397be9033abf9fd2badfe68c9e3/api#output-schema
|
106 |
+
#print(item, end="")
|
107 |
+
|
108 |
+
if __name__=='__main__':
|
109 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
PyPDF2
|
3 |
+
streamlit
|
4 |
+
replicate
|
5 |
+
python-dotenv
|
6 |
+
faiss-cpu
|
7 |
+
streamlit-extras
|