ibrahim313 commited on
Commit
92e3434
·
verified ·
1 Parent(s): 27633ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -125
app.py CHANGED
@@ -7,6 +7,9 @@ os.environ["GROQ_API_KEY"] = "gsk_BYXg06vIXpWdFjwDMLnFWGdyb3FYjlovjvzUzo5jtu5A1I
7
  # Initialize Groq client
8
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
9
 
 
 
 
10
  # Define the LLaMA model to be used
11
  MODEL_NAME = "llama3-8b-8192"
12
 
@@ -24,107 +27,83 @@ def call_groq_api(prompt):
24
  # Define functions for each tool
25
  def personalized_learning_assistant(topic):
26
  examples = [
27
- "Explain quantum mechanics. Example: Quantum mechanics is the study of particles at the atomic level.",
28
- "Explain general relativity. Example: General relativity describes gravity as a curvature in space-time.",
29
- "Explain machine learning. Example: Machine learning involves algorithms that improve through experience."
30
  ]
31
- prompt = f"Here are some examples of explanations:\n\n{examples}\n\nNow, explain the topic: {topic}"
32
  return call_groq_api(prompt)
33
 
34
  def ai_coding_mentor(code_snippet):
35
  examples = [
36
- "Review the following code snippet:\n\nCode: 'for i in range(10): print(i)'\nSuggestion: Use list comprehension for cleaner code.",
37
- "Review this code:\n\nCode: 'def add(a, b): return a + b'\nSuggestion: Add type hints for better readability."
38
  ]
39
- prompt = f"Here are some examples of code reviews:\n\n{examples}\n\nReview the following code snippet:\n{code_snippet}"
40
  return call_groq_api(prompt)
41
 
42
  def smart_document_summarizer(document_text):
43
  examples = [
44
- "Summarize the following text:\n\nText: 'Quantum computing is a rapidly evolving field with potential to revolutionize technology.'\nSummary: 'Quantum computing could transform technology.'",
45
- "Summarize this passage:\n\nText: 'The global climate change crisis necessitates urgent action to reduce carbon emissions.'\nSummary: 'Immediate action is needed to tackle climate change.'"
46
  ]
47
- prompt = f"Here are some examples of summaries:\n\n{examples}\n\nSummarize this document:\n{document_text}"
48
  return call_groq_api(prompt)
49
 
50
  def interactive_study_planner(exam_schedule):
51
  examples = [
52
- "Create a study plan based on this schedule:\n\nSchedule: '3 exams in a week'\nPlan: 'Study 2 hours per subject each day before the exam.'",
53
- "Generate a study plan for:\n\nSchedule: 'Exams in 2 weeks'\nPlan: 'Focus on subjects with more weight and review daily.'"
54
  ]
55
- prompt = f"Here are some examples of study plans:\n\n{examples}\n\nCreate a study plan for the following schedule:\n{exam_schedule}"
56
  return call_groq_api(prompt)
57
 
58
  def real_time_qa_support(question):
59
  examples = [
60
- "Answer this question:\n\nQuestion: 'What is Newton's second law of motion?'\nAnswer: 'Newton's second law states that force equals mass times acceleration (F=ma).'",
61
- "Provide an explanation for:\n\nQuestion: 'What is photosynthesis?'\nAnswer: 'Photosynthesis is the process by which plants convert light energy into chemical energy.'"
62
  ]
63
- prompt = f"Here are some examples of Q&A responses:\n\n{examples}\n\nAnswer the following question:\n{question}"
64
  return call_groq_api(prompt)
65
 
66
  def mental_health_check_in(feelings):
67
  examples = [
68
- "Provide advice for:\n\nFeeling: 'Stressed about exams'\nAdvice: 'Take breaks, get enough sleep, and manage your study time effectively.'",
69
- "Offer support for:\n\nFeeling: 'Feeling overwhelmed'\nAdvice: 'Consider relaxation techniques and seek support from friends or a counselor.'"
70
  ]
71
- prompt = f"Here are some examples of mental health advice:\n\n{examples}\n\nProvide advice for:\n{feelings}"
72
  return call_groq_api(prompt)
73
 
74
  # Define Streamlit app
75
  st.set_page_config(page_title="EduNexus", page_icon=":book:", layout="wide")
76
 
