Spaces:
Paused
Paused
File size: 13,589 Bytes
412b3ca 8f6f820 412b3ca 9734990 f6b5f5a 1fc191c 412b3ca 9734990 412b3ca 9734990 412b3ca 8f6f820 412b3ca 1fc191c 412b3ca 1fc191c 412b3ca 1fc191c 412b3ca |
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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
import streamlit as st
#from transformers import AutoModelForCausalLM, AutoTokenizer
from langchain_community.llms import CTransformers
from transformers.utils import is_flash_attn_2_available
from transformers import BitsAndBytesConfig
import pandas as pd
import os
import torch
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
import streamlit as st
import llama_cpp
from llama_cpp import Llama
import llama_cpp.llama_tokenizer
from langchain.llms.base import LLM
from typing import Optional, List, Mapping, Any
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings.sentence_transformer import (
SentenceTransformerEmbeddings,
)
# SET TO WIDE LAYOUT
st.set_page_config(layout="wide")
#_______________________________________________SET VARIABLES_____________________________________________________
EMBEDDING = "all-MiniLM-L6-v2"
COLLECTION_NAME = f'vb_summarizer_{EMBEDDING}_test'
CHROMA_DATA_PATH = 'feedback_360'
#_______________________________________________LOAD MODELS_____________________________________________________
# LOAD MODEL
@st.cache_resource
class LlamaLLM(LLM):
model_path: str
llm: Llama
@property
def _llm_type(self) -> str:
return "llama-cpp-python"
def __init__(self, model_path: str, **kwargs: Any):
model_path = model_path
llm = Llama(model_path=model_path)
super().__init__(model_path=model_path, llm=llm, **kwargs)
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
response = self.llm(prompt, stop=stop or [])
return response["choices"][0]["text"]
@property
def _identifying_params(self) -> Mapping[str, Any]:
return {"model_path": self.model_path}
@st.cache_resource
def load_model():
llm_model = llama_cpp.Llama.from_pretrained(
repo_id="TheBloke/toxicqa-Llama2-7B-GGUF",
filename="toxicqa-llama2-7b.Q5_K_M.gguf",
# repo_id="TheBloke/Llama-2-7b-Chat-GGUF",
# filename="llama-2-7b-chat.Q4_K_M.gguf",
#tokenizer=llama_cpp.llama_tokenizer.LlamaHFTokenizer.from_pretrained("Qwen/Qwen1.5-0.5B"),
embedding=True,
verbose=False,
n_ctx=1024,
n_threads = 2,
n_gpu_layers=0, # The number of layers to offload to GPU, if you have GPU acceleration available
chat_format="llama-2",
cache_dir='./model_cached'
)
#from ctransformers import AutoModelForCausalLM
# Set gpu_layers to the number of layers to offload to GPU. Set to 0 if no GPU acceleration is available on your system.
#from ctransformers import AutoModelForCausalLM
#import ctransformers
# Set gpu_layers to the number of layers to offload to GPU. Set to 0 if no GPU acceleration is available on your system.
#llm = AutoModelForCausalLM.from_pretrained("TheBloke/Llama-2-7b-Chat-GGUF", model_file="llama-2-7b-chat.q4_K_M.gguf", model_type="llama", gpu_layers=0)
#llm = CTransformers(model = "TheBloke/Llama-2-7b-Chat-GGUF", model_file="llama-2-7b-chat.q4_K_M.gguf", model_type = 'llama')
#print(llm("AI is going to"))
return llm_model
# LOAD VECTORSTORE
@st.cache_resource
def load_data(embedding) :
# CREATE EMBEDDING
embedding_function = SentenceTransformerEmbeddings(model_name=embedding)
db3 = Chroma(collection_name = COLLECTION_NAME, persist_directory="./chroma", embedding_function = embedding_function)
return db3
# Create a text element and let the reader know the data is loading.
model_load_state = st.text('Loading model...')
# Load 10,000 rows of data into the dataframe.
llm_model = load_model()
# Notify the reader that the data was successfully loaded.
model_load_state.text('Loading model...done!')
# Create a text element and let the reader know the data is loading.
data_load_state = st.text('Loading data...')
# Load 10,000 rows of data into the dataframe.
vectorstore = load_data(EMBEDDING)
# Notify the reader that the data was successfully loaded.
data_load_state.text('Loading data...done!')
# INFERENCE
# def prompt_formatter(reviews, type_of_doc):
# return f"""You are a summarization bot.
# You will receive {type_of_doc} and you will extract all relevant information from {type_of_doc} and return one paragraph in which you will summarize what was said.
# {type_of_doc} are listed below under inputs.
# Inputs: {reviews}
# Answer :
# """
# def prompt_formatter(reviews):
# return f"""You are a summarization bot.
# You will an input and summarize in one paragraph the meaning of the input.
# Do not quote from the input and do not repeat what was said in the input.
# Do not make things up.
# Input: {reviews}
# Answer :
# """
# def prompt_formatter(reviews):
# return f"""You are a summarization bot.
# You will receive reviews of Clockify from different users.
# You will summarize what these reviews said while keeping the information about each of the user.
# You will return the answer in the form : Review [number of review] : [summarization of review].
# Reviews are listed below.
# Reviews: {reviews}
# Answer :
# """
# def prompt_formatter(reviews):
# return f"""You are a summarization bot.
# You will receive reviews of Clockify from different users.
# You will create one paragraph with the summarization of what the reviews say about Clockify.
# Reviews are listed below.
# Do not make things up. Use only information from reviews.
# Reviews: {reviews}
# Answer :
# """
def prompt_formatter(reviews):
return f"""You are a summarization bot.
You will receive reviews of Clockify from different users.
You will summarize what are good and bad Clockify qualities according to all reviews.
Reviews are listed below.
Do not make things up. Use only information from reviews.
Reviews: {reviews}
Answer :
"""
def mirror_mirror(inputs, prompt_formatter):
prompt = prompt_formatter(inputs)
######### LLAMA2_Q4
# response = llm_model.create_chat_completion(
# messages=[
# {
# "role": "user",
# "content": prompt
# }
# ],
# response_format={
# "type": "text",
# },
# temperature = 0.4,
# min_p = 0.01,
# max_tokens = 256,
# #presence_penalty = 100,
# repeat_penalty = 2,
# )
# output_text = response['choices'][0]['message']['content']
# TOXIQA-LLAMA2
response = llm_model(
prompt, # Prompt
max_tokens=512, # Generate up to 512 tokens
stop=["</s>"], # Example stop token - not necessarily correct for this specific model! Please check before using.
echo=True # Whether to echo the prompt
)
print(response)
output_text = response['choices'][0]['text'].replace(prompt,'')
return prompt, output_text
def summarization(example : list[str], results_df : pd.DataFrame = pd.DataFrame()) -> pd.DataFrame :
# INFERENCE
results = []
scores = []
for cnt in range(0,3) :
print(cnt)
prompt, result = mirror_mirror(example, prompt_formatter)
example_embedded = np.array(llm_model.create_embedding(result)["data"][0]["embedding"]).reshape(1, -1)
result_embedded = np.array(llm_model.create_embedding(example)["data"][0]["embedding"]).reshape(1, -1)
score = cosine_similarity(example_embedded,result_embedded)
scores.append(str(score[0][0]))
if score>0.1 :
fin_result = result
max_score = score
break
#print(result)
results.append(f'Summary{cnt} : '+result)
max_score = max(scores)
# save final result and its attributes
try :
fin_result
except :
fin_result = results[np.argmax(scores)]
row = pd.DataFrame({'model' : 'llama_neka_cpp', 'prompt' : prompt, 'reviews' : example, 'summarization' : fin_result, 'scores' :[max_score] })
results_df = pd.concat([results_df,row], ignore_index = True)
return results_df
def create_filter(group:str=None, platform:str=None, ReviewerPosition:str=None, Industry:str=None, CompanySize:str=None,
UsagePeriod:str=None, LinkedinVerified:str=None, Date:str=None, Rating:str=None) :
keys = ['group', 'Platform', 'ReviewerPosition', 'Industry', 'CompanySize',
'UsagePeriod', 'LinkedinVerified', 'Date', 'Rating']
input_keys = [group,platform, ReviewerPosition, Industry, CompanySize, UsagePeriod, LinkedinVerified, Date, Rating]
# create filter dict
filter_dict = {}
for key, in_key in zip(keys, input_keys) :
if not in_key == None and not in_key == ' ':
filter_dict[key] = {'$eq' : in_key}
print(filter_dict)
return filter_dict
#_______________________________________________UI_____________________________________________________
st.title("Mirror, mirror, on the cloud, what do Clockify users say aloud?")
st.subheader("--Clockify review summarizer--")
col1, col2, col3 = st.columns(3, gap = 'small')
with col1:
platform = st.selectbox(label = 'Platform',
options = [' ', 'Capterra', 'Chrome Extension', 'GetApp', 'AppStore', 'GooglePlay',
'Firefox Extension', 'JIRA Plugin', 'Trustpilot', 'G2',
'TrustRadius']
)
with col2:
company_size = st.selectbox(label = 'Company Size',
options = [' ', '1-10 employees', 'Self-employed', 'self-employed',
'Small-Business(50 or fewer emp.)', '51-200 employees',
'Mid-Market(51-1000 emp.)', '11-50 employees',
'501-1,000 employees', '10,001+ employees', '201-500 employees',
'1,001-5,000 employees', '5,001-10,000 employees',
'Enterprise(> 1000 emp.)', 'Unknown', '1001-5000 employees']
)
with col3:
linkedin_verified = st.selectbox(label = 'Linkedin Verified',
options = [' ', 'True', 'False'],
placeholder = 'Choose an option'
)
num_to_return = int(st.number_input(label = 'Number of documents to return', min_value = 2, max_value = 50, step = 1))
# group = st.selectbox(label = 'Review Platform Group',
# options = ['Software Review Platforms', 'Browser Extension Stores', 'Mobile App Stores', 'Plugin Marketplace']
# )
default_value = "Clockify"
query = st.text_area("Query", default_value, height = 50)
#type_of_doc = st.text_area("Type of text", 'text', height = 25)
# result = ''
# score = ''
# reviews = ''
if 'result' not in st.session_state:
st.session_state['result'] = ''
if 'score' not in st.session_state:
st.session_state['score'] = ''
if 'reviews' not in st.session_state:
st.session_state['reviews'] = ''
col11, col21 = st.columns(2, gap = 'small')
with col11:
button_query = st.button('Conquer and query!')
with col21:
button_summarize = st.button('Summon the summarizer!')
if button_query :
print('Querying')
# create filter from drop-downs
filter_dict = create_filter(#group = group,
platform = platform,
CompanySize = company_size,
LinkedinVerified = linkedin_verified
)
# FILTER BY META
if filter_dict == {} :
retriever = vectorstore.as_retriever(search_kwargs = {"k": num_to_return})
elif len(filter_dict.keys()) == 1 :
retriever = vectorstore.as_retriever(search_kwargs = {"k": num_to_return,
"filter": filter_dict})
else :
retriever = vectorstore.as_retriever(search_kwargs = {"k": num_to_return,
"filter":{'$and': [{key : value} for key,value in filter_dict.items()]}
}
)
reviews = retriever.get_relevant_documents(query = query)
# only get page content
st.session_state['reviews'] = [f'Review {cnt} : {review.page_content}\n\n' for cnt,review in enumerate(reviews)]
#print(st.session_state['reviews'])
result = 'You may summarize now!'
if button_summarize :
print('Summarization in progress')
st.session_state['result'] = 'Summarization in progress'
results_df = summarization("\n".join(st.session_state['reviews']))
# only one input
st.session_state['result'] = results_df.summarization[0]
st.session_state['score'] = results_df.scores[0]
col12, col22 = st.columns(2, gap = 'small')
with col12:
chosen_reviews = st.text_area("Reviews to be summarized", "\n".join(st.session_state['reviews']), height = 275)
with col22:
summarized_text = st.text_area("Summarized text", st.session_state['result'], height = 275)
score = st.text_area("Cosine similarity score", st.session_state['score'], height = 25)
|