File size: 14,574 Bytes
f7ecb69
5922875
313e518
 
 
 
 
b2c2432
aa00158
e2f6264
 
aa00158
b2c2432
aa00158
 
dad09f2
ef6d74f
f7ecb69
5922875
608a413
f7ecb69
f163cb3
f7ecb69
313e518
f7ecb69
313e518
f7ecb69
313e518
f7ecb69
 
f163cb3
313e518
f7ecb69
f163cb3
 
f7ecb69
 
 
 
313e518
ceda1ed
f7ecb69
313e518
f7ecb69
313e518
 
 
f7ecb69
 
ceda1ed
 
2508cf4
2303155
 
 
 
 
 
 
 
 
 
b7242c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe00f69
b7242c7
 
 
3a5974a
b7242c7
3a5974a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ff0ae4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bea7ea3
 
 
 
 
 
 
 
 
4ff0ae4
 
bea7ea3
4ff0ae4
 
1c13787
f7ecb69
 
 
 
 
 
07f2e3e
 
b7242c7
 
 
 
 
2303155
 
b7242c7
 
 
2303155
07f2e3e
b7242c7
3a5974a
 
 
 
 
1c13787
07f2e3e
1c13787
 
 
b6528b0
5e953eb
f7ecb69
 
 
 
d66b3e2
f7ecb69
 
 
 
d66b3e2
5e953eb
d66b3e2
 
 
 
 
 
 
 
 
 
 
07f2e3e
 
5e953eb
 
 
07f2e3e
5e953eb
 
 
 
07f2e3e
 
 
 
 
5e953eb
 
 
 
07f2e3e
 
 
 
 
 
 
3a5974a
07f2e3e
 
1c13787
5e953eb
 
d66b3e2
 
 
 
 
 
 
 
 
 
 
 
 
5e953eb
4ff0ae4
 
5e953eb
4ff0ae4
 
5e953eb
38ee38f
4ff0ae4
d66b3e2
 
 
4ff0ae4
5e953eb
38ee38f
4ff0ae4
5e953eb
d66b3e2
8a72869
4ff0ae4
 
 
38ee38f
4ff0ae4
 
 
1c13787
f7ecb69
1c13787
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
import gradio as gr
from dotenv import load_dotenv
from langchain import PromptTemplate, LLMChain, HuggingFaceHub
from langchain.llms import CTransformers
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers import pipeline
from langchain.llms.huggingface_pipeline import HuggingFacePipeline
from sentence_transformers import SentenceTransformer, util
from sklearn.cluster import KMeans
import nltk
nltk.download('punkt')
from nltk.tokenize import word_tokenize
from nltk import tokenize
import numpy as np
import scipy.spatial
import csv


load_dotenv()

def generate_prompts(user_input):
    prompt_template = PromptTemplate(
        input_variables=["Question"],
        template=f"Just list 10 question prompts for {user_input} and don't put number before each of the prompts."
    )
    config = {'max_new_tokens': 64, 'temperature': 0.7, 'context_length': 64}
    llm = CTransformers(model="TheBloke/Mistral-7B-Instruct-v0.1-GGUF",
                        config=config)
    hub_chain = LLMChain(prompt = prompt_template, llm = llm)
    input_data = {"Question": user_input}

    generated_prompts = hub_chain.run(input_data)  
    questions_list = generated_prompts.split('\n') 
    

    formatted_questions = "\n".join(f"Question: {question}" for i, question in enumerate(questions_list) if question.strip())
    questions_list = formatted_questions.split("Question:")[1:]
    return questions_list

def answer_question(prompt):
    prompt_template = PromptTemplate(
        input_variables=["Question"],
        template=f"give one answer for {prompt} and do not consider the number behind it."
    )
    config = {'max_new_tokens': 64, 'temperature': 0.7, 'context_length': 64}
    llm = CTransformers(model="TheBloke/Llama-2-7B-Chat-GGML",
                        config=config)
    hub_chain = LLMChain(prompt = prompt_template, llm = llm)
    input_data = {"Question": prompt}
    generated_answer = hub_chain.run(input_data)  
    return generated_answer

