mahika123 commited on
Commit
d678852
·
verified ·
1 Parent(s): d2dcd01
Files changed (1) hide show
  1. app.py +87 -120
app.py CHANGED
@@ -1,8 +1,7 @@
1
-
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
4
 
5
- #STEP 1 FROM SEMANTIC SEARCH
6
  from sentence_transformers import SentenceTransformer
7
  import torch
8
 
@@ -10,144 +9,99 @@ theme = gr.themes.Soft(
10
  primary_hue="rose",
11
  secondary_hue="zinc",
12
  neutral_hue="pink"
13
- )
14
 
15
  custom_css = """
16
- :root { /* This applies to the light mode */
17
- --background-fill-primary: #FFB6C2 !important; /* Light pink */
18
  }
19
- .dark { /* This applies to the dark mode */
20
- --background-fill-primary: #FFB6C1 !important; /* Light pink */
21
  }
22
  """
23
 
24
- #STEP 2 FROM SEMANTIC SEARCH
25
- # Open the water_cycle.txt file in read mode with UTF-8 encoding
26
  with open("research.txt", "r", encoding="utf-8") as file:
27
- # Read the entire contents of the file and store it in a variable
28
- research_text = file.read()
29
 
30
- # Print the text below
31
- print(research_text)
32
-
33
- # STEP 3 FROM SEMANTIC SEARCH
34
  def preprocess_text(text):
35
- # Strip extra whitespace from the beginning and the end of the text
36
- cleaned_text = text.strip()
37
- # Split the cleaned_text by every newline character (\n)
38
- chunks = cleaned_text.split("\n")
39
- # Create an empty list to store cleaned chunks
40
- cleaned_chunks = []
41
- # Write your for-in loop below to clean each chunk and add it to the cleaned_chunks list
42
- for chunk in chunks:
43
- stripped_chunk = chunk.strip()
44
- if stripped_chunk != "":
45
- cleaned_chunks.append(stripped_chunk)
46
- # Print cleaned_chunks
47
- print(cleaned_chunks)
48
- # Print the length of cleaned_chunks
49
- print(len(cleaned_chunks))
50
- # Return the cleaned_chunks
51
- return cleaned_chunks
52
- # Call the preprocess_text function and store the result in a cleaned_chunks variable
53
- cleaned_chunks = preprocess_text(research_text) # Complete this line
54
-
55
- # STEP 4 FROM SEMANTIC SEARCH
56
- # Load the pre-trained embedding model that converts text to vectors
57
- model = SentenceTransformer('all-MiniLM-L6-v2')
58
 
59
- def create_embeddings(text_chunks):
60
- # Convert each text chunk into a vector embedding and store as a tensor
61
- chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True) # Replace ... with the text_chunks list
62
-
63
- # Print the chunk embeddings
64
- print(chunk_embeddings)
65
 
66
- # Print the shape of chunk_embeddings
67
- print(chunk_embeddings.shape)
68
 
69
- # Return the chunk_embeddings
70
- return chunk_embeddings
 
71
 
72
- # Call the create_embeddings function and store the result in a new chunk_embeddings variable
73
- chunk_embeddings = create_embeddings(cleaned_chunks) # Complete this line
74
 
75
- #STEP 5 IN SEMANTIC SEARCH
76
  def get_top_chunks(query, chunk_embeddings, text_chunks):
77
- # Convert the query text into a vector embedding
78
- query_embedding = model.encode(query,convert_to_tensor=True) # Complete this line
79
-
80
- # Normalize the query embedding to unit length for accurate similarity comparison
81
- query_embedding_normalized = query_embedding / query_embedding.norm()
82
-
83
- # Normalize all chunk embeddings to unit length for consistent comparison
84
- chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
85
-
86
- # Calculate cosine similarity between query and all chunks using matrix multiplication
87
- similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized) # Complete this line
88
-
89
- # Print the similarities
90
- print(similarities)
91
-
92
- # Find the indices of the 3 chunks with highest similarity scores
93
- top_indices = torch.topk(similarities, k=3).indices
94
-
95
- # Print the top indices
96
- print(top_indices)
97
 