77
- # Add custom CSS for neon and animated design
78
  st.markdown("""
79
  <style>
80
- body {
81
- background: white;
82
- color: #e0e0e0;
83
- font-family: 'Arial', sans-serif;
84
- overflow-x: hidden;
85
  }
86
- .neon-text {
87
- font-size: 32px;
88
- color: #ff0081;
89
- text-shadow: ;
90
- margin-bottom: 20px;
91
- }
92
- .neon-border {
93
- border: 2px solid #ff0081;
94
- border-radius: 8px;
95
- padding: 15px;
96
- margin-bottom: 20px;
97
- box-shadow: 0 0 15px #ff0081;
98
  }
99
- .animated-button {
100
- animation: pulse 2s infinite;
101
- background-color: #ff0081;
102
- color: #000;
103
- border: none;
104
- border-radius: 8px;
105
  padding: 10px 20px;
106
  font-size: 16px;
107
- cursor: pointer;
108
- margin: 10px 0;
109
- }
110
- .animated-button:hover {
111
- background-color: #cc0077;
112
  }
113
- .input-container {
114
- margin-bottom: 20px;
115
  }
116
- @keyframes pulse {
117
- 0% { transform: scale(1); }
118
- 50% { transform: scale(1.05); }
119
- 100% { transform: scale(1); }
120
  }
121
  </style>
122
  """, unsafe_allow_html=True)
123
 
124
- # Title and introduction
125
- st.title("EduNexus: The Ultimate AI-Powered Student Companion")
126
- st.markdown("<div class='neon-text'>Welcome to EduNexus! Choose a tool below to get started.</div>", unsafe_allow_html=True)
127
-
128
  # Define function to clear all inputs
129
  def clear_chat():
130
  st.session_state['personalized_learning_assistant'] = ""
@@ -134,72 +113,69 @@ def clear_chat():
134
  st.session_state['real_time_qa_support'] = ""
135
  st.session_state['mental_health_check_in'] = ""
136
 
137
- # Add Clear Chat button using HTML
138
- if st.button("Clear All", key="clear_button", help="Click to clear all inputs"):
139
  clear_chat()
140
 
141
- # Personalized Learning Assistant
142
- st.header("Personalized Learning Assistant")
143
- with st.form(key="learning_form"):
144
- topic_input = st.text_input("Enter a topic you want to learn about", key="topic_input", placeholder="e.g., Quantum Mechanics")
145
- submit_button = st.form_submit_button("Generate Learning Material")
146
- if submit_button:
147
- if topic_input:
148
- st.write(personalized_learning_assistant(topic_input))
149
- else:
150
- st.write("Please enter a topic.")
151
-
152
- # AI Coding Mentor
153
- st.header("AI Coding Mentor")
154
- with st.form(key="coding_form"):
155
- code_input = st.text_area("Paste your code snippet", key="code_input", placeholder="e.g., for i in range(10): print(i)")
156
- submit_button = st.form_submit_button("Get Coding Assistance")
157
- if submit_button:
158
- if code_input:
159
- st.write(ai_coding_mentor(code_input))
160
- else:
161
- st.write("Please paste your code snippet.")
162
-
163
- # Smart Document Summarizer
164
- st.header("Smart Document Summarizer")
165
- with st.form(key="summary_form"):
166
- doc_input = st.text_area("Paste the text of the document", key="doc_input", placeholder="e.g., Quantum computing is...")
167
- submit_button = st.form_submit_button("Summarize Document")
168
- if submit_button:
169
- if doc_input:
170
- st.write(smart_document_summarizer(doc_input))
171
- else:
172
- st.write("Please paste the document text.")
173
-
174
- # Interactive Study Planner
175
- st.header("Interactive Study Planner")
176
- with st.form(key="planner_form"):
177
- schedule_input = st.text_area("Enter your exam schedule", key="schedule_input", placeholder="e.g., 3 exams in 1 week")
178
- submit_button = st.form_submit_button("Generate Study Plan")
179
- if submit_button:
180
- if schedule_input:
181
- st.write(interactive_study_planner(schedule_input))
182
- else:
183
- st.write("Please enter your exam schedule.")
184
-
185
- # Real-Time Q&A Support
186
- st.header("Real-Time Q&A Support")
187
- with st.form(key="qa_form"):
188
- question_input = st.text_input("Ask any academic question", key="question_input", placeholder="e.g., What is Newton's second law?")
189
- submit_button = st.form_submit_button("Get Answer")
190
- if submit_button:
191
- if question_input:
192
- st.write(real_time_qa_support(question_input))
193
- else:
194
- st.write("Please ask a question.")
195
-
196
- # Mental Health Check-In
197
- st.header("Mental Health Check-In")
198
- with st.form(key="checkin_form"):
199
- feelings_input = st.text_area("How are you feeling?", key="feelings_input", placeholder="e.g., Stressed about exams")
200
- submit_button = st.form_submit_button("Check In")
201
- if submit_button:
202
- if feelings_input:
203
- st.write(mental_health_check_in(feelings_input))
204
- else:
205
- st.write("Please describe your feelings.")
 