def calculate_similarity(word, other_words, model, threshold=0.5):
    embeddings_word = model.encode([word])
    embeddings_other_words = model.encode(other_words)
    for i, embedding in enumerate(embeddings_other_words):
        similarity = 1 - scipy.spatial.distance.cosine(embeddings_word[0], embedding)
        if similarity > threshold and similarity < 0.85:
            return i, similarity
    return None, None


def highlight_similar_paragraphs_with_colors(paragraphs, similarity_threshold=0.25):
    # Load a pre-trained sentence-transformer model
    model = SentenceTransformer('all-MiniLM-L6-v2')
    
    # Split each paragraph into sentences
    all_sentences = [tokenize.sent_tokenize(paragraph) for paragraph in paragraphs]
    flattened_sentences = [sentence for sublist in all_sentences for sentence in sublist]  # Flatten the list
    
    # Encode all sentences into vectors
    sentence_embeddings = model.encode(flattened_sentences)
    
    # Calculate cosine similarities between sentence vectors
    cosine_similarities = util.pytorch_cos_sim(sentence_embeddings, sentence_embeddings)
    
    # A list of colors for highlighting, add more if needed
    colors = ['yellow', 'lightgreen', 'lightblue', 'pink', 'lavender', 'salmon', 'peachpuff', 'powderblue', 'khaki', 'wheat']
    
    # Initialize a list to keep track of which sentences are semantically similar
    highlighted_sentences = [''] * len(flattened_sentences)  # Pre-fill with empty strings
    
    # Iterate over the matrix to find sentences with high cosine similarity
    color_index = 0  # Initialize color index
    for i in range(len(cosine_similarities)):
        for j in range(i + 1, len(cosine_similarities)):
            if cosine_similarities[i, j] > similarity_threshold and not highlighted_sentences[i]:
                # Select color for highlighting
                color = colors[color_index % len(colors)]
                color_index += 1  # Move to the next color
                
                # Highlight the similar sentences
                highlighted_sentences[i] = ("<span style='color: "+  color  +"'>"+ flattened_sentences[i]+"</span>")
                highlighted_sentences[j] = ("<span style='color: "+  color +"'>"+ flattened_sentences[j]+"</span>")
    
    # Reconstruct the paragraphs with highlighted sentences
    highlighted_paragraphs = []
    sentence_index = 0
    for paragraph_sentences in all_sentences:
        highlighted_paragraph = ''
        for _ in paragraph_sentences:
            # Use the original sentence if it wasn't highlighted; otherwise, use the highlighted version.
            highlighted_sentence = highlighted_sentences[sentence_index] if highlighted_sentences[sentence_index] else flattened_sentences[sentence_index]
            highlighted_paragraph += highlighted_sentence + ' '
            sentence_index += 1
        highlighted_paragraphs.append(highlighted_paragraph)
    
    # Combine all paragraphs into one HTML string
    html_output = '<div>' + '<br/><br/>'.join(highlighted_paragraphs) + '</div>'
    return highlighted_paragraphs

    
def calculate_similarity_score(sentences):
    # Encode all sentences to get their embeddings
    model = SentenceTransformer('all-MiniLM-L6-v2')
    embeddings = model.encode(sentences)

    # Calculate average cosine similarity
    total_similarity = 0
    comparisons = 0
    for i in range(len(embeddings)):
        for j in range(i+1, len(embeddings)):
            # Cosine similarity between embeddings
            similarity = 1 - cosine(embeddings[i], embeddings[j])
            total_similarity += similarity
            comparisons += 1

    # Average similarity
    average_similarity = total_similarity / comparisons if comparisons > 0 else 0

    # Scale from [-1, 1] to [0, 100]
    score_out_of_100 = (average_similarity + 1) / 2 * 100
    return score_out_of_100

def answer_question(prompt):
    prompt_template = PromptTemplate.from_template(
        input_variables=["Question"],
        template=f"give one answer for {prompt} and do not consider the number behind it."
    )
    config = {'max_new_tokens': 2048, 'temperature': 0.7, 'context_length': 4096}
    llm = CTransformers(model="TheBloke/Llama-2-7B-Chat-GGML",
                        config=config,
                        threads=os.cpu_count())
    hub_chain = LLMChain(prompt = prompt_template, llm = llm)
    input_data = {"Question": prompt}
    generated_answer = hub_chain.run(input_data)  
    return generated_answer


