File size: 4,040 Bytes
baba351
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import streamlit as st
import os
import time
from dotenv import load_dotenv
from getpass import getpass
from langchain.llms import replicate
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.prompts import PromptTemplate

from PyPDF2 import PdfReader
from streamlit_extras.add_vertical_space import add_vertical_space
from langchain.text_splitter import RecursiveCharacterTextSplitter
#from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import faiss

load_dotenv()
REPLICATE_API_TOKEN = os.environ.get("REPLICATE_API_TOKEN")

with st.sidebar:
    st.title("File Research using LLM")
    st.markdown(''' Upload your file and ask questions and do Research''')
    add_vertical_space(5)
    pdf=st.file_uploader('Upload your file (PDF)', type='pdf')
    if pdf is not None:
        pdf_reader=PdfReader(pdf)
        text=""
        for page in pdf_reader.pages:
            text+=page.extract_text()
        text_splitter=RecursiveCharacterTextSplitter(
            chunk_size=1000,
            chunk_overlap=200,
            length_function=len
        )
        chunks=text_splitter.split_text(text)
        
        
           
        
    st.write('Made by ALOK')
    

def main():
    st.header('Talk to your file')
    os.environ["REPLICATE_API_TOKEN"]=REPLICATE_API_TOKEN
    #embeddings=OpenAIEmbeddings()
    #vectorstore=faiss.FAISS.from_texts(chunks, embedding=embeddings)
    
    # The meta/llama-2-70b-chat model can stream output as it's running.

    
    if "messages" not in st.session_state:
        st.session_state.messages = []

    # Display chat messages from history on app rerun
    for message in st.session_state.messages:
        with st.chat_message(message["role"]):
            st.markdown(message["content"])

    # React to user input
    if prompt := st.chat_input("Type Here"):
        # Display user message in chat message container
        st.chat_message("user").markdown(prompt)
        # Add user message to chat history
        st.session_state.messages.append({"role": "user", "content": prompt})
        replite_api='r8_4fktoXrDGkgHY8uw1XlVtQJKQlAILKv0iBmPI'
        
        
        # rep = replicate.Client(api_token=replite_api)
        # output = replicate.run(
        # "meta/llama-2-70b-chat:02e509c789964a7ea8736978a43525956ef40397be9033abf9fd2badfe68c9e3",
        # input={"prompt": prompt}
        # )
        
        model="meta/llama-2-70b-chat:02e509c789964a7ea8736978a43525956ef40397be9033abf9fd2badfe68c9e3"
        llm=replicate.Replicate(
            streaming=True,
            callbacks=[StreamingStdOutCallbackHandler()],
            model=model,
            model_kwargs={"temperature": 0.75, "max_length": 500, "top_p": 1},
            replicate_api_token=REPLICATE_API_TOKEN
        )
        prompt = """
        User: Answer the following yes/no question by reasoning step by step. Please don't provide incomplete answer. Can a dog drive a car?
        Assistant:
        """
        
        
        # Display assistant response in chat message container
        with st.chat_message("assistant"):
            message_placeholder = st.empty()
        message_placeholder.markdown(llm(prompt) + "▌")
            
        # # The predict method returns an iterator, and you can iterate over that output.
        # response_till_now=''
        # for item in output:
        #     response_till_now+=item
        #     time.sleep(0.03)
        #     message_placeholder.markdown(response_till_now + "▌")
        # message_placeholder.markdown(response_till_now)
        # response = f"AI: {response_till_now}"
        
        
        # Add assistant response to chat history
        # st.session_state.messages.append({"role": "assistant", "content": response})
            # https://replicate.com/meta/llama-2-70b-chat/versions/02e509c789964a7ea8736978a43525956ef40397be9033abf9fd2badfe68c9e3/api#output-schema
            #print(item, end="")
    
if __name__=='__main__':
    main()