7
  # Initialize Groq client
8
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
9
 
10
+
11
+
12
+
13
  # Define the LLaMA model to be used
14
  MODEL_NAME = "llama3-8b-8192"
15
 
 
27
  # Define functions for each tool
28
  def personalized_learning_assistant(topic):
29
  examples = [
30
+ "Explain quantum mechanics with a real-world example.",
31
+ "Describe general relativity and its significance.",
32
+ "Provide a simple explanation of machine learning and its applications."
33
  ]
34
+ prompt = f"Here are some example explanations:\n\n{examples}\n\nNow, explain the topic: {topic}. Provide a detailed, yet simple explanation with a practical example."
35
  return call_groq_api(prompt)
36
 
37
  def ai_coding_mentor(code_snippet):
38
  examples = [
39
+ "Review this code snippet for optimization opportunities:\n\nCode: 'for i in range(10): print(i)'\nSuggestion: Use list comprehension for more efficient code.",
40
+ "Analyze this code snippet for best practices:\n\nCode: 'def add(a, b): return a + b'\nSuggestion: Include type hints to improve readability and maintainability."
41
  ]
42
+ prompt = f"Here are some code review examples:\n\n{examples}\n\nReview the following code snippet and provide suggestions for improvement:\n{code_snippet}. Include any potential issues or improvements."
43
  return call_groq_api(prompt)
44
 
45
  def smart_document_summarizer(document_text):
46
  examples = [
47
+ "Summarize this text:\n\nText: 'Quantum computing represents a revolutionary approach to computing that leverages quantum mechanics.'\nSummary: 'Quantum computing uses quantum mechanics to advance computing technology.'",
48
+ "Create a summary for this passage:\n\nText: 'The rise of electric vehicles is a major step towards reducing global carbon emissions and combating climate change.'\nSummary: 'Electric vehicles help reduce carbon emissions and fight climate change.'"
49
  ]
50
+ prompt = f"Here are some document summarization examples:\n\n{examples}\n\nSummarize the following document text concisely:\n{document_text}. Focus on capturing the main points clearly."
51
  return call_groq_api(prompt)
52
 
53
  def interactive_study_planner(exam_schedule):
54
  examples = [
55
+ "Generate a study plan for a schedule with multiple exams in a week:\n\nSchedule: '3 exams in one week'\nPlan: 'Allocate 2 hours per subject each day, with review sessions on weekends.'",
56
+ "Create a study plan for preparing for exams over a period of 2 weeks:\n\nSchedule: 'Exams in 2 weeks'\nPlan: 'Prioritize subjects based on difficulty and importance, with daily reviews and mock tests.'"
57
  ]
58
+ prompt = f"Here are some study planning examples:\n\n{examples}\n\nCreate a tailored study plan based on the following schedule:\n{exam_schedule}. Include daily study goals and break times."
59
  return call_groq_api(prompt)
60
 
61
  def real_time_qa_support(question):
62
  examples = [
63
+ "Provide an answer to this question:\n\nQuestion: 'What is Newton's third law of motion?'\nAnswer: 'Newton's third law states that for every action, there is an equal and opposite reaction.'",
64
+ "Explain this concept:\n\nQuestion: 'What is the principle of conservation of energy?'\nAnswer: 'The principle of conservation of energy states that energy cannot be created or destroyed, only transformed from one form to another.'"
65
  ]
66
+ prompt = f"Here are some examples of answers to academic questions:\n\n{examples}\n\nAnswer the following question:\n{question}. Provide a clear and comprehensive explanation."
67
  return call_groq_api(prompt)
68
 
69
  def mental_health_check_in(feelings):
70
  examples = [
71
+ "Offer advice for managing exam stress:\n\nFeeling: 'Stressed about upcoming exams'\nAdvice: 'Develop a study schedule, take regular breaks, and practice relaxation techniques.'",
72
+ "Provide support for feeling overwhelmed:\n\nFeeling: 'Feeling overwhelmed with coursework'\nAdvice: 'Break tasks into smaller, manageable parts and seek support from peers or a counselor.'"
73
  ]
74
+ prompt = f"Here are some examples of mental health advice:\n\n{examples}\n\nProvide advice based on the following feeling:\n{feelings}. Offer practical suggestions for improving well-being."
75
  return call_groq_api(prompt)
76
 
77
  # Define Streamlit app