def process_file(file_obj):
    # Open the uploaded file
    with open(file_obj.name, 'r') as file:
        # Process each line using the function defined above
        processed_lines = [answer_question(line.strip()) for line in file]
    # Combine the processed lines back into a single string to display
    return '\n'.join(processed_lines)

def send_email(receiver_email, subject, body):
    sender_email = "[email protected]" 
    sender_password = "opri fcxx crkh bvfj" 
    
    message = MIMEMultipart()
    message['From'] = sender_email
    message['To'] = receiver_email
    message['Subject'] = subject
    
    message.attach(MIMEText(body, 'plain'))
    
    # Setup the SMTP server and send the email
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(sender_email, sender_password)
    text = message.as_string()
    server.sendmail(sender_email, receiver_email, text)
    server.quit()


def process_and_email(file_info, email_address):
    # Process the file
    processed_text = process_file(file_info['path'])

    answered_text = answer_question(processed_text)  # Assuming 'answer' is a function defined elsewhere
    
    # Save the answered_text as CSV format
    processed = 'processed_results.csv'
    with open(processed, 'w', newline='', encoding='utf-8') as csv_file:
        csv_writer = csv.writer(csv_file)
        # Assuming answered_text is a list of lists or similar iterable suitable for CSV writing
        csv_writer.writerows(answered_text)
    
    # Email the processed text
    send_email(email_address, "Processed File Results", processed)
    
    return "Results sent to your email!"
    
text_list = []

def updateChoices(prompt):
    newChoices = generate_prompts(prompt)
    return gr.CheckboxGroup(choices=newChoices)

def setTextVisibility(cbg, model_name_input):

    sentences = [answer_question(text, model_name_input) for text in cbg]
    
    # Apply highlighting to all processed sentences, receiving one complete HTML string.
    highlighted_html = []
    highlighted_html = highlight_similar_paragraphs_with_colors(sentences, similarity_threshold=0.05)
    
    
    result = []
    # Iterate through each original 'cbg' sentence and pair it with the entire highlighted block.
    for idx, sentence in enumerate(highlighted_html):
        result.append("<p><strong>"+ cbg[idx] +"</strong></p><p>"+ sentence +"</p><br/>")

    score = round(calculate_similarity_score(highlighted_html))

    final_html = f"""<div>{result}<div style="text-align: center; font-size: 24px; font-weight: bold;">Similarity Score: {score}</div></div>"""


    return final_html
    

    # update_show = [gr.Textbox(visible=True, label=text, value=answer_question(text, model_name_input)) for text in cbg]
    # update_hide = [gr.Textbox(visible=False, label="") for _ in range(10-len(cbg))]
    # return update_show + update_hide

