Spaces:
Sleeping
Sleeping
File size: 3,176 Bytes
e4cba99 3f48f6d 20cfb40 fd87352 a06864c fd87352 e4cba99 20cfb40 6f15760 3f48f6d 533dd6b 3f48f6d a06864c 0f11b93 a06864c 20cfb40 3f48f6d a06864c 0f11b93 a06864c 20cfb40 3f48f6d a06864c 0f11b93 a06864c 20cfb40 3f48f6d efad7dd 3f48f6d efad7dd 3f48f6d 23703f2 3f48f6d 23703f2 efad7dd 23703f2 efad7dd 23703f2 efad7dd 23703f2 2a9ddb1 23703f2 efad7dd a06864c e4cba99 3f48f6d |
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 |
import gradio as gr
import cohere
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Initialize Cohere API client
co = cohere.Client()
# Adaptive learning functions
def assess_knowledge(name, experience, goals):
try:
level_prompt = f"User Name: {name}, Experience: {experience}, Goals: {goals}. Classify as beginner, intermediate, or advanced."
response = co.generate(prompt=level_prompt)
level = response.generations[0].text.strip()
return level
except Exception as e:
return "Error in knowledge assessment: " + str(e)
def generate_explanation(topic, level):
try:
explanation_prompt = f"Explain the topic '{topic}' to a {level} level student."
response = co.generate(prompt=explanation_prompt)
explanation = response.generations[0].text.strip()
return explanation
except Exception as e:
return "Error in generating explanation: " + str(e)
def generate_challenge(topic, level):
try:
challenge_prompt = f"Generate a {level} level challenge for learning '{topic}' in computer science."
response = co.generate(prompt=challenge_prompt)
challenge = response.generations[0].text.strip()
return challenge
except Exception as e:
return "Error in generating challenge: " + str(e)
def tutor_interface(name, experience, goals, topic, request_challenge=False):
# Assess knowledge and generate explanation as before
level = assess_knowledge(name, experience, goals)
explanation = generate_explanation(topic, level)
# Generate challenge if requested
if request_challenge:
challenge = generate_challenge(topic, level)
return f"**Level:** {level}\n\n**Explanation:**\n{explanation}\n\n**Challenge:**\n{challenge}"
else:
return f"**Level:** {level}\n\n**Explanation:**\n{explanation}"
# Updated Gradio interface with challenge request option
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# Adaptive Computer Science Tutor")
gr.Markdown("Welcome to your personalized Computer Science Tutor! This tutor adapts to your level and learning pace, offering explanations and practice challenges in areas like data structures, algorithms, and more.")
with gr.Row():
with gr.Column():
name = gr.Textbox(label="Your Name", placeholder="Enter your name")
experience = gr.Textbox(label="Describe your programming experience", placeholder="e.g., Beginner, 2 years Python")
with gr.Column():
goals = gr.Textbox(label="What are your learning goals?", placeholder="What do you want to achieve?")
topic = gr.Textbox(label="Topic you'd like to learn about", placeholder="e.g., Binary Search, Arrays")
request_challenge = gr.Checkbox(label="Include a practice challenge", value=False)
output = gr.Markdown(label="Tutor's Response", min_height=48)
submit_button = gr.Button("Get Started", variant="primary")
submit_button.click(tutor_interface, inputs=[name, experience, goals, topic, request_challenge], outputs=output)
if __name__ == "__main__":
demo.launch() |