78
  st.set_page_config(page_title="EduNexus", page_icon=":book:", layout="wide")
79
 
80
+ # Add custom styling using Streamlit
81
  st.markdown("""
82
  <style>
83
+ .css-1o7k8tt {
84
+ background-color: #282c34;
85
+ color: #ffffff;
 
 
86
  }
87
+ .css-1o7k8tt h1 {
88
+ color: #61dafb;
 
 
 
 
 
 
 
 
 
 
89
  }
90
+ .stButton {
91
+ background-color: #61dafb;
92
+ color: #000000;
93
+ border-radius: 12px;
 
 
94
  padding: 10px 20px;
95
  font-size: 16px;
 
 
 
 
 
96
  }
97
+ .stButton:hover {
98
+ background-color: #4fa3d1;
99
  }
100
+ .stTextInput, .stTextArea {
101
+ border: 1px solid #61dafb;
102
+ border-radius: 8px;
 
103
  }
104
  </style>
105
  """, unsafe_allow_html=True)
106
 
 
 
 
 
107
  # Define function to clear all inputs
108
  def clear_chat():
109
  st.session_state['personalized_learning_assistant'] = ""
 
113
  st.session_state['real_time_qa_support'] = ""
114
  st.session_state['mental_health_check_in'] = ""
115
 
116
+ # Add Clear Chat button
117
+ if st.button("Clear All", key="clear_button"):
118
  clear_chat()
119
 
120
+ # Navigation sidebar
121
+ st.sidebar.title("EduNexus Tools")
122
+ selected_tool = st.sidebar.radio(
123
+ "Select a tool",
124
+ ("Personalized Learning Assistant", "AI Coding Mentor", "Smart Document Summarizer",
125
+ "Interactive Study Planner", "Real-Time Q&A Support", "Mental Health Check-In")
126
+ )
127
+
128
+ # Display tool based on selection
129
+ if selected_tool == "Personalized Learning Assistant":
130
+ st.header("Personalized Learning Assistant")
131
+ with st.form(key="learning_form"):
132
+ topic_input = st.text_input("Enter a topic you want to learn about", placeholder="e.g., Quantum Mechanics")
133
+ submit_button = st.form_submit_button("Get Explanation")
134
+ if submit_button:
135
+ explanation = personalized_learning_assistant(topic_input)
136
+ st.write(explanation)
137
+
138
+ elif selected_tool == "AI Coding Mentor":
139
+ st.header("AI Coding Mentor")
140
+ with st.form(key="coding_form"):
141
+ code_input = st.text_area("Enter your code snippet", placeholder="e.g., def add(a, b): return a + b")
142
+ submit_button = st.form_submit_button("Review Code")
143
+ if submit_button:
144
+ review = ai_coding_mentor(code_input)
145
+ st.write(review)
146
+
147
+ elif selected_tool == "Smart Document Summarizer":
148
+ st.header("Smart Document Summarizer")
149
+ with st.form(key="summarizer_form"):
150
+ document_input = st.text_area("Enter the text you want to summarize", placeholder="Paste document text here...")
151
+ submit_button = st.form_submit_button("Summarize Document")
152
+ if submit_button:
153
+ summary = smart_document_summarizer(document_input)
154
+ st.write(summary)
155
+
156
+ elif selected_tool == "Interactive Study Planner":
157
+ st.header("Interactive Study Planner")
158
+ with st.form(key="planner_form"):
159
+ schedule_input = st.text_area("Enter your exam schedule", placeholder="e.g., 3 exams in 1 week")
160
+ submit_button = st.form_submit_button("Generate Study Plan")
161
+ if submit_button:
162
+ study_plan = interactive_study_planner(schedule_input)
163
+ st.write(study_plan)
164
+
165
+ elif selected_tool == "Real-Time Q&A Support":
166
+ st.header("Real-Time Q&A Support")
167
+ with st.form(key="qa_form"):
168
+ question_input = st.text_input("Ask any academic question", placeholder="e.g., What is Newton's second law?")
169
+ submit_button = st.form_submit_button("Get Answer")
170
+ if submit_button:
171
+ answer = real_time_qa_support(question_input)
172
+ st.write(answer)
173
+
174
+ elif selected_tool == "Mental Health Check-In":
175
+ st.header("Mental Health Check-In")
176
+ with st.form(key="checkin_form"):
177
+ feelings_input = st.text_area("How are you feeling?", placeholder="e.g., Stressed about exams")
178
+ submit_button = st.form_submit_button("Check In")
179
+ if submit_button:
180
+ advice = mental_health_check_in(feelings_input)
181
+ st.write(advice)