Spaces:
Sleeping
Sleeping
update
Browse files
app.py
CHANGED
|
@@ -1,43 +1,175 @@
|
|
| 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 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
def
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
"
|
| 31 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
-
return links.get(career, "Select a career to see resources.")
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
with gr.Tabs():
|
| 37 |
-
with gr.Tab("
|
| 38 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
with gr.Tab("Explore
|
|
|
|
| 41 |
dropdown_explore = gr.Dropdown(
|
| 42 |
choices=[
|
| 43 |
"Highest Paying STEM Jobs",
|
|
@@ -51,7 +183,8 @@ with gr.Blocks() as demo:
|
|
| 51 |
output_explore = gr.Textbox(label="Top-Ranked Jobs", interactive=False)
|
| 52 |
dropdown_explore.change(fn=show_info, inputs=dropdown_explore, outputs=output_explore)
|
| 53 |
|
| 54 |
-
with gr.Tab("Resources Page"):
|
|
|
|
| 55 |
dropdown_resources = gr.Dropdown(
|
| 56 |
choices=[
|
| 57 |
"AI/Machine Learning Engineer",
|
|
@@ -63,9 +196,14 @@ with gr.Blocks() as demo:
|
|
| 63 |
],
|
| 64 |
label="Choose a Career"
|
| 65 |
)
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
|
| 71 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
| 3 |
from sentence_transformers import SentenceTransformer
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# Theme
|
| 7 |
+
theme = gr.themes.Soft(
|
| 8 |
+
primary_hue="rose",
|
| 9 |
+
secondary_hue="zinc",
|
| 10 |
+
neutral_hue="pink"
|
| 11 |
+
)
|
| 12 |
|
| 13 |
+
custom_css = """
|
| 14 |
+
:root {
|
| 15 |
+
--background-fill-primary: #FFB6C2 !important;
|
| 16 |
+
}
|
| 17 |
+
.dark {
|
| 18 |
+
--background-fill-primary: #FFB6C1 !important;
|
| 19 |
}
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
# Load research file
|
| 23 |
+
with open("research.txt", "r", encoding="utf-8") as file:
|
| 24 |
+
research_text = file.read()
|
| 25 |
+
|
| 26 |
+
# Preprocess text
|
| 27 |
+
def preprocess_text(text):
|
| 28 |
+
cleaned_text = text.strip()
|
| 29 |
+
chunks = cleaned_text.split("\n")
|
| 30 |
+
cleaned_chunks = [chunk.strip() for chunk in chunks if chunk.strip() != ""]
|
| 31 |
+
return cleaned_chunks
|
| 32 |
+
|
| 33 |
+
cleaned_chunks = preprocess_text(research_text)
|
| 34 |
+
|
| 35 |
+
# Create embeddings
|
| 36 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 37 |
+
|
| 38 |
+
def create_embeddings(text_chunks):
|
| 39 |
+
chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True)
|
| 40 |
+
return chunk_embeddings
|
| 41 |
+
|
| 42 |
+
chunk_embeddings = create_embeddings(cleaned_chunks)
|
| 43 |
+
|
| 44 |
+
# Get top chunks
|
| 45 |
+
def get_top_chunks(query, chunk_embeddings, text_chunks):
|
| 46 |
+
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 47 |
+
query_embedding_normalized = query_embedding / query_embedding.norm()
|
| 48 |
+
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
|
| 49 |
+
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
|
| 50 |
+
top_indices = torch.topk(similarities, k=3).indices
|
| 51 |
+
top_chunks = [text_chunks[i] for i in top_indices]
|
| 52 |
+
return top_chunks
|
| 53 |
+
|
| 54 |
+
# Inference client
|
| 55 |
+
client = InferenceClient("Qwen/Qwen2.5-72B-Instruct")
|
| 56 |
|
| 57 |
+
def respond(message, history):
|
| 58 |
+
top_results = get_top_chunks(message, chunk_embeddings, cleaned_chunks)
|
| 59 |
+
str_top_results = '\n'.join(top_results)
|
| 60 |
+
messages = [
|
| 61 |
+
{'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}'}
|
| 62 |
+
]
|
| 63 |
+
if history:
|
| 64 |
+
messages.extend(history)
|
| 65 |
+
messages.append({'role': 'user', 'content': message})
|
| 66 |
|
| 67 |
+
response = client.chat_completion(
|
| 68 |
+
messages,
|
| 69 |
+
max_tokens=1000,
|
| 70 |
+
temperature=0.2
|
| 71 |
+
)
|
| 72 |
+
return response['choices'][0]['message']['content'].strip()
|
| 73 |
|
| 74 |
+
def display_image():
|
| 75 |
+
return "KWKbanner.png"
|
| 76 |
+
|
| 77 |
+
# Explore Page Info
|
| 78 |
+
def show_info(topic):
|
| 79 |
+
responses = {
|
| 80 |
+
"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",
|
| 81 |
+
"Most Flexible STEM Jobs": "1. Software Developer\n2. Cloud Solutions Architect\n3. Data Scientist\n4. Cybersecurity Analyst\n5. Statistician",
|
| 82 |
+
"Most Creative STEM Jobs": "1. Software Developer\n2. AI/Machine Learning Engineer\n3. Biomedical Engineer\n4. Mechanical Engineer\n5. Biochemist",
|
| 83 |
+
"Fastest Growing STEM Jobs": "1. AI/Machine Learning Engineer\n2. Cybersecurity Analyst\n3. Data Scientist\n4. Software Developer\n5. Cloud Solutions Architect",
|
| 84 |
+
"Low-Stress STEM Jobs": "1. Statistician\n2. Mathematician\n3. Operations Research Analyst\n4. Environmental Scientist\n5. Biochemist"
|
| 85 |
+
}
|
| 86 |
+
return responses.get(topic, "Select a category to see the corresponding careers.")
|
| 87 |
+
|
| 88 |
+
# Resources Page Info (HTML + Embedded Video)
|
| 89 |
+
def resource_block(career):
|
| 90 |
+
resources = {
|
| 91 |
+
"AI/Machine Learning Engineer": {
|
| 92 |
+
"links": [
|
| 93 |
+
("DeepLearning.AI", "https://www.deeplearning.ai"),
|
| 94 |
+
("Fast.ai", "https://www.fast.ai"),
|
| 95 |
+
("Stanford CS229", "https://cs229.stanford.edu/")
|
| 96 |
+
],
|
| 97 |
+
"video": "https://www.youtube.com/embed/5NgNicANyqM"
|
| 98 |
+
},
|
| 99 |
+
"Data Scientist": {
|
| 100 |
+
"links": [
|
| 101 |
+
("Kaggle Learn", "https://www.kaggle.com/learn"),
|
| 102 |
+
("Harvard Data Science", "https://online-learning.harvard.edu/series/data-science"),
|
| 103 |
+
("DataCamp", "https://www.datacamp.com")
|
| 104 |
+
],
|
| 105 |
+
"video": "https://www.youtube.com/embed/xC-c7E5PK0Y"
|
| 106 |
+
},
|
| 107 |
+
"Cloud Solutions Architect": {
|
| 108 |
+
"links": [
|
| 109 |
+
("AWS Training", "https://aws.amazon.com/training/"),
|
| 110 |
+
("Azure Certifications", "https://learn.microsoft.com/en-us/certifications/"),
|
| 111 |
+
("Google Cloud Boost", "https://cloudskillsboost.google/")
|
| 112 |
+
],
|
| 113 |
+
"video": "https://www.youtube.com/embed/Y1OVgGIGvfc"
|
| 114 |
+
},
|
| 115 |
+
"Cybersecurity Analyst": {
|
| 116 |
+
"links": [
|
| 117 |
+
("Cybrary", "https://www.cybrary.it"),
|
| 118 |
+
("MITRE ATT&CK", "https://attack.mitre.org/"),
|
| 119 |
+
("TryHackMe", "https://tryhackme.com")
|
| 120 |
+
],
|
| 121 |
+
"video": "https://www.youtube.com/embed/9fWjKkFvQxg"
|
| 122 |
+
},
|
| 123 |
+
"Statisticians": {
|
| 124 |
+
"links": [
|
| 125 |
+
("Statistics with R (Coursera)", "https://www.coursera.org/specializations/statistics"),
|
| 126 |
+
("Khan Academy Statistics", "https://www.khanacademy.org/math/statistics-probability")
|
| 127 |
+
],
|
| 128 |
+
"video": "https://www.youtube.com/embed/xxpc-HPKN28"
|
| 129 |
+
},
|
| 130 |
+
"Biomedical Engineer": {
|
| 131 |
+
"links": [
|
| 132 |
+
("Johns Hopkins BME", "https://www.bme.jhu.edu/"),
|
| 133 |
+
("edX Biomedical Courses", "https://www.edx.org/learn/biomedical-engineering"),
|
| 134 |
+
("NIH Biomedical Research", "https://www.nih.gov/")
|
| 135 |
+
],
|
| 136 |
+
"video": "https://www.youtube.com/embed/NM5EekDaF3g"
|
| 137 |
+
}
|
| 138 |
}
|
|
|
|
| 139 |
|
| 140 |
+
content = resources.get(career)
|
| 141 |
+
if not content:
|
| 142 |
+
return "Select a career to see resources.", ""
|
| 143 |
+
|
| 144 |
+
link_html = "<ul>"
|
| 145 |
+
for name, url in content["links"]:
|
| 146 |
+
link_html += f'<li><a href="{url}" target="_blank">{name}</a></li>'
|
| 147 |
+
link_html += "</ul>"
|
| 148 |
+
|
| 149 |
+
video_iframe = f"""
|
| 150 |
+
<iframe width="560" height="315" src="{content['video']}"
|
| 151 |
+
title="YouTube video player" frameborder="0" allow="accelerometer; autoplay;
|
| 152 |
+
clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
|
| 153 |
+
"""
|
| 154 |
+
|
| 155 |
+
return link_html, video_iframe
|
| 156 |
+
|
| 157 |
+
# UI Layout
|
| 158 |
+
with gr.Blocks(theme=theme, css=custom_css) as chatbot:
|
| 159 |
+
gr.Image(display_image)
|
| 160 |
+
|
| 161 |
with gr.Tabs():
|
| 162 |
+
with gr.Tab("ChatBot"):
|
| 163 |
+
gr.ChatInterface(
|
| 164 |
+
respond,
|
| 165 |
+
type="messages",
|
| 166 |
+
title="Hi, I'm Path Pilot!",
|
| 167 |
+
textbox=gr.Textbox(placeholder="Share your interests and explore more on your career of choice!"),
|
| 168 |
+
description="This tool provides information on STEM Careers."
|
| 169 |
+
)
|
| 170 |
|
| 171 |
+
with gr.Tab("Explore Page"):
|
| 172 |
+
gr.Markdown("### Explore STEM Career Categories")
|
| 173 |
dropdown_explore = gr.Dropdown(
|
| 174 |
choices=[
|
| 175 |
"Highest Paying STEM Jobs",
|
|
|
|
| 183 |
output_explore = gr.Textbox(label="Top-Ranked Jobs", interactive=False)
|
| 184 |
dropdown_explore.change(fn=show_info, inputs=dropdown_explore, outputs=output_explore)
|
| 185 |
|
| 186 |
+
with gr.Tab("Resources Page"):
|
| 187 |
+
gr.Markdown("### Career-Specific Educational Resources")
|
| 188 |
dropdown_resources = gr.Dropdown(
|
| 189 |
choices=[
|
| 190 |
"AI/Machine Learning Engineer",
|
|
|
|
| 196 |
],
|
| 197 |
label="Choose a Career"
|
| 198 |
)
|
| 199 |
+
output_links = gr.HTML()
|
| 200 |
+
output_video = gr.HTML()
|
| 201 |
+
dropdown_resources.change(
|
| 202 |
+
fn=resource_block,
|
| 203 |
+
inputs=dropdown_resources,
|
| 204 |
+
outputs=[output_links, output_video]
|
| 205 |
+
)
|
| 206 |
|
| 207 |
+
chatbot.launch()
|
| 208 |
|
| 209 |
|