TheHickman commited on
Commit
b92e0fa
·
verified ·
1 Parent(s): f83b893

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -75
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
- from anthropic import Anthropic
3
 
4
- client = Anthropic()
5
 
6
  CONTENT_PARAGRAPHS = [
7
  "Text generation is the task of producing natural language text given an input prompt. It is commonly used for chatbots, creative writing, summarization, and code generation.",
@@ -23,22 +23,27 @@ def explain_text(selected_text, current_html):
23
  selected_text = selected_text.strip()
24
 
25
  try:
26
- response = client.messages.create(
27
- model="claude-opus-4-5",
28
- max_tokens=300,
29
  messages=[
 
 
 
 
30
  {
31
  "role": "user",
32
- "content": f"You are an ML instructor. Explain this text from a learning resource in 2-3 clear, simple sentences for a beginner:\n\n\"{selected_text}\"",
33
  }
34
  ],
 
 
35
  )
36
- explanation = response.content[0].text.strip()
 
 
37
  except Exception as e:
38
  return current_html, f"❌ Error: {str(e)}"
39
 
40
- # Replace the selected text in the HTML with explanation block
41
- escaped = selected_text.replace('"', '"')
42
  replacement = (
43
  f'<mark class="explained-original" title="Original text">{selected_text}</mark>'
44
  f'<span class="explanation-block">💡 {explanation}</span>'
@@ -58,9 +63,7 @@ def reset_content():
58
 
59
  CSS = """
60
  @import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@600&family=Source+Serif+4:ital,opsz,wght@0,8..60,400;1,8..60,300&display=swap');
61
-
62
  body { background: #faf8f4 !important; }
63
-
64
  #reader-content {
65
  font-family: 'Source Serif 4', Georgia, serif;
66
  font-size: 1.05rem;
@@ -72,18 +75,7 @@ body { background: #faf8f4 !important; }
72
  border: 1px solid #e8dfc8;
73
  min-height: 320px;
74
  }
75
-
76
- #reader-content h1 {
77
- font-family: 'Playfair Display', Georgia, serif;
78
- font-size: 2rem;
79
- color: #1a1208;
80
- margin-bottom: 4px;
81
- }
82
-
83
- .content-para {
84
- margin-bottom: 1.4em;
85
- }
86
-
87
  mark.explained-original {
88
  background: linear-gradient(120deg, #f5e6a3, #f0d870);
89
  border-radius: 3px;
@@ -94,7 +86,6 @@ mark.explained-original {
94
  font-style: italic;
95
  opacity: 0.75;
96
  }
97
-
98
  .explanation-block {
99
  display: inline-block;
100
  background: #fffbee;
@@ -105,50 +96,6 @@ mark.explained-original {
105
  color: #3a2e08;
106
  font-size: 0.93rem;
107
  line-height: 1.6;
108
- box-shadow: 0 2px 8px rgba(180,140,0,0.1);
109
- }
110
-
111
- #hint-text {
112
- font-family: 'Source Serif 4', Georgia, serif;
113
- color: #a09070;
114
- font-size: 0.88rem;
115
- margin-bottom: 8px;
116
- }
117
-
118
- .selected-box textarea {
119
- font-family: 'Source Serif 4', Georgia, serif !important;
120
- font-size: 0.95rem !important;
121
- color: #3a2e08 !important;
122
- background: #fffbee !important;
123
- border: 1px solid #d4c88a !important;
124
- border-radius: 8px !important;
125
- }
126
-
127
- .explain-btn {
128
- background: #1a1208 !important;
129
- color: #f5e8c0 !important;
130
- border-radius: 8px !important;
131
- font-family: 'Source Serif 4', Georgia, serif !important;
132
- font-size: 0.95rem !important;
133
- border: none !important;
134
- }
135
- .explain-btn:hover {
136
- background: #2e2010 !important;
137
- }
138
-
139
- .reset-btn {
140
- background: transparent !important;
141
- color: #a09070 !important;
142
- border: 1px solid #d4c88a !important;
143
- border-radius: 8px !important;
144
- font-family: 'Source Serif 4', Georgia, serif !important;
145
- font-size: 0.9rem !important;
146
- }
147
-
148
- .status-text {
149
- font-family: 'Source Serif 4', Georgia, serif !important;
150
- color: #6b5a10 !important;
151
- font-size: 0.85rem !important;
152
  }
153
  """
154
 
@@ -181,9 +128,6 @@ with gr.Blocks(css=CSS, js=JS_CAPTURE_SELECTION, title="ML Reader — Highlight
181
  <p style="font-family:'Source Serif 4',Georgia,serif; font-size:0.82rem; color:#a09070; letter-spacing:0.08em; text-transform:uppercase; margin:0 0 12px;">
182
  Machine Learning · Reading Guide
183
  </p>
184
- <p id="hint-text" style="font-family:'Source Serif 4',Georgia,serif; color:#a09070; font-size:0.88rem; margin-bottom:20px;">
185
- ✦ Highlight any text below, then click <em>Explain Selection</em> to replace it with an AI explanation.
186
- </p>
187
  </div>
188
  """)
189
 
@@ -203,12 +147,11 @@ with gr.Blocks(css=CSS, js=JS_CAPTURE_SELECTION, title="ML Reader — Highlight
203
  )
204
 
205
  with gr.Row():
206
- explain_btn = gr.Button("✦ Explain Selection", elem_classes=["explain-btn"], variant="primary")
207
- reset_btn = gr.Button("↺ Reset", elem_classes=["reset-btn"])
208
 
209
- status = gr.Textbox(label="", interactive=False, show_label=False, elem_classes=["status-text"])
210
 
211
- # Hidden state to track current HTML
212
  html_state = gr.State(f'<div id="reader-content">{INITIAL_HTML}</div>')
213
 
214
  explain_btn.click(
 
1
  import gradio as gr
2
+ from openai import OpenAI
3
 
4
+ client = OpenAI()
5
 
6
  CONTENT_PARAGRAPHS = [
7
  "Text generation is the task of producing natural language text given an input prompt. It is commonly used for chatbots, creative writing, summarization, and code generation.",
 
23
  selected_text = selected_text.strip()
24
 
25
  try:
26
+ response = client.chat.completions.create(
27
+ model="gpt-4o-mini",
 
28
  messages=[
29
+ {
30
+ "role": "system",
31
+ "content": "You are an ML instructor who explains concepts clearly and simply for beginners."
32
+ },
33
  {
34
  "role": "user",
35
+ "content": f"Explain this text from a learning resource in 2-3 clear, simple sentences for a beginner:\n\n\"{selected_text}\""
36
  }
37
  ],
38
+ max_tokens=300,
39
+ temperature=0.7,
40
  )
41
+
42
+ explanation = response.choices[0].message.content.strip()
43
+
44
  except Exception as e:
45
  return current_html, f"❌ Error: {str(e)}"
46
 
 
 
47
  replacement = (
48
  f'<mark class="explained-original" title="Original text">{selected_text}</mark>'
49
  f'<span class="explanation-block">💡 {explanation}</span>'
 
63
 
64
  CSS = """
65
  @import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@600&family=Source+Serif+4:ital,opsz,wght@0,8..60,400;1,8..60,300&display=swap');
 
66
  body { background: #faf8f4 !important; }
 
67
  #reader-content {
68
  font-family: 'Source Serif 4', Georgia, serif;
69
  font-size: 1.05rem;
 
75
  border: 1px solid #e8dfc8;
76
  min-height: 320px;
77
  }
78
+ .content-para { margin-bottom: 1.4em; }
 
 
 
 
 
 
 
 
 
 
 
79
  mark.explained-original {
80
  background: linear-gradient(120deg, #f5e6a3, #f0d870);
81
  border-radius: 3px;
 
86
  font-style: italic;
87
  opacity: 0.75;
88
  }
 
89
  .explanation-block {
90
  display: inline-block;
91
  background: #fffbee;
 
96
  color: #3a2e08;
97
  font-size: 0.93rem;
98
  line-height: 1.6;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  }
100
  """
101
 
 
128
  <p style="font-family:'Source Serif 4',Georgia,serif; font-size:0.82rem; color:#a09070; letter-spacing:0.08em; text-transform:uppercase; margin:0 0 12px;">
129
  Machine Learning · Reading Guide
130
  </p>
 
 
 
131
  </div>
132
  """)
133
 
 
147
  )
148
 
149
  with gr.Row():
150
+ explain_btn = gr.Button("✦ Explain Selection", variant="primary")
151
+ reset_btn = gr.Button("↺ Reset")
152
 
153
+ status = gr.Textbox(label="", interactive=False, show_label=False)
154
 
 
155
  html_state = gr.State(f'<div id="reader-content">{INITIAL_HTML}</div>')
156
 
157
  explain_btn.click(