ahmad-fakhar commited on
Commit
54632a4
·
verified ·
1 Parent(s): 872c3d9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +179 -0
app.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from groq import Groq
4
+
5
+ # Set the Groq API key
6
+ api_key = st.secrets["GROQ_API_KEY"]
7
+
8
+ # Initialize the Groq client using the API key
9
+ client = Groq(api_key=api_key)
10
+
11
+ # Define the LLaMA model to be used
12
+ MODEL_NAME = "llama3-8b-8192"
13
+
14
+ # Function to call Groq API
15
+ def call_groq_api(prompt):
16
+ try:
17
+ chat_completion = client.chat.completions.create(
18
+ messages=[{"role": "user", "content": prompt}],
19
+ model=MODEL_NAME
20
+ )
21
+ return chat_completion.choices[0].message.content
22
+ except Exception as e:
23
+ return f"Error: {str(e)}"
24
+
25
+ # Define functions for each tool
26
+ def personalized_learning_assistant(topic):
27
+ examples = [
28
+ "Explain quantum mechanics with a real-world example.",
29
+ "Describe general relativity and its significance.",
30
+ "Provide a simple explanation of machine learning and its applications."
31
+ ]
32
+ 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."
33
+ return call_groq_api(prompt)
34
+
35
+ def ai_coding_mentor(code_snippet):
36
+ examples = [
37
+ "Review this code snippet for optimization opportunities:\n\nCode: 'for i in range(10): print(i)'\nSuggestion: Use list comprehension for more efficient code.",
38
+ "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."
39
+ ]
40
+ 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."
41
+ return call_groq_api(prompt)
42
+
43
+ def smart_document_summarizer(document_text):
44
+ examples = [
45
+ "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.'",
46
+ "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.'"
47
+ ]
48
+ 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."
49
+ return call_groq_api(prompt)
50
+
51
+ def interactive_study_planner(exam_schedule):
52
+ examples = [
53
+ "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.'",
54
+ "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.'"
55
+ ]
56
+ 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."
57
+ return call_groq_api(prompt)
58
+
59
+ def real_time_qa_support(question):
60
+ examples = [
61
+ "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.'",
62
+ "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.'"
63
+ ]
64
+ 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."
65
+ return call_groq_api(prompt)
66
+
67
+ def mental_health_check_in(feelings):
68
+ examples = [
69
+ "Offer advice for managing exam stress:\n\nFeeling: 'Stressed about upcoming exams'\nAdvice: 'Develop a study schedule, take regular breaks, and practice relaxation techniques.'",
70
+ "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.'"
71
+ ]
72
+ 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."
73
+ return call_groq_api(prompt)
74
+
75
+ # Define Streamlit app
76
+ st.set_page_config(page_title="EduNexus", page_icon=":book:", layout="wide")
77
+
78
+ # Add custom styling using Streamlit
79
+ st.markdown("""
80
+ <style>
81
+ .css-1o7k8tt {
82
+ background-color: #282c34;
83
+ color: #ffffff;
84
+ }
85
+ .css-1o7k8tt h1 {
86
+ color: #61dafb;
87
+ }
88
+ .stButton {
89
+ background-color: #61dafb;
90
+ color: #000000;
91
+ border-radius: 12px;
92
+ padding: 10px 20px;
93
+ font-size: 16px;
94
+ }
95
+ .stButton:hover {
96
+ background-color: #4fa3d1;
97
+ }
98
+ .stTextInput, .stTextArea {
99
+ border: 1px solid #61dafb;
100
+ border-radius: 8px;
101
+ }
102
+ </style>
103
+ """, unsafe_allow_html=True)
104
+
105
+ # Define function to clear all inputs
106
+ def clear_chat():
107
+ st.session_state['personalized_learning_assistant'] = ""
108
+ st.session_state['ai_coding_mentor'] = ""
109
+ st.session_state['smart_document_summarizer'] = ""
110
+ st.session_state['interactive_study_planner'] = ""
111
+ st.session_state['real_time_qa_support'] = ""
112
+ st.session_state['mental_health_check_in'] = ""
113
+
114
+ # Add Clear Chat button
115
+ if st.button("Clear All", key="clear_button"):
116
+ clear_chat()
117
+
118
+ # Navigation sidebar
119
+ st.sidebar.title("EduNexus Tools")
120
+ selected_tool = st.sidebar.radio(
121
+ "Select a tool",
122
+ ("Personalized Learning Assistant", "AI Coding Mentor", "Smart Document Summarizer",
123
+ "Interactive Study Planner", "Real-Time Q&A Support", "Mental Health Check-In")
124
+ )
125
+
126
+ # Display tool based on selection
127
+ if selected_tool == "Personalized Learning Assistant":
128
+ st.header("Personalized Learning Assistant")
129
+ with st.form(key="learning_form"):
130
+ topic_input = st.text_input("Enter a topic you want to learn about", placeholder="e.g., Quantum Mechanics")
131
+ submit_button = st.form_submit_button("Get Explanation")
132
+ if submit_button:
133
+ explanation = personalized_learning_assistant(topic_input)
134
+ st.write(explanation)
135
+
136
+ elif selected_tool == "AI Coding Mentor":
137
+ st.header("AI Coding Mentor")
138
+ with st.form(key="coding_form"):
139
+ code_input = st.text_area("Enter your code snippet", placeholder="e.g., def add(a, b): return a + b")
140
+ submit_button = st.form_submit_button("Review Code")
141
+ if submit_button:
142
+ review = ai_coding_mentor(code_input)
143
+ st.write(review)
144
+
145
+ elif selected_tool == "Smart Document Summarizer":
146
+ st.header("Smart Document Summarizer")
147
+ with st.form(key="summarizer_form"):
148
+ document_input = st.text_area("Enter the text you want to summarize", placeholder="Paste document text here...")
149
+ submit_button = st.form_submit_button("Summarize Document")
150
+ if submit_button:
151
+ summary = smart_document_summarizer(document_input)
152
+ st.write(summary)
153
+
154
+ elif selected_tool == "Interactive Study Planner":
155
+ st.header("Interactive Study Planner")
156
+ with st.form(key="planner_form"):
157
+ schedule_input = st.text_area("Enter your exam schedule", placeholder="e.g., 3 exams in 1 week")
158
+ submit_button = st.form_submit_button("Generate Study Plan")
159
+ if submit_button:
160
+ study_plan = interactive_study_planner(schedule_input)
161
+ st.write(study_plan)
162
+
163
+ elif selected_tool == "Real-Time Q&A Support":
164
+ st.header("Real-Time Q&A Support")
165
+ with st.form(key="qa_form"):
166
+ question_input = st.text_input("Ask any academic question", placeholder="e.g., What is Newton's second law?")
167
+ submit_button = st.form_submit_button("Get Answer")
168
+ if submit_button:
169
+ answer = real_time_qa_support(question_input)
170
+ st.write(answer)
171
+
172
+ elif selected_tool == "Mental Health Check-In":
173
+ st.header("Mental Health Check-In")
174
+ with st.form(key="checkin_form"):
175
+ feelings_input = st.text_area("How are you feeling?", placeholder="e.g., Stressed about exams")
176
+ submit_button = st.form_submit_button("Check In")
177
+ if submit_button:
178
+ advice = mental_health_check_in(feelings_input)
179
+ st.write(advice)