Amirizaniani commited on
Commit
313e518
1 Parent(s): 3011d90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -177
app.py CHANGED
@@ -1,126 +1,43 @@
1
  import gradio as gr
2
  from dotenv import load_dotenv
3
- from langchain.chains import LLMChain
4
- from langchain_community.llms import CTransformers
5
- from langchain_core.prompts import PromptTemplate
6
- from sentence_transformers import SentenceTransformer
7
- from sklearn.cluster import KMeans
8
- from nltk.tokenize import word_tokenize
9
- import numpy as np
10
- import scipy.spatial
11
- from scipy.spatial.distance import cosine
12
-
13
 
14
  load_dotenv()
15
  def generate_prompts(user_input):
16
  prompt_template = PromptTemplate(
17
  input_variables=["Question"],
18
- template= f"Your task is to formulate 5 unique queries for each given question. These queries must adhere to the criteria of relevance and diversity.write the questions in seperate lines.{user_input} "
19
  )
20
- config = {'max_new_tokens': 2048, 'temperature': 0.7, 'context_length': 4096}
21
  llm = CTransformers(model="TheBloke/Mistral-7B-Instruct-v0.1-GGUF",
22
- config=config,
23
- threads=os.cpu_count())
24
  hub_chain = LLMChain(prompt = prompt_template, llm = llm)
25
-
26
  input_data = {"Question": user_input}
27
 
28
- # Here you would integrate your prompt template with your model
29
- # For demonstration, this is just a placeholder
30
- generated_prompts = hub_chain.run(input_data) # Modify this part based on how you run the model
31
  questions_list = generated_prompts.split('\n')
32
 
33
 
34
  formatted_questions = "\n".join(f"Question: {question}" for i, question in enumerate(questions_list) if question.strip())
35
  questions_list = formatted_questions.split("Question:")[1:]
36
-
37
-
38
  return questions_list
39
 
40
- def answer_question(prompt, model_name):
41
  prompt_template = PromptTemplate(
42
  input_variables=["Question"],
43
- template=f"Give a short answer to this question '{prompt}' and do not consider the number behind it."
44
  )
45
- config = {'max_new_tokens': 512, 'temperature': 0.7, 'context_length': 512}
46
- llm = CTransformers(model=model_name, #"TheBloke/Llama-2-7B-Chat-GGML",
47
- config=config,
48
- threads=os.cpu_count())
49
  hub_chain = LLMChain(prompt = prompt_template, llm = llm)
50
-
51
  input_data = {"Question": prompt}
52
-
53
-
54
  generated_answer = hub_chain.run(input_data)
55
-
56
  return generated_answer
57
- def calculate_similarity(word, other_words, model, threshold=0.5):
58
- embeddings_word = model.encode([word])
59
- embeddings_other_words = model.encode(other_words)
60
- for i, embedding in enumerate(embeddings_other_words):
61
- similarity = 1 - scipy.spatial.distance.cosine(embeddings_word[0], embedding)
62
- if similarity > threshold and similarity < 0.85:
63
- return i, similarity
64
- return None, None
65
-
66
- def highlight_words_within_cluster(sentences, model, exclude_words):
67
- # Create a dictionary to map words to color codes
68
- word_to_color = {}
69
- color_codes = [
70
- "\033[41m", # Background Red
71
- "\033[42m", # Background Green
72
- "\033[43m", # Background Yellow
73
- "\033[44m", # Background Blue
74
- "\033[45m", # Background Purple
75
- "\033[46m", # Background Cyan
76
- "\033[100m", # Background Dark Gray
77
- "\033[101m", # Background Light Red
78
- "\033[102m", # Background Light Green
79
- "\033[103m", # Background Light Yellow
80
- "\033[104m", # Background Light Blue
81
- "\033[105m", # Background Light Purple
82
- "\033[106m", # Background Light Cyan
83
- "\033[47m" # Background Gray
84
- ]
85
- html_color_codes = ["red", "green", "blue", "purple", "cyan", "fuchsia", "lime", "maroon", "olive", "navy", "teal", "gray"]
86
- color_index = 0
87
-
88
- highlighted_sentences = []
89
- for sentence in sentences:
90
- words = word_tokenize(sentence)
91
- other_sentences = [s for s in sentences if s != sentence]
92
- all_other_words = [word for s in other_sentences for word in word_tokenize(s) if word.lower() not in exclude_words and word.isalnum()]
93
-
94
- highlighted_words = []
95
- for word in words:
96
- if word.lower() not in exclude_words and word.isalnum():
97
- match_index, similarity = calculate_similarity(word, all_other_words, model)
98
- if match_index is not None:
99
- # Assign color to the word if not already assigned
100
- if word not in word_to_color:
101
- word_to_color[word] = html_color_codes[color_index % len(html_color_codes)]
102
- color_index += 1
103
- # Highlight the word
104
- #highlighted_word = f"{word_to_color[word]}{word}\033[0m"
105
- highlighted_word = "<span style='color: "+ word_to_color[word] +"'>"+ word +"</span>"
106
- else:
107
- highlighted_word = word
108
- highlighted_words.append(highlighted_word)
109
- else:
110
- highlighted_words.append(word)
111
-
112
- highlighted_sentences.append(' '.join(highlighted_words))
113
- return highlighted_sentences
114
-
115
- # Rest of the code, including the cluster_sentences function, remains the same
116
- def cluster_sentences(sentences, model, num_clusters=3):
117
- embeddings = model.encode(sentences)
118
- kmeans = KMeans(n_clusters=num_clusters)
119
- kmeans.fit(embeddings)
120
- return kmeans.labels_
121
-
122
- model = SentenceTransformer('all-mpnet-base-v2')
123
- exclude_words = {"a", "the", "for", "from", "of", "in", "over", "as", "on", "is", "am", "have", "an", "has", "had", "and", "by", "it", "its", "those", "these", "above", "to"}
124
 
