datawithsuman
commited on
Commit
•
1ab8a9c
1
Parent(s):
6c3ca71
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,3 @@
|
|
1 |
-
# !pip install -U pymupdf
|
2 |
-
# !pip install llama-index-embeddings-openai
|
3 |
-
# !pip install llama-index-llms-openai
|
4 |
-
# !pip install chromadb
|
5 |
-
# !pip install llama-index-vector-stores-chroma
|
6 |
-
# !pip install pydantic==1.10.11
|
7 |
-
# !pip install llama-index-retrievers-bm25
|
8 |
-
# !pip install sentence-transformers
|
9 |
-
# !pip install llmlingua
|
10 |
-
# !pip install accelerate
|
11 |
-
# !pip install rouge
|
12 |
-
# !pip install semantic-text-similarity
|
13 |
-
# !pip install evaluate
|
14 |
-
# !pip install streamlit
|
15 |
-
|
16 |
import os
|
17 |
import streamlit as st
|
18 |
import streamlit.components.v1 as components
|
@@ -30,7 +15,7 @@ from llama_index.core.retrievers import BaseRetriever
|
|
30 |
from llama_index.core.node_parser import SentenceSplitter
|
31 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
32 |
from llmlingua import PromptCompressor
|
33 |
-
from
|
34 |
from semantic_text_similarity.models import WebBertSimilarity
|
35 |
import nest_asyncio
|
36 |
|
@@ -98,7 +83,7 @@ if uploaded_files:
|
|
98 |
model=model,
|
99 |
messages=[
|
100 |
{"role":"system",
|
101 |
-
"content":"You are a helpful assistant who answers from the following context. If the answer can't be found in context,
|
102 |
},
|
103 |
{"role": "user",
|
104 |
"content": prompt,
|
@@ -168,8 +153,8 @@ if uploaded_files:
|
|
168 |
st.markdown(compressed_res[3])
|
169 |
|
170 |
# Save token summary and evaluation details to session state
|
171 |
-
|
172 |
-
scores =
|
173 |
webert_model = WebBertSimilarity(device='cpu')
|
174 |
similarity_score = webert_model.predict([(compressed_res[3], orig_res[3])])[0] / 5 * 100
|
175 |
|
@@ -184,8 +169,8 @@ if uploaded_files:
|
|
184 |
|
185 |
st.session_state.messages.append({"role": "assistant", "content": "Comparing Original and Optimized Prompt Response..."})
|
186 |
st.success("Comparing Original and Optimized Prompt Response...")
|
187 |
-
st.session_state.messages.append({"role": "assistant", "content": f"Rouge Score : {scores[
|
188 |
-
st.write(f"Rouge Score : {scores[
|
189 |
st.session_state.messages.append({"role": "assistant", "content": f"Semantic Text Similarity Score : {similarity_score}"})
|
190 |
st.write(f"Semantic Text Similarity Score : {similarity_score}")
|
191 |
|
@@ -198,15 +183,3 @@ if uploaded_files:
|
|
198 |
st.session_state.messages.append({"role": "assistant", "content": f"The optimized prompt has ${saving:.4f} saved in GPT-4."})
|
199 |
st.success(f"The optimized prompt has ${saving:.4f} saved in GPT-4.")
|
200 |
|
201 |
-
|
202 |
-
### Future scope -
|
203 |
-
|
204 |
-
# 1. Make this runnig in JPMC system.
|
205 |
-
# 2. Scale it read multiple files at once.
|
206 |
-
# 3. Cache the llm lingua roberta model to save time in downloading model every time.
|
207 |
-
# 4. Play around with the llm lingua hyperparameters and observe changes in output and dollar value.
|
208 |
-
|
209 |
-
### Refereces -
|
210 |
-
|
211 |
-
# 1. https://docs.llamaindex.ai/en/stable/understanding/
|
212 |
-
# 2. https://github.com/microsoft/LLMLingua/blob/main/examples/LLMLingua2.ipynb
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
import streamlit.components.v1 as components
|
|
|
15 |
from llama_index.core.node_parser import SentenceSplitter
|
16 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
17 |
from llmlingua import PromptCompressor
|
18 |
+
from rouge_score import rouge_scorer
|
19 |
from semantic_text_similarity.models import WebBertSimilarity
|
20 |
import nest_asyncio
|
21 |
|
|
|
83 |
model=model,
|
84 |
messages=[
|
85 |
{"role":"system",
|
86 |
+
"content":"You are a helpful assistant who answers from the following context. If the answer can't be found in context, politely refuse"
|
87 |
},
|
88 |
{"role": "user",
|
89 |
"content": prompt,
|
|
|
153 |
st.markdown(compressed_res[3])
|
154 |
|
155 |
# Save token summary and evaluation details to session state
|
156 |
+
scorer = rouge_scorer.RougeScorer(['rougeL'], use_stemmer=True)
|
157 |
+
scores = scorer.score(compressed_res[3],orig_res[3])
|
158 |
webert_model = WebBertSimilarity(device='cpu')
|
159 |
similarity_score = webert_model.predict([(compressed_res[3], orig_res[3])])[0] / 5 * 100
|
160 |
|
|
|
169 |
|
170 |
st.session_state.messages.append({"role": "assistant", "content": "Comparing Original and Optimized Prompt Response..."})
|
171 |
st.success("Comparing Original and Optimized Prompt Response...")
|
172 |
+
st.session_state.messages.append({"role": "assistant", "content": f"Rouge Score : {scores['rougeL'].fmeasure * 100}"})
|
173 |
+
st.write(f"Rouge Score : {scores['rougeL'].fmeasure * 100}")
|
174 |
st.session_state.messages.append({"role": "assistant", "content": f"Semantic Text Similarity Score : {similarity_score}"})
|
175 |
st.write(f"Semantic Text Similarity Score : {similarity_score}")
|
176 |
|
|
|
183 |
st.session_state.messages.append({"role": "assistant", "content": f"The optimized prompt has ${saving:.4f} saved in GPT-4."})
|
184 |
st.success(f"The optimized prompt has ${saving:.4f} saved in GPT-4.")
|
185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|