tiahchia commited on
Commit
7f0d419
·
verified ·
1 Parent(s): 65c622b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -82
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import gradio as gr
2
  from utils import generate_lesson
3
  import html
4
- import re
5
 
6
  # Beginner coding topics
7
  TOPICS = [
@@ -12,60 +11,14 @@ TOPICS = [
12
  "Building a Button in HTML/CSS", "Making a Webpage Layout",
13
  ]
14
 
15
- # CSS for polished glowing UI
16
  css_styles = """
17
  body {
18
- background: linear-gradient(135deg, #0f0f0f, #1a1a1a);
19
  color: #fa29bc;
20
- font-family: Consolas, monospace;
21
  padding: 20px;
22
  }
23
  footer { display:none; }
24
-
25
- .card {
26
- background: #1a1a1a;
27
- padding: 25px;
28
- border-radius: 16px;
29
- margin-bottom: 25px;
30
- box-shadow: 0 0 30px rgba(250, 41, 188, 0.5);
31
- transition: transform 0.2s, box-shadow 0.2s;
32
- position: relative;
33
- overflow: hidden;
34
- }
35
- .card::before {
36
- content: "";
37
- position: absolute;
38
- top: -50%;
39
- left: -50%;
40
- width: 200%;
41
- height: 200%;
42
- background: radial-gradient(circle at center, rgba(250,41,188,0.2), transparent 70%);
43
- animation: glow 5s linear infinite;
44
- }
45
- @keyframes glow {
46
- 0% { transform: rotate(0deg); }
47
- 100% { transform: rotate(360deg); }
48
- }
49
- .card:hover {
50
- transform: translateY(-5px);
51
- box-shadow: 0 0 60px rgba(250, 41, 188, 0.8);
52
- }
53
- .card pre {
54
- background: #222;
55
- padding: 15px;
56
- border-radius: 12px;
57
- overflow-x: auto;
58
- font-family: Consolas, monospace;
59
- color: #fa29bc;
60
- white-space: pre-wrap;
61
- }
62
- .exercise {
63
- background: #111;
64
- border-left: 5px solid #fa29bc;
65
- padding: 12px;
66
- margin-top: 15px;
67
- border-radius: 8px;
68
- }
69
  button {
70
  background:#fa29bc;
71
  color:white;
@@ -76,50 +29,36 @@ button {
76
  }
77
  """
78
 
79
- def highlight_code_blocks(text):
80
- """
81
- Finds triple-backtick code blocks and wraps them in <pre> tags for proper display.
82
- Escapes HTML to prevent rendering issues.
83
- """
84
- def repl(match):
85
- lang = match.group(1) or ""
86
- code = match.group(2)
87
- escaped_code = html.escape(code)
88
- return f"<pre class='{lang}'>{escaped_code}</pre>"
89
-
90
- # Replace ```lang ... ``` with <pre> blocks
91
- return re.sub(r"```(\w*)\n(.*?)```", repl, text, flags=re.DOTALL)
92
-
93
  with gr.Blocks(title="Beginner Coding Lessons", css=css_styles) as demo:
94
 
95
  with gr.Column():
96
  topic_dropdown = gr.Dropdown(choices=TOPICS, label="Choose Topic", value=TOPICS[0])
97
  generate_btn = gr.Button("Generate Lesson")
 
 
98
  lesson_output = gr.Markdown()
 
 
 
99
 
100
  def generate(topic):
 
 
 
 
101
  file_path, lesson_text = generate_lesson(topic)
102
-
103
- # Escape HTML outside of code blocks
104
- # First, highlight code blocks properly
105
- lesson_text = highlight_code_blocks(lesson_text)
106
-
107
- # Escape remaining HTML
108
  lesson_text = html.escape(lesson_text)
109
- # Allow <pre> tags to render
110
- lesson_text = lesson_text.replace("&lt;pre&gt;", "<pre>").replace("&lt;/pre&gt;", "</pre>")
111
-
112
- # Style practice exercises
113
- lesson_text = lesson_text.replace(
114
- "Practice:", "<div class='exercise'><strong>Practice:</strong>"
115
- ).replace("\n", "<br>") + "</div>"
116
 
117
- # Wrap in glowing card
118
- card_text = f"<div class='card'>{lesson_text}</div>"
119
- return card_text
120
-
121
- generate_btn.click(fn=generate, inputs=topic_dropdown, outputs=lesson_output)
122
 
123
  if __name__ == "__main__":
124
  demo.launch()
125
-
 
1
  import gradio as gr
2
  from utils import generate_lesson
3
  import html
 
4
 
5
  # Beginner coding topics
6
  TOPICS = [
 
11
  "Building a Button in HTML/CSS", "Making a Webpage Layout",
12
  ]
13
 
 
14
  css_styles = """
15
  body {
16
+ background: #111;
17
  color: #fa29bc;
18
+ font-family: monospace;
19
  padding: 20px;
20
  }
21
  footer { display:none; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  button {
23
  background:#fa29bc;
24
  color:white;
 
29
  }
30
  """
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  with gr.Blocks(title="Beginner Coding Lessons", css=css_styles) as demo:
33
 
34
  with gr.Column():
35
  topic_dropdown = gr.Dropdown(choices=TOPICS, label="Choose Topic", value=TOPICS[0])
36
  generate_btn = gr.Button("Generate Lesson")
37
+
38
+ # Standard Markdown output for lessons
39
  lesson_output = gr.Markdown()
40
+
41
+ # Status message for "please wait"
42
+ status_label = gr.Label(value="")
43
 
44
  def generate(topic):
45
+ # Show "please wait" message
46
+ status_label.value = "✨ Your lesson is being generated... please wait!"
47
+
48
+ # Generate the lesson
49
  file_path, lesson_text = generate_lesson(topic)
50
+
51
+ # Escape HTML to avoid rendering issues
 
 
 
 
52
  lesson_text = html.escape(lesson_text)
53
+ # Preserve line breaks in Markdown
54
+ lesson_text = lesson_text.replace("\\n", "\n")
55
+
56
+ # Clear the status message
57
+ status_label.value = ""
58
+
59
+ return lesson_text, status_label.value
60
 
61
+ generate_btn.click(fn=generate, inputs=topic_dropdown, outputs=[lesson_output, status_label])
 
 
 
 
62
 
63
  if __name__ == "__main__":
64
  demo.launch()