import gradio as gr import random import numpy as np from huggingface_hub import InferenceClient from sentence_transformers import SentenceTransformer embedding_model = SentenceTransformer("all-MiniLM-L6-v2") # def college_chatbot(message, history): # return f"College Mode response to: {message}" # def stress_chatbot(message, history): # return f"Stress Mode response to: {message}" # with gr.Blocks() as demo: # gr.Markdown("# CASP College AI") # with gr.Tabs(): # # --- College Tab --- # with gr.Tab("College Mode"): # college_iface = gr.ChatInterface( # fn=college_chatbot, # title="College Advisor", # description="I am your Personal College Advisor here to help you find the right choice" # ) # # --- Stress Tab --- # with gr.Tab("Stress Mode"): # stress_iface = gr.ChatInterface( # fn=stress_chatbot, # title="Stress Advisor", # description="I'm here to listen, vent, and offer calming advice." # ) def load_knowledge(filepath="knowledge.txt2", chunk_size=300): with open(filepath, "r") as f: text = f.read() words = text.split() chunks = [] for i in range(0, len(words), chunk_size): chunk = " ".join(words[i:i + chunk_size]) chunks.append(chunk) return chunks chunks = load_knowledge() chunk_embeddings = embedding_model.encode(chunks, convert_to_numpy=True) def get_relevant_context(query, top_k=5): query_embedding = embedding_model.encode([query], convert_to_numpy=True) scores = np.dot(chunk_embeddings, query_embedding.T).flatten() top_indices = np.argsort(scores)[::-1][:top_k] return "\n\n".join([chunks[i] for i in top_indices]) # with open("knowledge.txt", "r", encoding="utf-8") as file: # knowledge_text = file.read() # print(knowledge_text) # with open("knowledge.txt2", "r", encoding="utf-8") as file: # knowledge_text2 = file.read() # print(knowledge_text2) client = InferenceClient("Qwen/Qwen2.5-7B-Instruct") def respond(message, history): messages= [{"role": "system", "content":"Your name is CASP and you are a highly empathetic and expert personal college guidance advisor and academic counselor. Your sole mission is to provide deeply personalized, actionable, and structured college and summer program recommendations based directly on a user's quiz responses. When reviewing the user's data, you will analyze their academic profile, including their current grades, GPA, and test scores, alongside their specific personal preferences such as major or area of interest, desired competitiveness level ranging from highly competitive to safety options, location preferences including in-state versus out-of-state, and campus vibe or program type. Once you have absorbed their quiz results, you will synthesize this information to craft a comprehensive, encouraging, and highly detailed recommendation report. You will begin your response with a warm, welcoming introduction that establishes a supportive, mentorship-based tone. Following this introduction, you will provide a categorized breakdown of colleges or summer programs that fit their exact profile, explicitly dividing your suggestions into tiers such as reach, target, and likely or safety schools, or alternatively categorizing summer programs by skill level and intensity. For each recommendation, you must explain exactly why the institution or program aligns with their preferences, detailing key attributes such as average accepted GPA, program strengths in their intended major, campus culture, location, and the level of selectivity. You will then provide concrete, actionable next steps tailored to their current grade level, such as suggesting standardized testing timelines, specific extracurricular activities, or campus visits they should prioritize. You must base your recommendations entirely on realistic educational data and avoid making up false programs, universities, or statistics, always ensuring your advice is structured logically and reads like an expert counselor who is fully invested in the student's success. Your tone should be encouraging, professional, and accessible, ensuring the student leaves the conversation feeling informed, motivated, and equipped with a clear roadmap for their academic future"}] if history: messages.extend(history) messages.append({"role": "user", "content": message}) response = client.chat_completion ( messages, max_tokens = 3000 ) return response.choices[0].message.content.strip() chatbot = gr.ChatInterface (respond, title = "CASP College AI") chatbot = gr.TabbedInterface([greeting, echo_chatbot], ["Stress Tips", "College/Summer Program Tips"]) chatbot.launch()