Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,114 +1,86 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cohere
|
|
|
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
|
|
|
|
| 5 |
load_dotenv()
|
| 6 |
|
| 7 |
-
# Initialize Cohere API
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
co = cohere.Client(userdata.get('COHERE_API_KEY'))
|
| 11 |
|
| 12 |
# Adaptive learning functions
|
| 13 |
def assess_knowledge(name, experience, goals):
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
def generate_explanation(topic, level):
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
def generate_challenge(topic, level):
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
def tutor_interface(name, experience, goals, topic, request_challenge=False):
|
| 38 |
-
#
|
| 39 |
if not all([name, experience, goals, topic]):
|
| 40 |
-
return "Please fill in all
|
| 41 |
|
| 42 |
-
#
|
| 43 |
level = assess_knowledge(name, experience, goals)
|
| 44 |
explanation = generate_explanation(topic, level)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
# Generate challenge if requested
|
| 48 |
if request_challenge:
|
| 49 |
challenge = generate_challenge(topic, level)
|
| 50 |
-
return f"Level
|
| 51 |
else:
|
| 52 |
-
return f"Level
|
| 53 |
-
|
| 54 |
|
| 55 |
-
# Gradio UI with
|
| 56 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 57 |
-
gr.Markdown(
|
| 58 |
-
"""
|
| 59 |
# Adaptive Computer Science Tutor
|
| 60 |
|
| 61 |
Welcome to your personalized Computer Science Tutor! This tutor adapts to your level and learning pace,
|
| 62 |
offering explanations and practice challenges in areas like data structures, algorithms, and more.
|
| 63 |
|
| 64 |
### How It Works
|
| 65 |
-
1. Enter your background and goals
|
| 66 |
-
2. Choose a topic you want to learn
|
| 67 |
-
3. Get adaptive explanations and challenges suited to your level
|
| 68 |
-
|
| 69 |
-
)
|
| 70 |
|
| 71 |
-
with gr.
|
| 72 |
-
with gr.
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
)
|
| 80 |
-
experience = gr.Textbox(
|
| 81 |
-
label="Programming Experience",
|
| 82 |
-
placeholder="e.g., Beginner, 2 years Python, etc.",
|
| 83 |
-
# required=True
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
with gr.Column(scale=1):
|
| 87 |
-
goals = gr.Textbox(
|
| 88 |
-
label="Learning Goals",
|
| 89 |
-
placeholder="What do you want to achieve?",
|
| 90 |
-
# required=True
|
| 91 |
-
)
|
| 92 |
-
topic = gr.Textbox(
|
| 93 |
-
label="Topic of Interest",
|
| 94 |
-
placeholder="e.g., Binary Search, Arrays, etc.",
|
| 95 |
-
# required=True
|
| 96 |
-
)
|
| 97 |
-
|
| 98 |
-
with gr.Group():
|
| 99 |
-
with gr.Row():
|
| 100 |
-
request_challenge = gr.Checkbox(
|
| 101 |
-
label="Include a practice challenge",
|
| 102 |
-
value=False
|
| 103 |
-
)
|
| 104 |
-
|
| 105 |
-
# Output area
|
| 106 |
-
output = gr.Markdown(
|
| 107 |
-
min_height=50,
|
| 108 |
-
label="Tutor's Response"
|
| 109 |
-
)
|
| 110 |
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
submit_button = gr.Button("Get Started", variant="primary")
|
| 113 |
submit_button.click(
|
| 114 |
tutor_interface,
|
|
@@ -116,7 +88,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 116 |
outputs=output
|
| 117 |
)
|
| 118 |
|
| 119 |
-
|
| 120 |
# Launch the app
|
| 121 |
if __name__ == "__main__":
|
| 122 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cohere
|
| 3 |
+
import os
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
+
# Load environment variables
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
+
# Initialize Cohere API client
|
| 10 |
+
co = cohere.Client()
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Adaptive learning functions
|
| 13 |
def assess_knowledge(name, experience, goals):
|
| 14 |
+
try:
|
| 15 |
+
level_prompt = f"User Name: {name}, Experience: {experience}, Goals: {goals}. Classify as beginner, intermediate, or advanced."
|
| 16 |
+
response = co.generate(prompt=level_prompt)
|
| 17 |
+
level = response.generations[0].text.strip()
|
| 18 |
+
return level
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return "Error in knowledge assessment: " + str(e)
|
| 21 |
|
| 22 |
def generate_explanation(topic, level):
|
| 23 |
+
try:
|
| 24 |
+
explanation_prompt = f"Explain the topic '{topic}' to a {level} level student."
|
| 25 |
+
response = co.generate(prompt=explanation_prompt)
|
| 26 |
+
explanation = response.generations[0].text.strip()
|
| 27 |
+
return explanation
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return "Error in generating explanation: " + str(e)
|
| 30 |
|
| 31 |
def generate_challenge(topic, level):
|
| 32 |
+
try:
|
| 33 |
+
challenge_prompt = f"Generate a {level} level challenge for learning '{topic}' in computer science."
|
| 34 |
+
response = co.generate(prompt=challenge_prompt)
|
| 35 |
+
challenge = response.generations[0].text.strip()
|
| 36 |
+
return challenge
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return "Error in generating challenge: " + str(e)
|
| 39 |
|
| 40 |
def tutor_interface(name, experience, goals, topic, request_challenge=False):
|
| 41 |
+
# Validate inputs
|
| 42 |
if not all([name, experience, goals, topic]):
|
| 43 |
+
return "Please fill in all required fields."
|
| 44 |
|
| 45 |
+
# Generate adaptive content
|
| 46 |
level = assess_knowledge(name, experience, goals)
|
| 47 |
explanation = generate_explanation(topic, level)
|
| 48 |
|
|
|
|
|
|
|
| 49 |
if request_challenge:
|
| 50 |
challenge = generate_challenge(topic, level)
|
| 51 |
+
return f"**Level:** {level}\n\n**Explanation:**\n{explanation}\n\n**Challenge:**\n{challenge}"
|
| 52 |
else:
|
| 53 |
+
return f"**Level:** {level}\n\n**Explanation:**\n{explanation}"
|
|
|
|
| 54 |
|
| 55 |
+
# Gradio UI setup with theme and structured layout
|
| 56 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 57 |
+
gr.Markdown("""
|
|
|
|
| 58 |
# Adaptive Computer Science Tutor
|
| 59 |
|
| 60 |
Welcome to your personalized Computer Science Tutor! This tutor adapts to your level and learning pace,
|
| 61 |
offering explanations and practice challenges in areas like data structures, algorithms, and more.
|
| 62 |
|
| 63 |
### How It Works
|
| 64 |
+
1. Enter your background and goals.
|
| 65 |
+
2. Choose a topic you want to learn.
|
| 66 |
+
3. Get adaptive explanations and challenges suited to your level.
|
| 67 |
+
""")
|
|
|
|
| 68 |
|
| 69 |
+
with gr.Row():
|
| 70 |
+
with gr.Column(scale=1):
|
| 71 |
+
name = gr.Textbox(label="Your Name", placeholder="Enter your name")
|
| 72 |
+
experience = gr.Textbox(label="Programming Experience", placeholder="e.g., Beginner, 2 years Python")
|
| 73 |
+
|
| 74 |
+
with gr.Column(scale=1):
|
| 75 |
+
goals = gr.Textbox(label="Learning Goals", placeholder="What do you want to achieve?")
|
| 76 |
+
topic = gr.Textbox(label="Topic of Interest", placeholder="e.g., Binary Search, Arrays")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
request_challenge = gr.Checkbox(label="Include a practice challenge", value=False)
|
| 79 |
+
|
| 80 |
+
# Output display with markdown formatting
|
| 81 |
+
output = gr.Markdown(label="Tutor's Response")
|
| 82 |
+
|
| 83 |
+
# Trigger action
|
| 84 |
submit_button = gr.Button("Get Started", variant="primary")
|
| 85 |
submit_button.click(
|
| 86 |
tutor_interface,
|
|
|
|
| 88 |
outputs=output
|
| 89 |
)
|
| 90 |
|
|
|
|
| 91 |
# Launch the app
|
| 92 |
if __name__ == "__main__":
|
| 93 |
demo.launch()
|