ibrahim313 commited on
Commit
63edf19
·
verified ·
1 Parent(s): adbaac2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -0
app.py CHANGED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import necessary libraries
2
+ import os
3
+ import streamlit as st
4
+ from groq import Groq
5
+
6
+ # Set the Groq API key
7
+ os.environ["GROQ_API_KEY"] = "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"
8
+
9
+ # Initialize Groq client
10
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
11
+
12
+ # Define the LLaMA model to be used
13
+ MODEL_NAME = "llama3-8b-8192"
14
+
15
+ # Function to call Groq API
16
+ def call_groq_api(prompt):
17
+ try:
18
+ chat_completion = client.chat.completions.create(
19
+ messages=[{"role": "user", "content": prompt}],
20
+ model=MODEL_NAME
21
+ )
22
+ return chat_completion.choices[0].message.content
23
+ except Exception as e:
24
+ return f"Error: {str(e)}"
25
+
26
+ # Define functions with few-shot prompting
27
+
28
+ def personalized_learning_assistant(topic):
29
+ examples = [
30
+ "Explain quantum mechanics. Example: Quantum mechanics is the study of particles at the atomic level.",
31
+ "Explain general relativity. Example: General relativity describes gravity as a curvature in space-time.",
32
+ "Explain machine learning. Example: Machine learning involves algorithms that improve through experience."
33
+ ]
34
+ prompt = f"Here are some examples of explanations:\n\n{examples}\n\nNow, explain the topic: {topic}"
35
+ return call_groq_api(prompt)
36
+
37
+ def ai_coding_mentor(code_snippet):
38
+ examples = [
39
+ "Review the following code snippet:\n\nCode: 'for i in range(10): print(i)'\nSuggestion: Use list comprehension for cleaner code.",
40
+ "Review this code:\n\nCode: 'def add(a, b): return a + b'\nSuggestion: Add type hints for better readability."
41
+ ]
42
+ prompt = f"Here are some examples of code reviews:\n\n{examples}\n\nReview the following code snippet:\n{code_snippet}"
43
+ return call_groq_api(prompt)
44
+
45
+ def smart_document_summarizer(document_text):
46
+ examples = [
47
+ "Summarize the following text:\n\nText: 'Quantum computing is a rapidly evolving field with potential to revolutionize technology.'\nSummary: 'Quantum computing could transform technology.'",
48
+ "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.'"
49
+ ]
50
+ prompt = f"Here are some examples of summaries:\n\n{examples}\n\nSummarize this document:\n{document_text}"
51
+ return call_groq_api(prompt)
52
+
53
+ def interactive_study_planner(exam_schedule):
54
+ examples = [
55
+ "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.'",
56
+ "Generate a study plan for:\n\nSchedule: 'Exams in 2 weeks'\nPlan: 'Focus on subjects with more weight and review daily.'"
57
+ ]
58
+ prompt = f"Here are some examples of study plans:\n\n{examples}\n\nCreate a study plan for the following schedule:\n{exam_schedule}"
59
+ return call_groq_api(prompt)
60
+
61
+ def real_time_qa_support(question):
62
+ examples = [
63
+ "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).'",
64
+ "Provide an explanation for:\n\nQuestion: 'What is photosynthesis?'\nAnswer: 'Photosynthesis is the process by which plants convert light energy into chemical energy.'"
65
+ ]
66
+ prompt = f"Here are some examples of Q&A responses:\n\n{examples}\n\nAnswer the following question:\n{question}"
67
+ return call_groq_api(prompt)
68
+
69
+ def mental_health_check_in(feelings):
70
+ examples = [
71
+ "Provide advice for:\n\nFeeling: 'Stressed about exams'\nAdvice: 'Take breaks, get enough sleep, and manage your study time effectively.'",
72
+ "Offer support for:\n\nFeeling: 'Feeling overwhelmed'\nAdvice: 'Consider relaxation techniques and seek support from friends or a counselor.'"
73
+ ]
74
+ prompt = f"Here are some examples of mental health advice:\n\n{examples}\n\nProvide advice for:\n{feelings}"
75
+ return call_groq_api(prompt)
76
+
77
+ # Define Streamlit app
78
+ st.title("EduNexus: The Ultimate AI-Powered Student Companion")
79
+
80
+ # Personalized Learning Assistant
81
+ st.header("Personalized Learning Assistant")
82
+ topic_input = st.text_input("Enter a topic you want to learn about")
83
+ if st.button("Generate Learning Material"):
84
+ if topic_input:
85
+ st.write(personalized_learning_assistant(topic_input))
86
+ else:
87
+ st.write("Please enter a topic.")
88
+
89
+ # AI Coding Mentor
90
+ st.header("AI Coding Mentor")
91
+ code_input = st.text_area("Paste your code snippet")
92
+ if st.button("Get Coding Assistance"):
93
+ if code_input:
94
+ st.write(ai_coding_mentor(code_input))
95
+ else:
96
+ st.write("Please paste your code snippet.")
97
+
98
+ # Smart Document Summarizer
99
+ st.header("Smart Document Summarizer")
100
+ doc_input = st.text_area("Paste the text of the document")
101
+ if st.button("Summarize Document"):
102
+ if doc_input:
103
+ st.write(smart_document_summarizer(doc_input))
104
+ else:
105
+ st.write("Please paste the document text.")
106
+
107
+ # Interactive Study Planner
108
+ st.header("Interactive Study Planner")
109
+ schedule_input = st.text_area("Enter your exam schedule or important dates")
110
+ if st.button("Generate Study Plan"):
111
+ if schedule_input:
112
+ st.write(interactive_study_planner(schedule_input))
113
+ else:
114
+ st.write("Please enter your exam schedule.")
115
+
116
+ # Real-Time Q&A Support
117
+ st.header("Real-Time Q&A Support")
118
+ question_input = st.text_input("Ask any academic question")
119
+ if st.button("Get Answer"):
120
+ if question_input:
121
+ st.write(real_time_qa_support(question_input))
122
+ else:
123
+ st.write("Please ask a question.")
124
+
125
+ # Mental Health Check-In
126
+ st.header("Mental Health Check-In")
127
+ feelings_input = st.text_area("How are you feeling?")
128
+ if st.button("Check In"):
129
+ if feelings_input:
130
+ st.write(mental_health_check_in(feelings_input))
131
+ else:
132
+ st.write("Please describe your feelings.")
133
+