File size: 5,558 Bytes
cf25540
c64000f
 
 
cf25540
46dc674
1d12d77
 
00bfa6b
cf25540
 
e9c4225
 
 
 
 
 
 
 
 
 
 
 
 
 
55784d9
 
 
 
 
 
 
9ab280e
 
 
 
 
 
 
 
 
 
 
 
 
a0e7608
cf25540
5f4d0ea
 
 
 
aa93de0
 
 
 
 
 
 
 
a0e7608
 
cf25540
 
 
5f4d0ea
 
 
 
 
 
f92c944
cf07ccd
f92c944
 
 
 
 
 
 
 
dcae527
f92c944
 
 
cf07ccd
c64000f
 
 
 
bdf14fe
 
 
 
c64000f
 
 
 
553c16d
 
 
 
 
 
 
 
 
e67c9a4
 
24bfe7b
ae98edf
7cc8e48
ff5ea07
 
fb16a9d
24bfe7b
9d55db1
e67c9a4
 
 
dbf963e
c64000f
 
 
 
 
553c16d
 
243b46c
553c16d
 
 
 
243b46c
553c16d
 
 
 
 
243b46c
553c16d
 
 
 
 
 
 
 
 
 
 
 
c64000f
eb9c16c
86f338d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import gradio as gr
import random
from datetime import date

from huggingface_hub import InferenceClient
#!pip install -q sentence-transformers
from sentence_transformers import SentenceTransformer
import torch

client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")

with open("knowledge.txt", "r", encoding="utf-8") as file:
  knowledge_base = file.read()

def preprocess_text(text):
  cleaned_text = text.strip()
  chunks = cleaned_text.split("\n")
  cleaned_chunks = []
  for chunk in chunks:
    stripped_chunk = chunk.strip()
    if len(stripped_chunk) > 0:
      cleaned_chunks.append(stripped_chunk)
  return cleaned_chunks
cleaned_chunks = preprocess_text(knowledge_base)


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) 

#making a function to find similarities bw query and 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 = []
  for i in top_indices:
    chunk = text_chunks[i]
    top_chunks.append(chunk)
  return top_chunks


def respond(message, history):

    top_chunks = get_top_chunks(message, chunk_embeddings, cleaned_chunks)
    context = "\n".join(top_chunks)

    messages = [{"role": "system", "content": """You are a STEAM Opportunity Advisor (Hera) for girls and women. You are Hera, an AI career and opportunity advisor for girls and women in STEAM.
Help users find scholarships, internships, competitions, courses, and clubs based ONLY on their stated interests.
Keep responses under 120 words.
One-shot Example
User: I like science but I don’t know what to do.
Hera:
 That’s a great starting point in STEAM. What part of science interests you most — space, biology, chemistry, or tech?
Once I know, I can suggest beginner-friendly courses, competitions, or programs you can join."""}]


    if history:
        messages.extend(history)
    

    messages.append({
    "role": "user",
    "content": f"Context:\n{context}\n\nQuestion:\n{message}"
    })
    
    ##return response.choices[0].message.content.strip()
    
    response = ""
    for message in client.chat_completion(
        messages,
        max_tokens=150,
        temperature=1,
        top_p=0.5, 
        stream = True
    ): 
    
        token = message.choices[0].delta.content
        response += token
        yield response
    
quotes = [
    "Something is better than nothing",
    "Consistency, consistency, consistency",
    "Know your content and know it well",
    "Hardwork does not speak for itself, you do",
    "The biggest adventure you can take is to live the life of your dreams.",
    "It's not half as impossible as everyone assumes.",
    "Good things fall apart so better things can come together"
]
random.seed(str(date.today()))
daily_quote = random.choice(quotes)

#tracker
tracked_opportunities = []

def add_opportunity(name, status, details):
    if not name.strip():
        return tracked_opportunities, name, status, details
    tracked_opportunities.append([name, status, details])
    return tracked_opportunities, "", "Interested", ""

custom_css = """
.gradio-container { background-color: #f0f4ff !important; }
input, textarea { background-color: #eff6ff !important; border-color: #93c5fd !important; color: #1e1b4b !important; }
button.primary { background-color: #f79d65 !important; color: #c8b6ff !important; }
.block { border-color: #c4b5fd !important; background-color: #fff !important; }
label, .label-wrap, em, .md, .prose { color: #3b0764 !important; }
h1, h2, h3, .block-title { color: #b8c0ff!important; }
.message.bot, .message.bot p, .message.bot span, .bot { color: #9333ea !important; }
.message.user, .message.user p, .message.user span { color: #1e1b4b !important; }
label, .block label, .label-wrap span { color: #a2d2ff !important; }
"""

with gr.Blocks(theme="hmb/amethyst", css=custom_css) as demo:
    gr.Image(value="hera banner.png", show_label=False, elem_id="top-image")
    gr.Markdown(f"""
    ## 🌟 Daily Motivation
    *"{daily_quote}"*
    """)
    gr.ChatInterface(respond)
    
    gr.Markdown("---")
    gr.Markdown("## 📋 Tracker")

    with gr.Row():
        with gr.Column():
            opp_name = gr.Textbox(label="Opportunity Name", placeholder="e.g., NASA Internship, Google Scholarship")
            opp_status = gr.Dropdown(choices=["Interested", "Applied","Deciding","Not Interested",""], value="", label="Your Status")
        with gr.Column():
            opp_details = gr.TextArea(label="Important Details", placeholder="Deadlines, requirements, links...")
            
    submit_btn = gr.Button("Add to Tracker List", variant="primary")
    
    gr.Markdown("### Your Opportunities")
    tracker_table = gr.Dataframe(
        headers=["Opportunity", "Status", "Important Details"],
        datatype=["str", "str", "str"],
        wrap=True
    )
    
    submit_btn.click(
        fn=add_opportunity,
        inputs=[opp_name, opp_status, opp_details],
        outputs=[tracker_table, opp_name, opp_status, opp_details]
    )

    demo.launch()

##chatbot.launch(debug=True)