Spaces:
Sleeping
Sleeping
Add adaptive UI to loading state and switch model
Browse files
app.py
CHANGED
|
@@ -7,13 +7,14 @@ from dotenv import load_dotenv
|
|
| 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:
|
|
@@ -22,7 +23,7 @@ def assess_knowledge(name, experience, goals):
|
|
| 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:
|
|
@@ -31,7 +32,7 @@ def generate_explanation(topic, level):
|
|
| 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:
|
|
@@ -80,12 +81,26 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 80 |
# Output display with markdown formatting
|
| 81 |
output = gr.Markdown(label="Tutor's Response")
|
| 82 |
|
| 83 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
submit_button = gr.Button("Get Started", variant="primary")
|
| 85 |
submit_button.click(
|
| 86 |
-
tutor_interface,
|
| 87 |
inputs=[name, experience, goals, topic, request_challenge],
|
| 88 |
-
outputs=output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
)
|
| 90 |
|
| 91 |
# Launch the app
|
|
|
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
# Initialize Cohere API client
|
| 10 |
+
co = cohere.Client(os.getenv('COHERE_API_KEY'))
|
| 11 |
+
MODEL_NAME = 'command-r-plus' # Define model name as a constant
|
| 12 |
|
| 13 |
# Adaptive learning functions
|
| 14 |
def assess_knowledge(name, experience, goals):
|
| 15 |
try:
|
| 16 |
level_prompt = f"User Name: {name}, Experience: {experience}, Goals: {goals}. Classify as beginner, intermediate, or advanced."
|
| 17 |
+
response = co.generate(prompt=level_prompt, model=MODEL_NAME)
|
| 18 |
level = response.generations[0].text.strip()
|
| 19 |
return level
|
| 20 |
except Exception as e:
|
|
|
|
| 23 |
def generate_explanation(topic, level):
|
| 24 |
try:
|
| 25 |
explanation_prompt = f"Explain the topic '{topic}' to a {level} level student."
|
| 26 |
+
response = co.generate(prompt=explanation_prompt, model=MODEL_NAME)
|
| 27 |
explanation = response.generations[0].text.strip()
|
| 28 |
return explanation
|
| 29 |
except Exception as e:
|
|
|
|
| 32 |
def generate_challenge(topic, level):
|
| 33 |
try:
|
| 34 |
challenge_prompt = f"Generate a {level} level challenge for learning '{topic}' in computer science."
|
| 35 |
+
response = co.generate(prompt=challenge_prompt, model=MODEL_NAME)
|
| 36 |
challenge = response.generations[0].text.strip()
|
| 37 |
return challenge
|
| 38 |
except Exception as e:
|
|
|
|
| 81 |
# Output display with markdown formatting
|
| 82 |
output = gr.Markdown(label="Tutor's Response")
|
| 83 |
|
| 84 |
+
# Loading indicator
|
| 85 |
+
with gr.Row():
|
| 86 |
+
loading = gr.Markdown("Loading...", visible=False)
|
| 87 |
+
|
| 88 |
+
# Submit button with loading feedback
|
| 89 |
submit_button = gr.Button("Get Started", variant="primary")
|
| 90 |
submit_button.click(
|
| 91 |
+
fn=tutor_interface,
|
| 92 |
inputs=[name, experience, goals, topic, request_challenge],
|
| 93 |
+
outputs=[output],
|
| 94 |
+
_js="""
|
| 95 |
+
() => {
|
| 96 |
+
document.querySelector('[id^=loading]').style.display = 'block';
|
| 97 |
+
return null;
|
| 98 |
+
}
|
| 99 |
+
"""
|
| 100 |
+
).then(
|
| 101 |
+
fn=lambda: gr.update(visible=False),
|
| 102 |
+
inputs=None,
|
| 103 |
+
outputs=loading
|
| 104 |
)
|
| 105 |
|
| 106 |
# Launch the app
|