125
  text_list = []
126
 
@@ -128,44 +45,12 @@ def updateChoices(prompt):
128
  newChoices = generate_prompts(prompt)
129
  return gr.CheckboxGroup(choices=newChoices)
130
 
131
- def setTextVisibility(cbg, model_name_input):
132
- sentences = []
133
- result = []
134
- model = SentenceTransformer('all-mpnet-base-v2')
135
- exclude_words = {"a", "the", "for", "from", "of", "in", "over", "as", "on", "is", "am", "have", "an", "has", "had", "and", "by", "it", "its", "those", "these", "above", "to"}
136
- sentences_org = ["In a quaint little town nestled in the heart of the mountains, a small bakery famous for its artisanal breads and pastries had a line of customers stretching out the door, eagerly waiting to savor the freshly baked goods that were known far and wide for their delightful flavors.",
137
- "Within a picturesque mountain village, there stood a renowned bakery, celebrated for its handcrafted bread and sweet treats, attracting a long queue of patrons each morning, all keen to enjoy the baked delicacies that had gained widespread acclaim for their exceptional taste.",
138
- "A charming bakery, located in a small mountainous hamlet, renowned for producing exquisite handmade pastries and bread, was bustling with a crowd of eager customers lined up outside, each anticipating the chance to indulge in the famous baked items celebrated for their extraordinary deliciousness.",
139
- "In a cozy, mountain-encircled village, a beloved bakery was the center of attraction, known for its traditional baking methods and delightful pastries, drawing a consistent stream of people waiting outside, all desiring to experience the renowned flavors that made the bakery's products distinctively mouth-watering."]
140
- for text in cbg:
141
- sentences.append(answer_question(text, model_name_input))
142
-
143
- # Step 1: Cluster the sentences
144
- num_clusters = 1
145
- sentence_clusters = cluster_sentences(sentences, model, num_clusters)
146
 
147
- # Step 2: Highlight similar words within each cluster
148
- clustered_sentences = [[] for _ in range(num_clusters)]
149
-
150
- for sentence, cluster_id in zip(sentences, sentence_clusters):
151
- clustered_sentences[cluster_id].append(sentence)
152
-
153
- highlighted_clustered_sentences = []
154
-
155
- for cluster in clustered_sentences:
156
- highlighted_clustered_sentences.extend(highlight_words_within_cluster(cluster, model, exclude_words))
157
-
158
- for idx, sentence in enumerate(highlighted_clustered_sentences):
159
- result.append("<p><strong>"+ cbg[idx] +"</strong></p><p>"+ sentence +"</p><br/>")
160
-
161
- return result
162
-
163
-
164
- # update_show = [gr.Textbox(visible=True, label=text, value=answer_question(text, model_name_input)) for text in cbg]
165
- # update_hide = [gr.Textbox(visible=False, label="") for _ in range(10-len(cbg))]
166
- # return update_show + update_hide
167
-
168
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
169
 
170
  gr.HTML("""
171
  <div style="text-align: center; max-width: 1240px; margin: 0 auto;">
@@ -174,55 +59,28 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
174
  </h1>
175
  <hr style="margin-bottom:5px; margin-top:5px;">
176
 
177
-
178
-
179
  </div>
180
  """)
181
- with gr.Tab("Live Mode"):
182
- with gr.Row():
183
- 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")
184
- with gr.Row():
185
- prompt_input = gr.Textbox(label="Enter your question", placeholder="Enter Your Question")
186
- with gr.Row():
187
- generate_button = gr.Button("Generate", variant="primary", min_width=300)
188
- with gr.Column():
189
- cbg = gr.CheckboxGroup(choices=[], label="List of the prompts", interactive=True)
190
-
191
- generate_button.click(updateChoices, inputs=[prompt_input], outputs=[cbg])
192
-
193
- with gr.Row() as exec:
194
- btnExec = gr.Button("Execute", variant="primary", min_width=200)
195
 
 
 
 
 
 
 
 
 
196
 
197
- with gr.Column() as texts:
198
- for i in range(10):
199
- text = gr.Textbox(label="_", visible=False)
200
- text_list.append(text)
201
-
202
- with gr.Column():
203
- html_result = gr.HTML("""<div style="color: red"></div>""")
204
-
205
- #btnExec.click(setTextVisibility, inputs=[cbg, model_name_input], outputs=text_list)
206
- btnExec.click(setTextVisibility, inputs=[cbg, model_name_input], outputs=html_result)
207
- gr.HTML("""
208
- <div style="text-align: center; font-size: 24px; font-weight: bold;">Similarity Score: 76%</div>
209
- """)
210
 
