File size: 8,984 Bytes
2c771c2
035189e
2c771c2
 
 
 
0a333a5
 
 
2c771c2
 
 
 
 
 
 
 
 
 
 
921de0f
 
 
 
 
 
12cd8a8
 
 
 
 
 
2c771c2
 
 
 
adf44ff
2c771c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9be1f67
2c771c2
 
 
 
9be1f67
2c771c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12cd8a8
2c771c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9be1f67
 
2c771c2
 
 
 
 
 
 
 
0a333a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c771c2
0a333a5
 
 
 
 
 
 
2c771c2
 
 
 
0a333a5
 
2c771c2
 
 
 
 
 
 
 
 
 
 
 
05e9128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbecc8a
9be1f67
05e9128
 
 
c9bceab
9be1f67
05e9128
c9bceab
2c771c2
 
 
 
 
 
 
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
from openai import OpenAI
import streamlit as st
import openai
import os 
import time
#from roles import *
import io
from pypdf import PdfReader  
#from langchain_community.document_loaders import PyPDFLoader
import tempfile
from RAG import load_graph,text_splitter
import torch 
from sentence_transformers import SentenceTransformer
import torch
import uuid 
import re
import requests
from cloudhands import CloudHandsPayment
from database_center import db_transaction
device='cuda' if torch.cuda.is_available() else 'cpu'
import os

# Explicitly override cache paths (matches Dockerfile ENV)
os.environ["HF_HOME"] = "/app/hf_cache"
os.environ["TRANSFORMERS_CACHE"] = "/app/hf_cache"

encoder = SentenceTransformer(
    "sentence-transformers/all-MiniLM-L6-v2",
    cache_folder="/app/hf_cache"
).to(device)


global chat_messages
chat_messages=[]
outputs=[]
# Set your OpenAI API key here or use environment variable
payment_key=os.getenv('Payment_Key')

def complete_payment():
    if st.session_state.token :
        chPay=st.session_state.chPay
        try:
            result = chPay.charge(
                charge=0.5,
                event_name="Sample cloudhands charge",
            )
            st.success(f"You payment is succeeded")
            st.session_state.transaction_id=result.transaction_id
            st.session_state.db_transaction.add({
            'id':str(uuid.uuid4()),
            'app':'app_title',
            'transaction-id':result.transaction_id,
            'price':0.5

            })
        except Exception as e:
            st.error(f"Charge failed: {e}")
    else:
        st.error('Please generate your Tokens.')




@st.dialog("Payment link")
def pay():
    chPay = st.session_state.chPay

    # Step 1: Show auth link only once
    auth_url = chPay.get_authorization_url()
    st.link_button("Authenticate", url=auth_url)

    # Step 2: User pastes the code
    code = st.text_input("Place your code")

    if st.button("Exchange Code"):
        try:
            token = chPay.exchange_code_for_token(code)
            st.session_state.token = token
            st.success("Code exchanged successfully! Token stored.")
        except Exception as e:
            st.error(f"Failed: {e}")

def embed_document(file_text):
    chunks=text_splitter.split_text(file_text)
    #embedded=[]
    embeddings=st.session_state.encoder.encode(chunks, convert_to_tensor=True, show_progress_bar=True)
    embeddings = embeddings.cpu().numpy()

    #embeddings=torch.concatenate(embedded).cpu().numpy()
    #embeddings=embeddings.cpu().numpy()
    #print(embedded)
    return embeddings,chunks


def embed_sentence(text):
    embeddings = st.session_state.encoder.encode([text], convert_to_tensor=True, show_progress_bar=True)  
    return embeddings.cpu().tolist() 



def stream_response():
    for char in extract_output(st.session_state.response).split(" "):
        yield char+" "
        time.sleep(0.1)  # Simulate a delay

def stream_thoughts():
    for char in extract_thinking(st.session_state.response).split(" "):
        yield char+" "
        time.sleep(0.1)  # Simulate a delay

def get_text(uploaded_file):
    # Save uploaded file to a temporary file
    with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
        tmp_file.write(uploaded_file.read())
        tmp_path = tmp_file.name
    loader = PyPDFLoader(tmp_path)
    pages = loader.load()
    text = "\n".join([page.page_content for page in pages])
    return text


def respond_chat(text):

    url="https://8000-01k3gce7dwxsk16d7dd40n75xb.cloudspaces.litng.ai/predict"
    payload = { "user_prompt":text}
    headers = {"Content-Type": "application/json"}
    response = requests.post(url, data=payload)
        
    if response.status_code == 200:
        complete_payment()
        if st.session_state.transaction_id:
            return response.json()['output'][0]

