alibidaran commited on
Commit
2c771c2
Β·
verified Β·
1 Parent(s): 928e0c3

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +221 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,223 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
 
 
2
  import streamlit as st
3
+ import openai
4
+ import os
5
+ import time
6
+ #from roles import *
7
+ from langchain_community.document_loaders import PyPDFLoader
8
+ import tempfile
9
+ from RAG import load_graph,text_splitter
10
+ import torch
11
+ from sentence_transformers import SentenceTransformer
12
+ import torch
13
+ import uuid
14
+ import re
15
+ import requests
16
+ from cloudhands import CloudHandsPayment
17
+ from database_center import db_transaction
18
+ device='cuda' if torch.cuda.is_available() else 'cpu'
19
 
20
+ global chat_messages
21
+ chat_messages=[]
22
+ outputs=[]
23
+ # Set your OpenAI API key here or use environment variable
24
+ payment_key=os.environ['Payment_Key']
25
+
26
+ def complete_payment():
27
+ if st.session_state.token :
28
+ chPay=st.session_state.chPay
29
+ try:
30
+ result = chPay.charge(
31
+ charge=0.5,
32
+ event_name="Sample cloudhands charge",
33
+ )
34
+ st.success(f"You payment is succeeded")
35
+ st.session_state.transaction_id=result.transaction_id
36
+ st.session_state.db_transaction.add({
37
+ 'id':str(uuid.uuid4()),
38
+ 'app':'app_title',
39
+ 'transaction-id':result.transaction_id,
40
+ 'price':0.5
41
+
42
+ })
43
+ except Exception as e:
44
+ st.error(f"Charge failed: {e}")
45
+ else:
46
+ st.error('Please generate your Tokens.')
47
+
48
+
49
+
50
+
51
+ @st.dialog("Payment link")
52
+ def pay():
53
+ chPay = st.session_state.chPay
54
+
55
+ # Step 1: Show auth link only once
56
+ auth_url = chPay.get_authorization_url()
57
+ st.link_button("Authenticate", url=auth_url)
58
+
59
+ # Step 2: User pastes the code
60
+ code = st.text_input("Place your code")
61
+
62
+ if st.button("Exchange Code"):
63
+ try:
64
+ token = chPay.exchange_code_for_token(code)
65
+ st.session_state.token = token
66
+ st.success("Code exchanged successfully! Token stored.")
67
+ except Exception as e:
68
+ st.error(f"Failed: {e}")
69
+
70
+ def embed_document(file_text):
71
+ chunks=text_splitter.split_text(file_text)
72
+ #embedded=[]
73
+ embeddings=st.session_state.encoder.encode(chunks, convert_to_tensor=True, show_progress_bar=True)
74
+ embeddings = embeddings.cpu().numpy()
75
+
76
+ #embeddings=torch.concatenate(embedded).cpu().numpy()
77
+ #embeddings=embeddings.cpu().numpy()
78
+ #print(embedded)
79
+ return embeddings,chunks
80
+
81
+
82
+ def embed_sentence(text):
83
+ embeddings = st.session_state.encoder.encode([text], convert_to_tensor=True, show_progress_bar=True)
84
+ return embeddings.cpu().tolist()
85
+
86
+
87
+
88
+ def stream_response():
89
+ for char in extract_output(response).split(" "):
90
+ yield char+" "
91
+ time.sleep(0.1) # Simulate a delay
92
+
93
+ def stream_thoughts():
94
+ for char in extract_thinking(response).split(" "):
95
+ yield char+" "
96
+ time.sleep(0.1) # Simulate a delay
97
+
98
+ def get_text(uploaded_file):
99
+ # Save uploaded file to a temporary file
100
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
101
+ tmp_file.write(uploaded_file.read())
102
+ tmp_path = tmp_file.name
103
+ loader = PyPDFLoader(tmp_path)
104
+ pages = loader.load()
105
+ text = "\n".join([page.page_content for page in pages])
106
+ return text
107
+
108
+
109
+ def respond_chat(text):
110
+
111
+ url="https://8000-01k3gce7dwxsk16d7dd40n75xb.cloudspaces.litng.ai/predict"
112
+ payload = { "user_prompt":text}
113
+ headers = {"Content-Type": "application/json"}
114
+ response = requests.post(url, data=payload)
115
+
116
+ if response.status_code == 200:
117
+ complete_payment()
118
+ if st.session_state.transaction_id:
119
+ return response.json()['output'][0]
120
+
121
+ def extract_thinking(text: str) -> str:
122
+ """
123
+ Extracts content inside <thinking>...</thinking> tags.
124
+ Returns the first match or an empty string if not found.
125
+ """
126
+ match = re.search(r"<thinking>(.*?)</thinking>", text, re.DOTALL | re.IGNORECASE)
127
+ return match.group(1).strip() if match else ""
128
+
129
+ def extract_output(text: str) -> str:
130
+ """
131
+ Extracts content inside <output>...</output> tags.
132
+ Returns the first match or an empty string if not found.
133
+ """
134
+ match = re.search(r"<output>(.*?)</output>", text, re.DOTALL | re.IGNORECASE)
135
+ return match.group(1).strip() if match else ""
136
+
137
+
138
+
139
+ # Dropdown for model selection
140
+ if 'doc_flag' not in st.session_state:
141
+ st.session_state.doc_flag = False
142
+ if 'flag' not in st.session_state:
143
+ st.session_state.flag = False
144
+ if 'encoder' not in st.session_state:
145
+ st.session_state.encoder = SentenceTransformer("all-MiniLM-L6-v2").to(device)
146
+ if 'file_text' not in st.session_state:
147
+ st.session_state.file_text = ""
148
+ if "chPay" not in st.session_state:
149
+ st.session_state.chPay = CloudHandsPayment(
150
+ author_key=payment_key
151
+ )
152
+
153
+ if "token" not in st.session_state:
154
+ st.session_state.token = None
155
+
156
+ if 'db_transaction' not in st.session_state:
157
+ st.session_state.db_transaction = db_transaction
158
+ if 'embeddings' not in st.session_state:
159
+ st.session_state.embeddings = None
160
+ if 'chunks' not in st.session_state:
161
+ st.session_state.chunks = None
162
+
163
+ # Sidebar document upload
164
+ st.sidebar.title("Uploading your document πŸ“„")
165
+ uploaded_file = st.sidebar.file_uploader(
166
+ "Upload your document πŸ“„",
167
+ type=["pdf"],
168
+ label_visibility="collapsed"
169
+ )
170
+ upload_button=st.sidebar.button("Upload Document")
171
+ if upload_button:
172
+ if uploaded_file is None:
173
+ st.warning("Please upload a PDF file.")
174
+ st.session_state.doc_flag = False
175
+ else:
176
+ file_text = get_text(uploaded_file)
177
+ st.session_state.file_text = file_text
178
+ embeddings,chunks = embed_document(file_text)
179
+ st.session_state.embeddings = embeddings
180
+ st.session_state.chunks = chunks
181
+ st.session_state.doc_flag = True
182
+
183
+ st.sidebar.write("Before making the your faviorate charecter sound, authenicate your code")
184
+ Authenication=st.sidebar.button('Authenicate')
185
+ if Authenication:
186
+ pay()
187
+
188
+ st.title("Virtual Supervisor")
189
+ #subject=st.pills('Select your subject',list(roles.keys()),selection_mode='single')
190
+ st.title("Plaito")
191
+ st.write("Chat with our reasoning model and ask your questions. The model show you it's chain of thoughts and final answer.")
192
+ text=st.text_area("Ask your question:", height=100)
193
+ document_button=st.pills("Ask based on Documents", ['search'], selection_mode="single")
194
+ generate_button=st.button("Generate Response")
195
+ if generate_button:
196
+ if document_button:
197
+ graph=load_graph(st.session_state.embeddings,st.session_state.chunks)
198
+ graph=graph.compile()
199
+ initial_state = {
200
+ "embedded_query":embed_sentence(text),
201
+ "knowledge": [],
202
+ "summary": "",
203
+ "final_response": None,}
204
+ final_state = graph.invoke(initial_state)
205
+ updated_text = f"""
206
+ Then respond to the client. Also follow the retrived information in the ##Summary section.
207
+ ## Instructions:
208
+ {text}
209
+ ## Summary:
210
+ {final_state['summary']}
211
+ """
212
+ complete_payment()
213
+ response=respond_chat(updated_text)
214
+
215
+ else:
216
+ response=respond_chat(text)
217
+ col1,col2=st.columns([2,1])
218
+ with col2:
219
+ st.write("### Thought Process")
220
+ st.write_stream(stream_thoughts())
221
+ with col1:
222
+ st.write("### Response")
223
+ st.write_stream(stream_response())