211
- clear = gr.ClearButton(link = "http://127.0.0.1:7865")
212
 
213
- with gr.Tab("Batch Mode"):
214
- with gr.Row():
215
- 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")
216
- with gr.Row():
217
- prompt_input = gr.Textbox(label="Enter your question", placeholder="Enter Your Question")
218
- with gr.Row():
219
- prompt_input = gr.Textbox(label="RELAVENCY", placeholder="Relavancy")
220
- prompt_input = gr.Textbox(label="Diversity", placeholder="Diversity")
221
 
222
- with gr.Row():
223
- prompt_input = gr.Textbox(label="Enter your email address", placeholder="Enter Your Email Address")
224
- with gr.Row():
225
- generate_button = gr.Button("Submit", variant="primary")
226
-
227
  # Launch the Gradio app
228
  demo.launch(share=True)
 
1
  import gradio as gr
2
  from dotenv import load_dotenv
3
+ from langchain import PromptTemplate, LLMChain, HuggingFaceHub
4
+ from langchain.llms import CTransformers
5
+ from transformers import AutoModelForCausalLM, AutoTokenizer
6
+ from transformers import pipeline
7
+ from langchain.llms.huggingface_pipeline import HuggingFacePipeline
 
 
 
 
 
8
 
9
  load_dotenv()
10
  def generate_prompts(user_input):
11
  prompt_template = PromptTemplate(
12
  input_variables=["Question"],
13
+ template=f"Just list 10 question prompts for {user_input} and don't put number before each of the prompts."
14
  )
15
+ config = {'max_new_tokens': 64, 'temperature': 0.7, 'context_length': 64}
16
  llm = CTransformers(model="TheBloke/Mistral-7B-Instruct-v0.1-GGUF",
17
+ config=config)
 
18
  hub_chain = LLMChain(prompt = prompt_template, llm = llm)
 
19
  input_data = {"Question": user_input}
20
 
21
+ generated_prompts = hub_chain.run(input_data)
 
 
22
  questions_list = generated_prompts.split('\n')
23
 
24
 
25
  formatted_questions = "\n".join(f"Question: {question}" for i, question in enumerate(questions_list) if question.strip())
26
  questions_list = formatted_questions.split("Question:")[1:]
 
 
27
  return questions_list
28
 
29
+ def answer_question(prompt):
30
  prompt_template = PromptTemplate(
31
  input_variables=["Question"],
32
+ template=f"give one answer for {prompt} and do not consider the number behind it."
33
  )
34
+ config = {'max_new_tokens': 64, 'temperature': 0.7, 'context_length': 64}
35
+ llm = CTransformers(model="TheBloke/Llama-2-7B-Chat-GGML",
36
+ config=config)
 
37
  hub_chain = LLMChain(prompt = prompt_template, llm = llm)
 
38
  input_data = {"Question": prompt}
 
 
39
  generated_answer = hub_chain.run(input_data)
 
40
  return generated_answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  text_list = []
43
 
 
45
  newChoices = generate_prompts(prompt)
46
  return gr.CheckboxGroup(choices=newChoices)
47
 
48
+ def setTextVisibility(cbg):
49
+ update_show = [gr.Textbox(visible=True, label=text, value=answer_question(text)) for text in cbg]
50
+ update_hide = [gr.Textbox(visible=False, label="") for _ in range(10-len(cbg))]
51
+ return update_show + update_hide
 
 
 
 
 
 
 
 
 
 
 
52
 
53
+ with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  gr.HTML("""
56
  <div style="text-align: center; max-width: 1240px; margin: 0 auto;">
 
59
  </h1>
60
  <hr style="margin-bottom:5px; margin-top:5px;">
61
 
 
 
62
  </div>
63
  """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
+ with gr.Row():
66
+ prompt_input = gr.Textbox(label="Enter your question", placeholder="Enter Your Question")
67
+ with gr.Row():
68
+ generate_button = gr.Button("Generate")
69
+ with gr.Column():
70
+ cbg = gr.CheckboxGroup(choices=[], label="List of the prompts", interactive=True)
71
+
72
+ generate_button.click(updateChoices, inputs=[prompt_input], outputs=[cbg])
73
 
74
+ with gr.Row(variant="compact") as exec:
75
+ btnExec = gr.Button("Execute")
76
+ with gr.Column() as texts:
77
+ for i in range(10):
78
+ text = gr.Textbox(label="_", visible=False)
79
+ text_list.append(text)
 
 
 
 
 
 
 
80
 
81
+ btnExec.click(setTextVisibility, inputs=cbg, outputs=text_list)
82
 
83
+ Clear = gr.ClearButton([prompt_input, cbg, text], scale=1)
 
 
 
 
 
 
 
84
 
 
 
 
 
 
85
  # Launch the Gradio app
86
  demo.launch(share=True)