def extract_thinking(text: str) -> str:
    """
    Extracts content inside <thinking>...</thinking> tags.
    Returns the first match or an empty string if not found.
    """
    match = re.search(r"<thinking>(.*?)</thinking>", text, re.DOTALL | re.IGNORECASE)
    return match.group(1).strip() if match else ""

def extract_output(text: str) -> str:
    """
    Extracts content inside <output>...</output> tags.
    Returns the first match or an empty string if not found.
    """
    match = re.search(r"<output>(.*?)</output>", text, re.DOTALL | re.IGNORECASE)
    return match.group(1).strip() if match else ""



# Dropdown for model selection
if 'doc_flag' not in st.session_state:
    st.session_state.doc_flag = False
if 'flag' not in st.session_state:
    st.session_state.flag = False
if 'encoder' not in st.session_state:
    st.session_state.encoder = encoder
if 'file_text' not in st.session_state:
    st.session_state.file_text = ""
if "chPay" not in st.session_state:
    st.session_state.chPay = CloudHandsPayment(
        author_key=payment_key
    )

if "token" not in st.session_state:
    st.session_state.token = None

if 'db_transaction' not in st.session_state:
    st.session_state.db_transaction = db_transaction
if 'embeddings' not in st.session_state:
    st.session_state.embeddings = None
if 'chunks' not in st.session_state:
    st.session_state.chunks = None
if 'response' not in st.session_state:
    st.session_state.response=''
# Sidebar document upload
st.sidebar.title("Uploading your document πŸ“„")
uploaded_file = st.sidebar.file_uploader(
    "Upload your document πŸ“„",
    type=["pdf"],
    label_visibility="collapsed"
)
upload_button=st.sidebar.button("Upload Document")
uploaded_file = st.sidebar.file_uploader(
    "Upload your PDF",
    type=["pdf"],
    key="pdf_uploader",
)

def extract_pdf_text_from_bytes(file_bytes: bytes) -> str:
    reader = PdfReader(io.BytesIO(file_bytes))
    pages_text = []
    for p in reader.pages:
        txt = p.extract_text() or ""
        pages_text.append(txt)
    return "\n".join(pages_text)

if uploaded_file is not None:
    with st.spinner("Reading & embedding your PDF..."):
        # Important: read bytes once on this rerun
        file_bytes = uploaded_file.read()
        # (Optional) if you ever re-use uploaded_file later, do: uploaded_file.seek(0)

        # Extract text purely in-memory (no /tmp files, no PyPDFLoader)
        file_text = extract_pdf_text_from_bytes(file_bytes)

        # Persist to session state
        st.session_state.file_text = file_text

        # Build embeddings (uses your existing text_splitter + encoder)
        chunks = text_splitter.split_text(file_text)
        embeddings = st.session_state.encoder.encode(
            chunks, convert_to_tensor=True, show_progress_bar=True
        ).cpu().numpy()

        st.session_state.embeddings = embeddings
        st.session_state.chunks = chunks
        st.session_state.doc_flag = True

    st.success(f"Loaded: {uploaded_file.name} β€” {len(st.session_state.chunks)} chunks")

st.sidebar.write("Before making the your faviorate charecter sound, authenicate your code")
Authenication=st.sidebar.button('Authenicate')
if Authenication:
    pay()

#subject=st.pills('Select your subject',list(roles.keys()),selection_mode='single')
st.title("Plaito")
st.write("Chat with our reasoning model and ask your questions. The model show you it's chain of thoughts and final answer.")
text=st.text_area("Ask your question:", height=100)
document_button=st.pills("Ask based on Documents", ['search'], selection_mode="single")
generate_button=st.button("Generate Response")
if generate_button:
    with st.spinner("Generating code..."):
        try:
            if document_button:
                graph=load_graph(st.session_state.embeddings,st.session_state.chunks)
                graph=graph.compile()
                initial_state = {
                    "embedded_query":embed_sentence(text),
                    "knowledge": [],
                    "summary": "",
                    "final_response": None,}
                final_state = graph.invoke(initial_state)
                updated_text = f"""
                Then respond to the client. Also follow the retrived information in the ##Summary section.
                ## Instructions:
                {text}
                ## Summary:
            {final_state['summary']}
                """
                if st.session_state.db_transaction:
                    response=respond_chat(updated_text)
                    st.session_state.response=response

            else: 
                if st.session_state.db_transaction:
                    response=respond_chat(text)
                    st.session_state.response=response
        except Exception as e:
                    st.error(f"Error during code generation: {e}")

    col1,col2=st.columns([2,1])
    with col2:
        st.write("### Thought Process")
        st.write_stream(stream_thoughts())
    with col1:
        st.write("### Response")
        st.write_stream(stream_response())