eddiebee commited on
Commit
20cfb40
·
verified ·
1 Parent(s): 533dd6b

Update app.py

Browse files

Revert changes

Files changed (1) hide show
  1. app.py +18 -33
app.py CHANGED
@@ -1,65 +1,58 @@
1
  import gradio as gr
2
  import cohere
3
- from dotenv import load_dotenv
4
  import os
 
5
 
6
  # Load environment variables
7
  load_dotenv()
8
 
9
- # Initialize Cohere API client with error handling for missing API key
10
- COHERE_API_KEY = os.getenv("COHERE_API_KEY")
11
- if COHERE_API_KEY is None:
12
- raise ValueError("Cohere API key not found. Please set COHERE_API_KEY in environment variables.")
13
- co = cohere.Client(COHERE_API_KEY)
14
-
15
- MODEL_NAME = 'command-r-plus' # Define model name as a constant
16
 
17
  # Adaptive learning functions
18
  def assess_knowledge(name, experience, goals):
19
  try:
20
  level_prompt = f"User Name: {name}, Experience: {experience}, Goals: {goals}. Classify as beginner, intermediate, or advanced."
21
- response = co.generate(prompt=level_prompt, model=MODEL_NAME)
22
  level = response.generations[0].text.strip()
23
  return level
24
  except Exception as e:
25
- return f"Error in knowledge assessment: {str(e)}"
26
 
27
  def generate_explanation(topic, level):
28
  try:
29
  explanation_prompt = f"Explain the topic '{topic}' to a {level} level student."
30
- response = co.generate(prompt=explanation_prompt, model=MODEL_NAME)
31
  explanation = response.generations[0].text.strip()
32
  return explanation
33
  except Exception as e:
34
- return f"Error in generating explanation: {str(e)}"
35
 
36
  def generate_challenge(topic, level):
37
  try:
38
  challenge_prompt = f"Generate a {level} level challenge for learning '{topic}' in computer science."
39
- response = co.generate(prompt=challenge_prompt, model=MODEL_NAME)
40
  challenge = response.generations[0].text.strip()
41
  return challenge
42
  except Exception as e:
43
- return f"Error in generating challenge: {str(e)}"
44
 
45
- # Tutor interface function with error handling and formatted output
46
  def tutor_interface(name, experience, goals, topic, request_challenge=False):
 
47
  if not all([name, experience, goals, topic]):
48
- return "", "Please fill in all required fields."
49
 
50
- # Generate adaptive content with structured response formatting
51
  level = assess_knowledge(name, experience, goals)
52
  explanation = generate_explanation(topic, level)
53
 
54
  if request_challenge:
55
  challenge = generate_challenge(topic, level)
56
- response = f"**Level:** {level}\n\n**Explanation:**\n{explanation}\n\n**Challenge:**\n{challenge}"
57
  else:
58
- response = f"**Level:** {level}\n\n**Explanation:**\n{explanation}"
59
-
60
- return "Generation complete!", response
61
 
62
- # Gradio UI setup with a loading status and structured layout
63
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
64
  gr.Markdown("""
65
  # Adaptive Computer Science Tutor
@@ -84,23 +77,15 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
84
 
85
  request_challenge = gr.Checkbox(label="Include a practice challenge", value=False)
86
 
87
- # Status message for loading state
88
- status = gr.Markdown("", elem_id="status")
89
-
90
  # Output display with markdown formatting
91
  output = gr.Markdown(label="Tutor's Response")
92
 
93
- # Submit button
94
  submit_button = gr.Button("Get Started", variant="primary")
95
-
96
- # Handle submission with loading state
97
  submit_button.click(
98
- fn=lambda: ("Generating response...", ""), # Show loading message
99
- outputs=[status, output],
100
- ).then(
101
- fn=tutor_interface,
102
  inputs=[name, experience, goals, topic, request_challenge],
103
- outputs=[status, output],
104
  )
105
 
106
  # Launch the app
 
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(os.getenv('COHERE_API_KEY'))
 
 
 
 
 
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
 
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,
 
 
 
87
  inputs=[name, experience, goals, topic, request_challenge],
88
+ outputs=output
89
  )
90
 
91
  # Launch the app