eddiebee's picture
Reduce the output min_height
2a9ddb1 verified
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()