98
- # Create an empty list to store the most relevant chunks
99
- top_chunks = []
100
 
101
- # Loop through the top indices and retrieve the corresponding text chunks
102
- for i in top_indices:
103
- top_chunks.append(text_chunks[i])
104
-
105
- # Return the list of most relevant chunks
106
- return top_chunks
107
-
108
- #STEP 6 IN SEMANTIC SEARCH
109
- # Call the get_top_chunks function with the original query
110
- top_results = ('How does water get into sky?',chunk_embeddings, cleaned_chunks) # Complete this line
111
-
112
- # Print the top results
113
- print(top_results)
114
-
115
- client=InferenceClient("Qwen/Qwen2.5-72B-Instruct")
116
  def respond(message, history):
117
- top_results= get_top_chunks(message, chunk_embeddings, cleaned_chunks)
118
- str_top_results= '\n'.join(top_results)
119
- messages = [{'role':'system', 'content': f'You are a chatbot. Complete all your sentences, do not be blunt, and do not cut yourself off. The word limit is 100 words. Start off by giving a career in a complete, kind sentence, and then if prompted by the user provide more information like salary, college course,etc. Base your response on the provided context:\n{str_top_results}'}]
 
 
120
  if history:
121
  messages.extend(history)
122
-
123
- messages.append({'role':'user','content':message})
124
 
125
  response = client.chat_completion(
126
  messages,
127
- max_tokens = 1000,
128
- temperature = 0.2
129
  )
130
  return response['choices'][0]['message']['content'].strip()
131
 
132
  def display_image():
133
  return "KWKbanner.png"
134
 
 
135
  def show_info(topic):
136
  responses = {
137
- "Highest Paying STEM Jobs": " 1. AI/Machine Learning Engineer – $171,774 \n 2. Cloud Solutions Architect – $150,241 \n 3. Quantitative Analyst (Quant) – $139,949 \n 4. Data Scientist – $128,115 \n 5. Actuary – $128,147",
138
- "Most Flexible STEM Jobs": " 1. Software Developer \n 2. Cloud Solutions Architect \n 3. Data Scientist \n 4. Cybersecurity Analyst \n 5. Statistician",
139
- "Most Creative STEM Jobs": " 1. Software Developer \n 2. AI/Machine Learning Engineer \n 3. Biomedical Engineer \n 4, Mechanical Engineer \n 5. Biochemist",
140
- "Fastest Growing STEM Jobs": " 1. AI/Machine Learning Engineer \n 2. Cybersecurity Analyst \n 3. Data Scientist \n 4. Software Developer \n 5. Cloud Solutions Architect",
141
- "Low-Stress STEM Jobs": " 1. Statistician \n 2. Mathematician \n 3. Operations Research Analyst \n 4. Environmental Scientist \n 5. Biochemist"
142
  }
143
  return responses.get(topic, "Select a category to see the corresponding careers.")
144
 
 
 
 
 
 
 
 
 
 
 
 
145
 
 
146
  with gr.Blocks(theme=theme, css=custom_css) as chatbot:
147
  gr.Image(display_image)
148
 
149
- explore_page= "Select a cateogry to see the corresponding careers."
150
-
151
  with gr.Tabs():
152
  with gr.Tab("ChatBot"):
153
  gr.ChatInterface(
@@ -157,23 +111,36 @@ with gr.Blocks(theme=theme, css=custom_css) as chatbot:
157
  textbox=gr.Textbox(placeholder="Share your interests and explore more on your career of choice!"),
158
  description="This tool provides information on STEM Careers."
159
  )
160
- with gr.Tab("Explore Page"):
161
- gr.Markdown("")
162
-
163
-
164
-
165
- with gr.TabItem("Explore Page"):
166
- gr.Markdown(explore_page)
167
- #gr.Markdown(explore_page)
168
- dropdown = gr.Dropdown(choices=["Highest Paying STEM Jobs", "Most Flexible STEM Jobs", "Most Creative STEM Jobs", "Fastest Growing STEM Jobs", "Low-Stress STEM Jobs"],
169
- label="Choose Your Category"
170
- )
171
- output = gr.Textbox(label="Top-Ranked Jobs", interactive=False)
172
-
173
- dropdown.change(fn=show_info, inputs=dropdown, outputs=output)
174
- #with gr.Tab("Educational PDFs"):
175
- # gr.Markdown("### 📘 Helpful Resources")
176
-
177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
 
