KWKfirstSF / app.py
mahika123's picture
Update app.py
0f08973 verified
import gradio as gr
from huggingface_hub import InferenceClient
from sentence_transformers import SentenceTransformer
import torch
# Theme
theme = gr.themes.Soft(
primary_hue="cyan", # ocean blue
secondary_hue="blue", # deeper ocean accent
neutral_hue="slate", # cool, balanced gray tone
)
custom_css = """
:root {
--background-fill-primary: #C9361C !important;
}
.dark {
--background-fill-primary: #C9361C !important;
}
"""
# Load research file
with open("research.txt", "r", encoding="utf-8") as file:
research_text = file.read()
# Preprocess text
def preprocess_text(text):
cleaned_text = text.strip()
chunks = cleaned_text.split("\n")
cleaned_chunks = [chunk.strip() for chunk in chunks if chunk.strip() != ""]
return cleaned_chunks
cleaned_chunks = preprocess_text(research_text)
# Create embeddings
model = SentenceTransformer('all-MiniLM-L6-v2')
def create_embeddings(text_chunks):
chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True)
return chunk_embeddings
chunk_embeddings = create_embeddings(cleaned_chunks)
# Get top chunks
def get_top_chunks(query, chunk_embeddings, text_chunks):
query_embedding = model.encode(query, convert_to_tensor=True)
query_embedding_normalized = query_embedding / query_embedding.norm()
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
top_indices = torch.topk(similarities, k=3).indices
top_chunks = [text_chunks[i] for i in top_indices]
return top_chunks
# Inference client
client = InferenceClient("Qwen/Qwen2.5-72B-Instruct")
def respond(message, history):
top_results = get_top_chunks(message, chunk_embeddings, cleaned_chunks)
str_top_results = '\n'.join(top_results)
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}'}
]
if history:
messages.extend(history)
messages.append({'role': 'user', 'content': message})
response = client.chat_completion(
messages,
max_tokens=1000,
temperature=0.2
)
return response['choices'][0]['message']['content'].strip()
def display_image():
return "KWKbanner.png"
# Explore Page Info
def show_info(topic):
responses = {
"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",
"Most Flexible STEM Jobs": "1. Software Developer\n2. Cloud Solutions Architect\n3. Data Scientist\n4. Cybersecurity Analyst\n5. Statistician",
"Most Creative STEM Jobs": "1. Software Developer\n2. AI/Machine Learning Engineer\n3. Biomedical Engineer\n4. Mechanical Engineer\n5. Biochemist",
"Fastest Growing STEM Jobs": "1. AI/Machine Learning Engineer\n2. Cybersecurity Analyst\n3. Data Scientist\n4. Software Developer\n5. Cloud Solutions Architect",
"Low-Stress STEM Jobs": "1. Statistician\n2. Mathematician\n3. Operations Research Analyst\n4. Environmental Scientist\n5. Biochemist"
}
return responses.get(topic, "Select a category to see the corresponding careers.")
# Resources Page Info - UPDATED
def resource_block(career):
resources = {
"AI/Machine Learning Engineer": {
"links": [
("Neural Networks – DeepLearning.AI", "https://www.deeplearning.ai"),
("Build ML Models – Fast.ai", "https://www.fast.ai"),
("Machine Learning – Stanford CS229", "https://cs229.stanford.edu/")
],
"college": {
"major": "Computer Science, Data Science",
"classes": [
"CS50: Introduction to Computer Science (Harvard)",
"Linear Algebra",
"Probability and Statistics",
"Machine Learning (Stanford CS229)",
"Algorithms"
]
}
},
"Data Scientist": {
"links": [
("Python & Pandas – Kaggle Learn", "https://www.kaggle.com/learn"),
("R Programming – Harvard Data Science", "https://online-learning.harvard.edu/series/data-science"),
("Project Practice – DataCamp", "https://www.datacamp.com")
],
"college": {
"major": "Data Science, Statistics, Computer Science",
"classes": [
"Introduction to Data Science",
"Statistics and Probability",
"Data Mining",
"Machine Learning",
"Database Systems"
]
}
},
"Cloud Solutions Architect": {
"links": [
("AWS Skills – AWS Training", "https://aws.amazon.com/training/"),
("Azure Certifications – Microsoft Learn", "https://learn.microsoft.com/en-us/certifications/"),
("Google Cloud Labs – Google Cloud Boost", "https://cloudskillsboost.google/")
],
"college": {
"major": "Computer Science, Information Technology",
"classes": [
"Cloud Computing Fundamentals",
"Computer Networks",
"Systems Design",
"Information Security",
"Operating Systems"
]
}
},
"Cybersecurity Analyst": {
"links": [
("Network Security – Cybrary", "https://www.cybrary.it"),
("Threat Intelligence – MITRE ATT&CK", "https://attack.mitre.org/"),
("Ethical Hacking – TryHackMe", "https://tryhackme.com")
],
"college": {
"major": "Cybersecurity, Computer Science, Information Security",
"classes": [
"Network Security",
"Cryptography",
"Ethical Hacking",
"Operating Systems",
"Incident Response"
]
}
},
"Statistician": {
"links": [
("Intro to Statistics – Coursera (R)", "https://www.coursera.org/specializations/statistics"),
("Probability – Khan Academy", "https://www.khanacademy.org/math/statistics-probability"),
("Statistical Tools – OpenIntro", "https://www.openintro.org/book/os/")
],
"college": {
"major": "Statistics, Mathematics",
"classes": [
"Probability Theory",
"Statistical Inference",
"Regression Analysis",
"Experimental Design",
"Data Analysis with R"
]
}
},
"Biomedical Engineer": {
"links": [
("Biomedical Research – JHU BME", "https://www.bme.jhu.edu/"),
("Medical Devices – edX Courses", "https://www.edx.org/learn/biomedical-engineering"),
("Clinical Trials – NIH", "https://www.nih.gov/")
],
"college": {
"major": "Biomedical Engineering, Bioengineering",
"classes": [
"Biomaterials",
"Human Physiology",
"Medical Instrumentation",
"Biomechanics",
"Tissue Engineering"
]
}
},
"Mechanical Engineer": {
"links": [
("CAD Design – Coursera", "https://www.coursera.org/learn/cad-design"),
("Thermodynamics – MIT OpenCourseWare", "https://ocw.mit.edu/courses/thermodynamics"),
("Materials Science Basics – edX", "https://www.edx.org/course/material-science")
],
"college": {
"major": "Mechanical Engineering",
"classes": [
"Thermodynamics",
"Fluid Mechanics",
"Materials Science",
"Computer-Aided Design (CAD)",
"Dynamics and Control"
]
}
},
"Environmental Scientist": {
"links": [
("Environmental Science – Khan Academy", "https://www.khanacademy.org/science/biology/ecology"),
("GIS Basics – Esri Training", "https://www.esri.com/training/catalog/57630435851d31e02a43f1c5/gis-basics/"),
("Data Analysis – Coursera", "https://www.coursera.org/learn/data-analysis")
],
"college": {
"major": "Environmental Science, Ecology",
"classes": [
"Ecology",
"Environmental Chemistry",
"Geographic Information Systems (GIS)",
"Data Analysis",
"Environmental Policy"
]
}
},
"Operations Research Analyst": {
"links": [
("Linear Programming – Khan Academy", "https://www.khanacademy.org/computing/computer-science/algorithms"),
("Optimization – MIT OpenCourseWare", "https://ocw.mit.edu/courses/optimization-methods"),
("Statistics – Harvard Online", "https://online-learning.harvard.edu/course/statistics-and-r")
],
"college": {
"major": "Operations Research, Applied Mathematics",
"classes": [
"Optimization Theory",
"Linear Programming",
"Probability",
"Statistics",
"Simulation Modeling"
]
}
},
"Mathematician": {
"links": [
("Abstract Algebra – MIT OpenCourseWare", "https://ocw.mit.edu/courses/abstract-algebra"),
("Calculus – Khan Academy", "https://www.khanacademy.org/math/calculus-1"),
("Proof Techniques – Coursera", "https://www.coursera.org/learn/proofs")
],
"college": {
"major": "Mathematics",
"classes": [
"Algebra",
"Calculus",
"Real Analysis",
"Abstract Algebra",
"Proof Writing"
]
}
},
"Chemical Engineer": {
"links": [
("Chemical Process Principles – MIT OCW", "https://ocw.mit.edu/courses/chemical-engineering"),
("Organic Chemistry – Khan Academy", "https://www.khanacademy.org/science/organic-chemistry"),
("Thermodynamics – Coursera", "https://www.coursera.org/learn/thermodynamics")
],
"college": {
"major": "Chemical Engineering",
"classes": [
"Organic Chemistry",
"Thermodynamics",
"Process Design",
"Fluid Mechanics",
"Chemical Reaction Engineering"
]
}
},
"Civil Engineer": {
"links": [
("Structural Analysis – Coursera", "https://www.coursera.org/learn/structural-analysis"),
("Construction Management – edX", "https://www.edx.org/course/construction-management"),
("AutoCAD – LinkedIn Learning", "https://www.linkedin.com/learning/topics/autocad")
],
"college": {
"major": "Civil Engineering",
"classes": [
"Structural Analysis",
"Construction Materials",
"Soil Mechanics",
"AutoCAD",
"Hydraulics"
]
}
},
"Electrical Engineer": {
"links": [
("Circuits and Electronics – MIT OCW", "https://ocw.mit.edu/courses/electrical-engineering-and-computer-science"),
("Signals and Systems – Coursera", "https://www.coursera.org/learn/signals-systems"),
("Electromagnetics – Khan Academy", "https://www.khanacademy.org/science/electrical-engineering")
],
"college": {
"major": "Electrical Engineering",
"classes": [
"Circuits",
"Signals and Systems",
"Electromagnetics",
"Control Systems",
"Digital Logic Design"
]
}
},
"Software Developer": {
"links": [
("CS50 – Harvard", "https://cs50.harvard.edu"),
("Learn to Code – Codecademy", "https://www.codecademy.com/catalog/subject/all"),
("Algorithms – Coursera", "https://www.coursera.org/learn/algorithms-part1")
],
"college": {
"major": "Computer Science, Software Engineering",
"classes": [
"Introduction to Computer Science (CS50)",
"Data Structures and Algorithms",
"Operating Systems",
"Software Engineering",
"Databases"
]
}
},
"Pharmacist": {
"links": [
("Pharmacology Basics – Coursera", "https://www.coursera.org/learn/pharmacology"),
("Drug Development – edX", "https://www.edx.org/course/drug-development"),
("Pharmacy Practice – FutureLearn", "https://www.futurelearn.com/courses/pharmacy-practice")
],
"college": {
"major": "Pharmacy, Pharmaceutical Sciences",
"classes": [
"Pharmacology",
"Medicinal Chemistry",
"Pharmaceutical Calculations",
"Pharmaceutics",
"Clinical Pharmacy"
]
}
},
"Physicist": {
"links": [
("Classical Mechanics – MIT OCW", "https://ocw.mit.edu/courses/physics"),
("Quantum Mechanics – edX", "https://www.edx.org/course/quantum-mechanics"),
("Thermodynamics – Khan Academy", "https://www.khanacademy.org/science/physics/thermodynamics")
],
"college": {
"major": "Physics",
"classes": [
"Classical Mechanics",
"Quantum Mechanics",
"Thermodynamics",
"Electromagnetism",
"Mathematical Methods for Physicists"
]
}
},
"Astronomer": {
"links": [
("Introduction to Astronomy – Coursera", "https://www.coursera.org/learn/astronomy"),
("Astrophysics – edX", "https://www.edx.org/course/astrophysics"),
("Cosmology – Khan Academy", "https://www.khanacademy.org/science/cosmology-and-astronomy")
],
"college": {
"major": "Astronomy, Astrophysics, Physics",
"classes": [
"Introduction to Astronomy",
"Astrophysics",
"Cosmology",
"Observational Astronomy",
"Data Analysis in Astronomy"
]
}
},
"Geologist": {
"links": [
("Physical Geology – OpenStax", "https://openstax.org/details/books/physical-geology"),
("Geochemistry – Coursera", "https://www.coursera.org/learn/geochemistry"),
("GIS Mapping – Esri Training", "https://www.esri.com/training/catalog/57630435851d31e02a43f1c5/gis-basics/")
],
"college": {
"major": "Geology, Earth Science",
"classes": [
"Physical Geology",
"Mineralogy and Petrology",
"Geochemistry",
"GIS and Remote Sensing",
"Structural Geology"
]
}
},
"Biochemist": {
"links": [
("Biochemistry – MIT OCW", "https://ocw.mit.edu/courses/biochemistry"),
("Molecular Biology – Coursera", "https://www.coursera.org/learn/molecular-biology"),
("Enzymology – Khan Academy", "https://www.khanacademy.org/science/biology")
],
"college": {
"major": "Biochemistry, Molecular Biology",
"classes": [
"General Biochemistry",
"Molecular Biology",
"Enzymology",
"Cell Biology",
"Genetics"
]
}
}
}
content = resources.get(career)
if not content:
return "Select a career to see resources.", ""
link_html = "<ul>"
for label, url in content["links"]:
link_html += f'<li><strong>{label}</strong>: <a href="{url}" target="_blank">{url}</a></li>'
link_html += "</ul>"
college_html = ""
if "college" in content:
college = content["college"]
college_html += "<p><strong>College & Classes</strong></p><ul>"
college_html += f"<li><em>Common Major(s):</em> {college['major']}</li>"
classes_list = college.get("classes", [])
if isinstance(classes_list, list):
classes_html = ", ".join(classes_list)
else:
classes_html = str(classes_list)
college_html += f"<li><em>Helpful College Classes:</em> {classes_html}</li>"
college_html += "</ul>"
# No video iframe, just empty string for second output
return link_html + college_html, ""
# UI Layout
with gr.Blocks(theme=theme, css=custom_css) as chatbot:
gr.Image(display_image)
with gr.Tab("ChatBot"):
gr.ChatInterface(
respond,
type="messages",
title="Hi, I am your basketball chatbot",
textbox=gr.Textbox(placeholder="Hi ask me any questions."),
description='This tool provides information on STEM Careers. All information is sourced from [census.gov](https://www.census.gov/).'
)
with gr.Tab("Explore Now"): # ✅ Changed title
gr.Markdown("### Explore STEM Career Categories")
dropdown_explore = gr.Dropdown(
choices=[
"Highest Paying STEM Jobs",
"Most Flexible STEM Jobs",
"Most Creative STEM Jobs",
"Fastest Growing STEM Jobs",
"Low-Stress STEM Jobs"
],
label="Choose a Category"
)
output_explore = gr.Markdown()
dropdown_explore.change(fn=show_info, inputs=dropdown_explore, outputs=output_explore)
with gr.Tab("Resources"):
gr.Markdown("### Career-Specific Educational Resources")
dropdown_resources = gr.Dropdown(
choices=[
"AI/Machine Learning Engineer",
"Data Scientist",
"Cloud Solutions Architect",
"Cybersecurity Analyst",
"Statistician",
"Biomedical Engineer",
"Mechanical Engineer",
"Environmental Scientist",
"Operations Research Analyst",
"Mathematician",
"Chemical Engineer",
"Civil Engineer",
"Electrical Engineer",
"Software Developer",
"Pharmacist",
"Physicist",
"Astronomer",
"Geologist",
"Biochemist"
],
label="Choose a Career"
)
output_links = gr.HTML()
output_video = gr.HTML()
dropdown_resources.change(
fn=resource_block,
inputs=dropdown_resources,
outputs=[output_links, output_video]
)
chatbot.launch()