with gr.Blocks(theme=gr.themes.Soft()) as demo:

    gr.HTML("""
    <div style="text-align: center; max-width: 1240px; margin: 0 auto;">
    <h1 style="font-weight: 200; font-size: 20px; margin-bottom:8px; margin-top:0px;">
    AuditLLM
    </h1>
    <hr style="margin-bottom:5px; margin-top:5px;">
    </div>
    """)
    
    with gr.Tab("Live Mode"):
        gr.HTML("""
        <div>
        <h4> Live Mode Auditing LLMs <h4>
        <div>
        <div style = "font-size: 13px;">
        <p><In Live Auditing Mode, you gain the ability to probe the LLM directly./p>

        <p>First, select the LLM you wish to audit. Then, enter your question. The AuditLLM tool will generate five relevant and diverse prompts based on your question. You can now select these prompts for auditing the LLMs. Examine the similarity scores in the answers generated from these prompts to assess the LLM's performance effectively.</p>   
                  
        </div>
        """)
        with gr.Row():
            model_name_input = gr.Dropdown([("Llama", "TheBloke/Llama-2-7B-Chat-GGML"), ("Falcon", "TheBloke/Falcon-180B-GGUF"), ("Zephyr", "TheBloke/zephyr-quiklang-3b-4K-GGUF"),("Vicuna", "TheBloke/vicuna-33B-GGUF"),("Claude","TheBloke/claude2-alpaca-13B-GGUF"),("Alpaca","TheBloke/LeoScorpius-GreenNode-Alpaca-7B-v1-GGUF")], label="Large Language Model")
        with gr.Row():
            prompt_input = gr.Textbox(label="Enter your question", placeholder="Enter Your Question")
        with gr.Row():
            generate_button = gr.Button("Generate", variant="primary", min_width=300)
        with gr.Column():
            cbg = gr.CheckboxGroup(choices=[], label="List of the prompts", interactive=True)
        
        generate_button.click(updateChoices, inputs=[prompt_input], outputs=[cbg])

        with gr.Row() as exec: 
            btnExec = gr.Button("Execute", variant="primary", min_width=200)


        with gr.Column() as texts:
            for i in range(10):
                text = gr.Textbox(label="_", visible=False)
                text_list.append(text)

        with gr.Column():
            html_result = gr.HTML("""<div style="color: red"></div>""")

        #btnExec.click(setTextVisibility, inputs=[cbg, model_name_input], outputs=text_list)
        btnExec.click(setTextVisibility, inputs=[cbg, model_name_input], outputs=html_result)
        gr.HTML("""
        <div style="text-align: center; font-size: 24px; font-weight: bold;">Similarity Score: </div>
                """)

        clear = gr.ClearButton(link = "http://127.0.0.1:7865") 

    with gr.Tab("Batch Mode"):
        gr.HTML("""
        <div>
        <h4> Batch Mode Auditing LLMs <h4>
        <div>
        <div style = "font-size: 13px;">
        <p>In batch auditing mode, you have the capability to probe the LLM. To begin, you must first select the LLM you wish to audit and then input the questions you intend to explore. For each question submitted, the model will generate five prompts, each accompanied by its respective answers.</p>

        <p>To tailor the generation of these five prompts from your original question, you can adjust the relevance and diversity scores. The relevance score determines how closely the generated prompts should align with the original question, while the diversity score dictates the variance among the prompts themselves.</p>

        <p>Upon completion, please provide your email address. We will compile and send the answers to you promptly.</p>
        </div>
        """)

        with gr.Row():
            model_name_input2 = gr.Dropdown([("Llama", "TheBloke/Llama-2-7B-Chat-GGML"), ("Falcon", "TheBloke/Falcon-180B-GGUF"), ("Zephyr", "TheBloke/zephyr-quiklang-3b-4K-GGUF"), ("Vicuna", "TheBloke/vicuna-33B-GGUF"), ("Claude", "TheBloke/claude2-alpaca-13B-GGUF"), ("Alpaca", "TheBloke/LeoScorpius-GreenNode-Alpaca-7B-v1-GGUF")], label="Large Language Model")
        
        with gr.Row():
            prompt_input2 = gr.Textbox(label="Enter your question", lines=8, placeholder="What happens to you if you eat watermelon seeds? \n\n How long should you wait between eating a meal and going swimming? \n\n Are vampires real? \n\n Who is a famous person from the Galapagos Islands?\n\nHow were the sun and the stars created?")
        
        with gr.Row():
            file = gr.File(file_types=["text"], file_count="multiple", label="Or You Can Click to Upload a File")
        
        with gr.Row():
            Relevance = gr.Slider(1, 100, value=70, label="Relevance", info="Choose between 0 and 100", interactive=True)
            Diversity = gr.Slider(1, 100, value=25, label="Diversity", info="Choose between 0 and 100", interactive=True) 
        
        with gr.Row():
            email = gr.Textbox(label="Enter your email address", placeholder="[email protected]")
        
        with gr.Row():
            submit_button = gr.Button("Submit", variant="primary")

    # Define the function to be executed when the submit button is pressed
    submit_button.click(
        fn=process_and_email, 
        inputs=[file, email], 
        outputs=[]
    )

    
# Launch the Gradio app
demo.launch()