179
- chatbot.launch()
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # STEP 1 FROM SEMANTIC SEARCH
5
  from sentence_transformers import SentenceTransformer
6
  import torch
7
 
 
9
  primary_hue="rose",
10
  secondary_hue="zinc",
11
  neutral_hue="pink"
12
+ )
13
 
14
  custom_css = """
15
+ :root {
16
+ --background-fill-primary: #FFB6C2 !important;
17
  }
18
+ .dark {
19
+ --background-fill-primary: #FFB6C1 !important;
20
  }
21
  """
22
 
23
+ # STEP 2: Load research file
 
24
  with open("research.txt", "r", encoding="utf-8") as file:
25
+ research_text = file.read()
 
26
 
27
+ # STEP 3: Preprocess text
 
 
 
28
  def preprocess_text(text):
29
+ cleaned_text = text.strip()
30
+ chunks = cleaned_text.split("\n")
31
+ cleaned_chunks = [chunk.strip() for chunk in chunks if chunk.strip() != ""]
32
+ return cleaned_chunks
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ cleaned_chunks = preprocess_text(research_text)
 
 
 
 
 
35
 
36
+ # STEP 4: Create embeddings
37
+ model = SentenceTransformer('all-MiniLM-L6-v2')
38
 
39
+ def create_embeddings(text_chunks):
40
+ chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True)
41
+ return chunk_embeddings
42
 
43
+ chunk_embeddings = create_embeddings(cleaned_chunks)
 
44
 
45
+ # STEP 5: Get top chunks
46
  def get_top_chunks(query, chunk_embeddings, text_chunks):
47
+ query_embedding = model.encode(query, convert_to_tensor=True)
48
+ query_embedding_normalized = query_embedding / query_embedding.norm()
49
+ chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
50
+ similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
51
+ top_indices = torch.topk(similarities, k=3).indices
52
+ top_chunks = [text_chunks[i] for i in top_indices]
53
+ return top_chunks
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
+ # STEP 6: Inference client
56
+ client = InferenceClient("Qwen/Qwen2.5-72B-Instruct")
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  def respond(message, history):
59
+ top_results = get_top_chunks(message, chunk_embeddings, cleaned_chunks)
60
+ str_top_results = '\n'.join(top_results)
61
+ messages = [
62
+ {'role': 'system', 'content': f'You are a chatbot. Complete all your sentences, do not be blunt, and do not cut yourself off. The word limit is 100 words. Start off by giving a career in a complete, kind sentence, and then if prompted by the user provide more information like salary, college course,etc. Base your response on the provided context:\n{str_top_results}'}
63
+ ]
64
  if history:
65
  messages.extend(history)
66
+ messages.append({'role': 'user', 'content': message})
 
67
 
68
  response = client.chat_completion(
69
  messages,
70
+ max_tokens=1000,
71
+ temperature=0.2
72
  )
73
  return response['choices'][0]['message']['content'].strip()
74
 
75
  def display_image():
76
  return "KWKbanner.png"
77
 
78
+ # EXPLORE PAGE DATA
79
  def show_info(topic):
80
  responses = {
81
+ "Highest Paying STEM Jobs": "1. AI/Machine Learning Engineer – $171,774\n2. Cloud Solutions Architect – $150,241\n3. Quantitative Analyst (Quant) – $139,949\n4. Data Scientist – $128,115\n5. Actuary – $128,147",
82
+ "Most Flexible STEM Jobs": "1. Software Developer\n2. Cloud Solutions Architect\n3. Data Scientist\n4. Cybersecurity Analyst\n5. Statistician",
83
+ "Most Creative STEM Jobs": "1. Software Developer\n2. AI/Machine Learning Engineer\n3. Biomedical Engineer\n4. Mechanical Engineer\n5. Biochemist",
84
+ "Fastest Growing STEM Jobs": "1. AI/Machine Learning Engineer\n2. Cybersecurity Analyst\n3. Data Scientist\n4. Software Developer\n5. Cloud Solutions Architect",
85
+ "Low-Stress STEM Jobs": "1. Statistician\n2. Mathematician\n3. Operations Research Analyst\n4. Environmental Scientist\n5. Biochemist"
86
  }
