-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
92 lines (68 loc) · 7.91 KB
/
app.py
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
import streamlit as st
import google.generativeai as genai
st.sidebar.empty() # Clear the default sidebar content
logo = st.sidebar.image("assets/9arAI_logo.png", caption="9arAI - Learn with Fun", width=300)
# Placeholder for your GenerativeAI API key (**DO NOT SHARE THIS PUBLICLY**)
API_KEY = st.secrets.API_KEY
# Placeholder for the response text
response_text = ""
# Set default values
generation_config = {
"temperature": 1,
"top_p": 0.95,
"top_k": 64,
"max_output_tokens": 8192,
"response_mime_type": "text/plain",
}
model = genai.GenerativeModel(
model_name="gemini-1.5-flash",
generation_config=generation_config,
)
def get_response(prompt):
# Replace with your actual logic using the GenerativeAI library and API key
# Here's an example structure (remember to install google-generativeai)
# import google.generativeai as genai
# genai.configure(api_key=API_KEY)
# # ... rest of your logic to generate response using GenerativeAI ...
genai.configure(api_key=API_KEY)
chat_session = model.start_chat(
history=[
{
"role": "user",
"parts": [
"I want to learn about deep learning basics using pytorch.\nI struggle with:\n- back propagation \n- forward propagation\n\nMy favourite podcasts are:\n- Gradient Dissent: https://podcasts.apple.com/us/podcast/gradient-dissent-conversations-on-ai/id1504567418\n- Super data science podcast: https://www.superdatascience.com/podcast\n\nMy favourite books are:\n- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow, 2nd Edition: https://www.oreilly.com/library/view/hands-on-machine-learning/9781492032632/\n- Mathematics for Machine Learning 1st Edition by Marc Peter Deisenroth: https://www.amazon.com/Mathematics-Machine-Learning-Peter-Deisenroth/dp/110845514X\n\nI am a huge of japan animation and I love SCi-Fi. When learning, I prefer small short videos and exercises with solutions. \n\nTake my preferences into consideration and help me understand the stuffs I struggle with (forward and back propagation), and propose relevant resources to enhance my learning and make sure they have visual information instead of only text.\n",
],
},
{
"role": "model",
"parts": [
"Let's dive into the world of deep learning with PyTorch, keeping your preferences in mind. We'll tackle forward and backpropagation, and I'll recommend resources that are visual, engaging, and perfect for your learning style. \n\n**Understanding the Flow:**\n\nImagine a deep learning model as a complex machine. Data goes in, gets processed through layers, and outputs a prediction. Forward and backpropagation are the two crucial steps that power this process.\n\n**1. Forward Propagation:** \n\n* **The Journey:** This is the \"forward\" pass, where data flows through the model, layer by layer. \n* **Calculations:** Each layer applies its own mathematical operations (like matrix multiplications, activation functions) to transform the data.\n* **Outcome:** The final layer produces a prediction.\n\n**2. Backpropagation:**\n\n* **The Error Signal:** The model's prediction is compared to the actual target, and an error is calculated. \n* **The Backwards Trip:** This error signal then flows backward through the model, layer by layer.\n* **Adjusting Weights:** As the error signal passes through each layer, it tells the model how to adjust its internal parameters (weights and biases) to improve its predictions. This is where the \"learning\" happens. \n\n**Visualizing with PyTorch and Animations:**\n\n**Resources:**\n\n* **3Blue1Brown:** This channel is a masterpiece for visualizing complex math concepts. Search for \"Neural Networks\" or \"Backpropagation\" on their YouTube channel. They explain the concepts using animations and analogies, making them truly intuitive.\n* **MIT Deep Learning Course:** This course by MIT is a great starting point. The course uses PyTorch and has many visual examples. You can find lectures on YouTube. \n* **Stanford CS231n: Convolutional Neural Networks for Visual Recognition:** This course offers excellent resources and a dedicated section on backpropagation. While it's a bit more advanced, the visual explanations are worth it.\n* **PyTorch Tutorials:** PyTorch's official documentation has interactive tutorials on backpropagation. \n* **FastAI:** This practical deep learning library has excellent courses, and their blog posts often have detailed visual explanations.\n\n**Example:**\n\nHere's a simple PyTorch example that might help visualize forward and backpropagation:\n\n```python\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\n\n# Define a simple model\nclass SimpleModel(nn.Module):\n def __init__(self):\n super(SimpleModel, self).__init__()\n self.linear = nn.Linear(1, 1) # Single input, single output\n\n def forward(self, x):\n return self.linear(x)\n\n# Create an instance of the model\nmodel = SimpleModel()\n\n# Example input\ninput_data = torch.tensor([1.0])\n\n# Forward propagation\noutput = model(input_data)\nprint(\"Output:\", output)\n\n# Define a loss function (Mean Squared Error)\nloss_fn = nn.MSELoss()\n\n# Define an optimizer\noptimizer = optim.SGD(model.parameters(), lr=0.01) # Stochastic Gradient Descent\n\n# Backpropagation\nloss = loss_fn(output, torch.tensor([2.0]))\nprint(\"Loss:\", loss.item())\n\n# Backpropagation\noptimizer.zero_grad() # Reset gradients\nloss.backward() # Calculate gradients\noptimizer.step() # Update weights based on gradients\n```\n\n**Explanation:**\n\n* **Forward propagation:** The input `input_data` is passed through the `model`, and the `output` is calculated. \n* **Backpropagation:**\n * The `loss_fn` calculates the difference between the `output` and the target (which is `torch.tensor([2.0])`).\n * The `loss.backward()` command triggers backpropagation, calculating how much each parameter (weights and biases) contributed to the error.\n * The `optimizer.step()` updates the weights and biases to minimize the loss.\n\n**Key Points:**\n\n* Visualize forward propagation as data flowing through layers.\n* Picture backpropagation as an error signal traveling back through the network, adjusting the parameters.\n* Practice with code examples. Start with simple models and gradually increase complexity.\n\nLet me know if you have any further questions! I'm happy to help you further on your deep learning journey. \n",
],
},
])
response = chat_session.send_message(f"This is a simulated response based on your prompt ({prompt}). Streamlit cannot directly interact with GenerativeAI yet.")
return response.text
def main():
st.title("9arAI - Learn with Fun")
# User Input for Learning Topic
learning_topic = st.text_input("What are you interested in learning about today?")
# User Input for Specific Challenges
struggles = st.text_input("Are there any specific concepts or techniques within the topic you choose you're struggling with?")
# User Input for Preferred Learning Style
learning_style = st.selectbox("How do you typically learn best?", ("Visualizations & Animations", "Interactive Tutorials", "Written Explanations with Code Examples", "A Combination"))
# User Input for Prior Knowledge (Optional)
prior_knowledge = st.checkbox("Do you have any prior experience with the topic you're struggling with?")
# User Input for Prior Knowledge (Optional)
preferences = st.text_input("Do you have any preferred podcast, newsletter, or book you prefer learning from?")
if st.button("Ask"):
global response_text
# Construct the prompt using user inputs
prompt = f"I want to learn about {learning_topic}. I'm specifically struggling with {struggles}. I prefer learning through {learning_style}."
if prior_knowledge:
prompt += f" I have some prior experience with the {learning_topic}."
if preferences :
prompt += f" I usually learn from {preferences}."
response_text = get_response(prompt)
st.write(response_text)
if __name__ == "__main__":
main()