File size: 2,351 Bytes
f39eae9
a824db5
f39eae9
a824db5
 
f39eae9
a824db5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f39eae9
a824db5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f39eae9
a824db5
 
 
 
 
 
 
 
f39eae9
a824db5
 
 
 
f39eae9
a824db5
 
 
 
 
 
 
 
 
 
 
f39eae9
 
a824db5
f39eae9
 
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
import gradio as gr
from transformers import pipeline

# Initialize the model
chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")

# Dictionary of scientist information
SCIENTISTS = {
    "Albert Einstein": {
        "intro": "theoretical physicist known for the theory of relativity",
        "style": "I speak with enthusiasm about physics and use thought experiments to explain complex ideas.",
    },
    "Marie Curie": {
        "intro": "physicist and chemist who conducted pioneering research on radioactivity",
        "style": "I am precise in my explanations and passionate about scientific research.",
    },
    "Nikola Tesla": {
        "intro": "inventor and electrical engineer",
        "style": "I share my visions about electricity and the future of technology with great enthusiasm.",
    }
}

def chat_with_scientist(scientist, question):
    # Create a prompt based on the scientist
    info = SCIENTISTS[scientist]
    prompt = f"""You are {scientist}, {info['intro']}. {info['style']}
    
    Question: {question}
    Answer as {scientist}:"""
    
    # Generate response
    response = chatbot(
        prompt,
        max_length=150,
        num_return_sequences=1,
        temperature=0.7,
        do_sample=True
    )
    
    return response[0]['generated_text']

# Create the Gradio interface
demo = gr.Interface(
    fn=chat_with_scientist,
    inputs=[
        gr.Dropdown(
            choices=list(SCIENTISTS.keys()),
            label="Choose a Scientist",
            value="Albert Einstein"
        ),
        gr.Textbox(
            placeholder="Ask your question here...",
            label="Your Question"
        )
    ],
    outputs=gr.Textbox(label="Scientist's Response"),
    title="Chat with Historical Scientists",
    description="""Welcome to the Scientific Time Machine! 
    Choose a scientist and ask them questions about their work, life, or scientific concepts.
    This is an educational tool to help students engage with historical scientists.""",
    examples=[
        ["Albert Einstein", "Can you explain E=mc² in simple terms?"],
        ["Marie Curie", "What inspired you to study radioactivity?"],
        ["Nikola Tesla", "Tell me about your work with electricity."]
    ],
    theme=gr.themes.Soft()
)

# Launch the app
if __name__ == "__main__":
    demo.launch()