Spaces:
Sleeping
Sleeping
Download app.py from Stemchatbot/STEM_Luminary_Chatbot: direct link, hf CLI and curl.
- Browser
- Download file 6.08 kB
-
https://huggingface.co/spaces/Stemchatbot/STEM_Luminary_Chatbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/Stemchatbot/STEM_Luminary_Chatbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Stemchatbot/STEM_Luminary_Chatbot/resolve/main/app.py
6.08 kB
| 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 | |
| ) |