87
  return responses.get(topic, "Select a category to see the corresponding careers.")
88
 
89
+ # RESOURCES PAGE DATA
90
+ def resource_links(career):
91
+ links = {
92
+ "AI/Machine Learning Engineer": "• [DeepLearning.AI](https://www.deeplearning.ai)\n• [Fast.ai](https://www.fast.ai)\n• [Stanford CS229](https://cs229.stanford.edu/)",
93
+ "Data Scientist": "• [Kaggle Learn](https://www.kaggle.com/learn)\n• [Harvard’s Data Science](https://online-learning.harvard.edu/series/data-science)\n• [DataCamp](https://www.datacamp.com)",
94
+ "Cloud Solutions Architect": "• [AWS Training](https://aws.amazon.com/training/)\n• [Azure Certification Paths](https://learn.microsoft.com/en-us/certifications/)\n• [Google Cloud Skills Boost](https://cloudskillsboost.google/)",
95
+ "Cybersecurity Analyst": "• [Cybrary](https://www.cybrary.it)\n• [MITRE ATT&CK](https://attack.mitre.org/)\n• [TryHackMe](https://tryhackme.com)",
96
+ "Statisticians": "• [Coursera: Statistics with R](https://www.coursera.org/specializations/statistics)\n• [Khan Academy: Statistics](https://www.khanacademy.org/math/statistics-probability)",
97
+ "Biomedical Engineer": "• [Johns Hopkins BME](https://www.bme.jhu.edu/)\n• [edX Biomedical Courses](https://www.edx.org/learn/biomedical-engineering)\n• [NIH Biomedical Research](https://www.nih.gov/)"
98
+ }
99
+ return links.get(career, "Select a career to see recommended learning platforms and resources.")
100
 
101
+ # UI
102
  with gr.Blocks(theme=theme, css=custom_css) as chatbot:
103
  gr.Image(display_image)
104
 
 
 
105
  with gr.Tabs():
106
  with gr.Tab("ChatBot"):
107
  gr.ChatInterface(
 
111
  textbox=gr.Textbox(placeholder="Share your interests and explore more on your career of choice!"),
112
  description="This tool provides information on STEM Careers."
113
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
+ with gr.Tab("Explore Page"):
116
+ gr.Markdown("### 🌟 Explore STEM Career Categories")
117
+ dropdown_explore = gr.Dropdown(
118
+ choices=[
119
+ "Highest Paying STEM Jobs",
120
+ "Most Flexible STEM Jobs",
121
+ "Most Creative STEM Jobs",
122
+ "Fastest Growing STEM Jobs",
123
+ "Low-Stress STEM Jobs"
124
+ ],
125
+ label="Choose a Category"
126
+ )
127
+ output_explore = gr.Textbox(label="Top-Ranked Jobs", interactive=False)
128
+ dropdown_explore.change(fn=show_info, inputs=dropdown_explore, outputs=output_explore)
129
+
130
+ with gr.Tab("Resources Page"):
131
+ gr.Markdown("### 📚 Career-Specific Educational Resources")
132
+ dropdown_resources = gr.Dropdown(
133
+ choices=[
134
+ "AI/Machine Learning Engineer",
135
+ "Data Scientist",
136
+ "Cloud Solutions Architect",
137
+ "Cybersecurity Analyst",
138
+ "Statisticians",
139
+ "Biomedical Engineer"
140
+ ],
141
+ label="Choose a Career"
142
+ )
143
+ output_resources = gr.Textbox(label="Recommended Resources", interactive=False)
144
+ dropdown_resources.change(fn=resource_links, inputs=dropdown_resources, outputs=output_resources)
145
 
146
+ chatbot.launch()