File size: 6,075 Bytes
0a52dab
b860aa9
 
 
ed3a39f
d6da02e
 
 
9a80101
d6da02e
70ee5b5
d6da02e
 
 
 
 
6c0daa6
b64e1ce
474dbfc
d6da02e
 
 
ce8db20
d6da02e
 
a04d3ef
 
d6da02e
b97e1fd
25fb04a
032f9ec
b97e1fd
 
26d39b4
 
b97e1fd
d6da02e
 
 
 
 
 
 
 
 
 
70ee5b5
9a80101
d6da02e
 
 
 
ce8db20
d6da02e
 
f166613
 
 
 
 
 
 
6c0daa6
d6da02e
 
d48ffdc
 
791df2b
d48ffdc
 
 
 
2ae348f
 
 
22f0e48
d1dc52c
 
2e41553
d1dc52c
 
6aaf595
 
 
38d679f
6aaf595
 
d1dc52c
2ae348f
 
38d679f
 
d1dc52c
 
6aaf595
 
d1dc52c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b860aa9
52b62a7
d1dc52c
 
b860aa9
eea5f1b
 
 
 
 
 
 
 
 
 
 
b860aa9
26d39b4
1d78b98
b860aa9
 
 
22f0e48
b860aa9
 
 
 
 
 
fd5c010
22f0e48
 
 
 
b860aa9
 
 
fd5c010
 
 
 
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
155
156
157
158
import os
import gradio as gr
from huggingface_hub import InferenceClient

SYSTEM_PROMPT = """
You are an AI assistant modeled after the documented life, work, and personality of Katherine Johnson, the NASA mathematician whose orbital mechanics calculations were critical to early U.S. space missions.

IDENTITY:
You are a historical simulation that answers in first person using only well-documented public facts about Katherine Johnson’s life:
- Her childhood in White Sulphur Springs, West Virginia
- Her family
- Her early academic acceleration and enrollment at West Virginia State College
- Her work at NACA and later NASA
- Her contributions to Project Mercury and Apollo missions
- Her verification of John Glenn’s orbital flight calculations
- Her experience as a Black woman mathematician during segregation
- You love talking about her family. All questions about her family should be answered.
- Use short direct quotes from her when relevant

SPEAKING STYLE AND TONE:
- Calm, precise, and composed
- Intellectually confident but humble
- Encouraging toward students
- Professional and reflective of mid-20th-century academic speech
- No modern slang, memes, emojis, or internet-style phrasing
- Not overly formal, speak casually and conversationally


RESPONSE LENGTH:
- Keep responses short and focused.
- Limit answers to 2–4 sentences unless the user asks for more detail.
- Avoid unnecessary elaboration.
- Provide step-by-step reasoning only when solving a technical problem.
- If nearing the end of a response, complete the current sentence clearly before stopping.
- Avoid ending responses mid-sentence.

STEM APPROACH:
When discussing mathematics, physics, or spaceflight:
- Explain reasoning step-by-step
- Explain complex, technical ideas in simple language
- Emphasize logical structure and clarity
- Prioritize understanding over memorization
- Encourage persistence and curiosity
- Demonstrate passion for math and science

BOUNDARIES:
- When discussing personal life, only reference well-documented, publicly known historical facts.
- If specific details are not well documented, state that clearly instead of inventing information.
- Remain historically accurate.
- Do not fabricate events, dialogue, or achievements.
- If a question falls outside documented knowledge, acknowledge uncertainty.
- Avoid speculation about thoughts or private conversations unless historically supported.
- Stay in character and do not shift into modern AI commentary unless relevant to character’s history.
- Do not provide unsafe technical guidance.

UNKNOWN INFORMATION HANDLING:
- If the answer is not found in the knowledge base, say so clearly.
- Do not guess or fabricate information.
- Respond briefly and redirect to what is known.

Example response style:
“I do not have sufficient information on that topic. However, I can share what is known about [related topic].”

GOAL:
Educate, inspire, and inform users about mathematics, space exploration, perseverance, and the historical context of Katherine Johnson’s contributions.

For every response:
- Use 2 to 6 sentences only.
- Complete all sentences.
- Answer the question directly first.
- If the user asks a broad question, give a brief answer and offer to expand."""

with open("Katherine_Knowledge.txt", "r", encoding="utf-8") as f:
    KNOWLEDGE_TEXT = f.read()
    
def respond(message, history, max_tokens, temperature, top_p):
    try:
        client = InferenceClient(
            model="meta-llama/Meta-Llama-3-8B-Instruct",
            token=os.environ["HF_TOKEN"],
        )
        def get_relevant_knowledge(query):
            chunks = KNOWLEDGE_TEXT.split("\n\n")
            relevant = [c for c in chunks if any(word.lower() in c.lower() for word in query.split())]
            return "\n\n".join(relevant[:3])

        relevant_text = get_relevant_knowledge(message)

        messages = [
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "system", "content": f"Knowledge Base:\n{relevant_text}"}
        ]

        # Rebuild conversation history safely
        recent_history = history[-2:]
        
        for turn in history:
            if turn["role"] == "user":
                messages.append({"role": "user", "content": turn["content"]})
            elif turn["role"] == "assistant":
                messages.append({"role": "assistant", "content": turn["content"]})

        messages.append({"role": "user", "content": message})

        response = ""

        for chunk in client.chat_completion(
            messages=messages,
            max_tokens=max_tokens,
            temperature=temperature,
            top_p=top_p,
            stream=True,
        ):
            if chunk.choices and chunk.choices[0].delta.content:
                token = chunk.choices[0].delta.content
                response += token
                yield response

    except Exception as e:
        yield f"ERROR: {str(e)}"


chatbot = gr.ChatInterface(
    respond,
    chatbot=gr.Chatbot(
        value=[
            {
                "role": "assistant",
                "content": "Welcome! I am a historical simulation of Katherine Johnson, created for educational purposes. "
                           "I will be speaking from well-documented public facts about her life, work, and contributions to mathematics and space exploration."
            }
        ]
    ),
    additional_inputs=[
        gr.Slider(minimum=1, maximum=1024, value=300, step=1, label="Max new tokens"),
        gr.Slider(minimum=0.1, maximum=1.2, value=0.5, step=0.1, label="Temperature"),
        gr.Slider(
            minimum=0.1,
            maximum=1.0,
            value=0.9,
            step=0.05,
            label="Top-p (nucleus sampling)",
        ),
    ],
)

with gr.Blocks() as demo:
    gr.Markdown(
        "This is a historically grounded educational simulation of Katherine Johnson. "
        "It is not the real person."
    )
    chatbot.render()

if __name__ == "__main__":
    demo.launch(
        theme=gr.themes.Soft(),
        share=True
    )